Download the training/test images and labels:
- train-images-idx3-ubyte.gz: training set images
- train-labels-idx1-ubyte.gz: training set labels
- t10k-images-idx3-ubyte.gz: test set images
- t10k-labels-idx1-ubyte.gz: test set labels
And uncompress them in a workdir, say samples/.
Get the python-mnist package from PyPi:
pip install python-mnist
Import the mnist package and read the training/test images:
from mnist import MNIST
mndata = MNIST('samples')
images, labels = mndata.load_training()
# or
images, labels = mndata.load_testing()
To display an image to the console:
index = random.randrange(0, len(images)) # choose an index ;-)
print(mndata.display(images[index]))
You’ll get something like this:
............................
............................
............................
............................
............................
.................@@.........
..............@@@@@.........
............@@@@............
..........@@................
..........@.................
...........@................
...........@................
...........@...@............
...........@@@@@.@..........
...........@@@...@@.........
...........@@.....@.........
..................@.........
..................@@........
..................@@........
..................@.........
.................@@.........
...........@.....@..........
...........@....@@..........
............@@@@............
.............@..............
............................
............................
............................
Explanation:
- Each image of the images list is a Python
listof unsigned bytes. - The labels is an Python
arrayof unsigned bytes.