import tensorflow as tf import numpy as np import matplotlib.pyplot as plt # Load training dataset of 60000 images with greyscale values in 28 x 28 # and labels (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data() # print the shape of the numpy arrays print ('Print shape of pixel data') print(x_train.shape) print ('Print shape of labels') print(y_train.shape) # normalize pixel to 0-1 x_train = x_train / 255 x_test = x_test / 255 # choose an image num to display and print num = 20 image = x_train[num] label = y_train[num] print ('Print normailzed pixel data of image ',num, ' :') print(x_train[num]) print ('Print label of image ',num , ' :' ) print(y_train[num]) # plot the image using imshow plt.imshow(image, cmap='gray') # set the title plt.title("Label: %d" % label ) # remove the axis labels and ticks plt.axis('off') # show the plot plt.show() # Plot 16 examples from the numpy array which was read in above # and display it fig, axes = plt.subplots(4, 4, figsize=(10, 10)) for i , ax in enumerate(axes.ravel()): ax.imshow(x_train[num+i], cmap='gray') ax.set_title("Label: %d" % y_train[num+i]) ax.axis('off') plt.suptitle("Examples of training set images") plt.show()