Search notes:

Python matplotlib example: MNIST character grid

The following example displays elems_y rows of elems_x MNIST characters in a grid.
import tensorflow as tf
import matplotlib.pyplot as plt 
import numpy as np 

(x, y), (xt, yt) = tf.keras.datasets.mnist.load_data()

elems_y = 5
elems_x = 8

fig, axes = plt.subplots(elems_y, elems_x, figsize=(elems_x, elems_y))
# fig.tight_layout(pad=0.1)
fig.tight_layout()

imIxs = np.random.choice(range(len(x)), elems_x * elems_y, replace = False)
for ax, imIx in zip(axes.flat, imIxs):

    ax.imshow(x[imIx] , cmap='gray_r')
    ax.set_title(y[imIx], color='red')
 
  #
  # Use following line to also include image number (index)
  #
  # ax.set_title(f'{y[imIx]} ({imIx})', color='red')

    ax.set_axis_off()


plt.savefig('img/MNIST-character-grid.png')
Github repository about-python, path: /libraries/matplotlib/_examples/MNIST-character-grid.py
When executed, the following image is produced:

See also

Displaying a single MNIST digit

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1759217413, '216.73.216.128', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/libraries/matplotlib/_examples/MNIST-character-grid(68): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78