display mnist 999 and 1000 image from dataset in python

To display the MNIST images with indices 999 and 1000 from the dataset in Python, you can use the following code:

main.py
from tensorflow.keras.datasets import mnist
import matplotlib.pyplot as plt

# Load the MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Display the 999th image
plt.imshow(x_train[999], cmap='gray')
plt.title('MNIST Image - 999')
plt.axis('off')
plt.show()

# Display the 1000th image
plt.imshow(x_train[1000], cmap='gray')
plt.title('MNIST Image - 1000')
plt.axis('off')
plt.show()
408 chars
18 lines

This code uses the mnist module from TensorFlow to load the MNIST dataset. It then accesses the training images x_train and displays the indexed images using imshow() from matplotlib.pyplot. Finally, it sets the title and turns off the axis for better visualization.

Remember to have the matplotlib and tensorflow libraries installed in your Python environment before running this code.

related categories

gistlibby LogSnag