plt.show()
plt.imshow(np.sin(np.linspace(0,10000,10000)).reshape(100,100) ** 2, cmap='gray')
-# does not work with QT_QPA_PLATFORM=wayland
+# non-blocking does not work with QT_QPA_PLATFORM=wayland
plt.show(block=False)
plt.close()
```
- Get the handwritten digits training data with `./download-mnist.sh`
+
+ - Read the MNIST database into numpy arrays with `./read_display_mnist.py`. Plot the first ten images and show their labels, to make sure the data makes sense:
+
+ ![visualize training data](screenshots/mnist-visualize-training-data.png)
--- /dev/null
+import struct
+
+import numpy as np
+
+def load(images_file, labels_file):
+ # see http://yann.lecun.com/exdb/mnist/ for the file format
+ with open(images_file, 'rb') as f:
+ # validate magic
+ assert struct.unpack('>I', f.read(4))[0] == 0x803
+ num = struct.unpack('>I', f.read(4))[0]
+ rows = struct.unpack('>I', f.read(4))[0]
+ cols = struct.unpack('>I', f.read(4))[0]
+ images = np.frombuffer(f.read(), dtype=np.uint8, count = num * rows * cols)
+ # split them up into an array of flat image vectors, so that first axis corresponds to labels
+ images = images.reshape(num, rows * cols)
+
+ with open(labels_file, 'rb') as f:
+ # validate magic
+ assert struct.unpack('>I', f.read(4))[0] == 0x801
+ num = struct.unpack('>I', f.read(4))[0]
+ labels = np.frombuffer(f.read(), dtype=np.uint8, count=num)
+
+ return images, labels, rows, cols
--- /dev/null
+#!/usr/bin/python3
+
+import numpy as np
+import matplotlib.pyplot as plt
+
+import mnist
+
+train_images, train_labels, rows, cols = mnist.load('train-images-idx3-ubyte', 'train-labels-idx1-ubyte')
+
+# show the first bunch of training data
+for i in range(10):
+ print(f'train image #{i}: label {train_labels[i]}')
+ plt.imshow(train_images[i].reshape(rows, cols), cmap='gray')
+ plt.show()