Python 如何在一行中显示单个MNIST数字?

Python 如何在一行中显示单个MNIST数字?,python,plot,mnist,Python,Plot,Mnist,我想显示0-9之间的单个数字,每行显示一个数字,每个数字显示5个示例。这张照片就是一个例子:在我的例子中,它有10行,5列 我成功地显示了前50幅图像,例如:非常抱歉,我无法在stackoverflow设置代码格式: 如何将每个数字的标签打印在一行中? 我已经试了几个小时了,但除了使用numpy.take外,我不知道该怎么做。我已经在谷歌上搜索了很多,但都没有可用的结果 提前谢谢 首先需要数据集: dataset = keras.datasets.mnist.load_data() 然后将其拆

我想显示0-9之间的单个数字,每行显示一个数字,每个数字显示5个示例。这张照片就是一个例子:在我的例子中,它有10行,5列

我成功地显示了前50幅图像,例如:非常抱歉,我无法在stackoverflow设置代码格式:

如何将每个数字的标签打印在一行中? 我已经试了几个小时了,但除了使用numpy.take外,我不知道该怎么做。我已经在谷歌上搜索了很多,但都没有可用的结果


提前谢谢

首先需要数据集:

dataset = keras.datasets.mnist.load_data()
然后将其拆分:

X_train = dataset[0][0]
y_train = dataset[0][1]
X_test = dataset[1][0]
y_test = dataset[1][1]
然后您可以创建一个数字索引字典

这里我使用测试数据集。如果需要,您可以使用火车,只需更换y_火车:

digits = {}

for i in range(10):
    digits[i] = np.where(y_test==i)[0][:5]

digits
这条格言将如下所示:

{0: array([ 3, 10, 13, 25, 28], dtype=int64),
 1: array([ 2,  5, 14, 29, 31], dtype=int64),
 2: array([ 1, 35, 38, 43, 47], dtype=int64),
 3: array([18, 30, 32, 44, 51], dtype=int64),
 4: array([ 4,  6, 19, 24, 27], dtype=int64),
 5: array([ 8, 15, 23, 45, 52], dtype=int64),
 6: array([11, 21, 22, 50, 54], dtype=int64),
 7: array([ 0, 17, 26, 34, 36], dtype=int64),
 8: array([ 61,  84, 110, 128, 134], dtype=int64),
 9: array([ 7,  9, 12, 16, 20], dtype=int64)}
import matplotlib.pyplot as plt
fig, ax = plt.subplots(10, 5, sharex='col', sharey='row')
for i in range(10):
    for j in range(5):
        ax[i, j].imshow(X_test[digits[i][j]])
最后创建一个图形和子图,如下所示:

{0: array([ 3, 10, 13, 25, 28], dtype=int64),
 1: array([ 2,  5, 14, 29, 31], dtype=int64),
 2: array([ 1, 35, 38, 43, 47], dtype=int64),
 3: array([18, 30, 32, 44, 51], dtype=int64),
 4: array([ 4,  6, 19, 24, 27], dtype=int64),
 5: array([ 8, 15, 23, 45, 52], dtype=int64),
 6: array([11, 21, 22, 50, 54], dtype=int64),
 7: array([ 0, 17, 26, 34, 36], dtype=int64),
 8: array([ 61,  84, 110, 128, 134], dtype=int64),
 9: array([ 7,  9, 12, 16, 20], dtype=int64)}
import matplotlib.pyplot as plt
fig, ax = plt.subplots(10, 5, sharex='col', sharey='row')
for i in range(10):
    for j in range(5):
        ax[i, j].imshow(X_test[digits[i][j]])