如何显示单MNIST数字,每一个行?

问题描述 投票:0回答:1

我想从0-9,每一列和5个实施例对于每一个数字显示个位数。照片是一个例子:example在我的情况,使10行,5列。

我设法显示第一图像50像这样(遗憾的图像,我无法格式化在计算器的代码):code

如何打印为标签每个数字在一排?我已经尝试了好几个小时,但我不知道,但是使用了numpy.take(),但我不知道怎么办。我已经用Google搜索了很多,没有可用的结果。

提前致谢!

python plot mnist
1个回答
7
投票

首先,你需要的数据集:

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_train您可以使用列车:

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]])

Result

© www.soinside.com 2019 - 2024. All rights reserved.