mnist fashion output for 7, 8, 9 images from test dataset in python

To output the images of class 7, 8, and 9 from the Fashion MNIST test dataset in Python, you can use the following code snippet:

main.py
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import fashion_mnist

# Load the Fashion MNIST dataset
(_, _), (X_test, y_test) = fashion_mnist.load_data()

# Get the indices of images with labels 7, 8, and 9
class_indices = np.where(np.isin(y_test, [7, 8, 9]))[0]

# Display the images with labels 7, 8, and 9
plt.figure(figsize=(10, 5))
for i, index in enumerate(class_indices[:3]):
    plt.subplot(1, 3, i+1)
    plt.imshow(X_test[index], cmap='gray')
    plt.title(f"Label: {y_test[index]}")
    plt.axis('off')
plt.tight_layout()
plt.show()
580 chars
20 lines

This code snippet uses the fashion_mnist dataset from the tensorflow.keras.datasets module. It loads the test dataset and retrieves the indices of images with labels 7, 8, and 9 using np.where and np.isin functions. Then, it displays the first 3 images along with their corresponding labels using matplotlib.pyplot.imshow.

Make sure you have the tensorflow and matplotlib libraries installed in your Python environment.

Note that Fashion MNIST is a different dataset from the regular MNIST digits dataset, but the code provided can be easily adapted to the regular MNIST dataset by changing the label values accordingly.

related categories

gistlibby LogSnag