We load the MNIST data into our session from Keras module with mnist.load_data(). After doing so, we will print the shape, size of the dataset as well as the number of classes and unique labels in the dataset.
(X_train, y_train), (X_test, y_test) = mnist.load_data()
print('Size of the training_set: ', X_train.shape)
print('Size of the test_set: ', X_test.shape)
print('Shape of each image: ', X_train[0].shape)
print('Total number of classes: ', len(np.unique(y_train)))
print('Unique class labels: ', np.unique(y_train))
We have a dataset with 10 different classes and 60000 images with each image having a shape of 28*28 and each class having 6000 images.
Let's plot and see what the handwritten images look like...
# Plot of 9 random images
for i in range(0, 9):
plt.subplot(331+i) # plot of 3 rows and 3 columns
plt.axis('off') # turn off axis
plt.imshow(X_train[i], cmap='gray') # gray scale
Figure 14.2: Plot of nine MNIST digits from the training set
Let's plot a handwritten digit from each class...
# plotting image from each class
fig=plt.figure(figsize=(8, 4))
columns = 5
rows = 2
for i in range(0, rows*columns):
fig.add_subplot(rows, columns, i+1)
plt.title(str(i)) # label
plt.axis('off') # turn off axis
plt.imshow(X_train[np.where(y_train==i)][0], cmap='gray') # gray scale
plt.show()
Figure 14.3: Plot of an MNIST digit from each class
Look at the maximum and the minimum pixel value in the dataset...
print('Maximum pixel value in the training_set: ', np.max(X_train))
print('Minimum pixel value in the training_set: ', np.min(X_train))
We see that the maximum pixel value in the dataset is 255 and the minimum is 0.