本文介绍了如何将MNIST数字分类到每个类别标签中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用(x_train, y_train), (x_test, y_test) = mnist.load_data()从Keras导入mnist数据集,我想要做的是按每个样本的对应数字对其进行排序.我正在想办法做到这一点,但我似乎找不到数据的任何标签属性.有任何简单的方法可以做到这一点吗?

I'm importing mnist dataset from Keras using (x_train, y_train), (x_test, y_test) = mnist.load_data() and what I want to do is sort each sample by it's corresponding digit. I'm imagining some trivial way to do this but I can't seem to find any label attribute of the data. Any simple way to do this?

推荐答案

y_trainy_test是包含分别与x_train和x_test中的每个图像关联的标签的向量.这将告诉您每个图像中显示的数字.因此,只需获取将使用np.argsort对这些向量进行排序的索引,然后使用这些索引对相应矩阵进行重新排序即可.

y_train and y_test are the vectors containing the label associated with each image in x_train and x_test respectively. That will tell you the digit shown in each image. So just get the indices that will sort these vectors using np.argsort and then use these indices to re-order the corresponding matrix.

import numpy as np

idx = np.argsort(y_train)
x_train_sorted = x_train[idx]
y_train_sorted = y_train[idx]

因此,如果您希望所有图像都使用一个特定的数字,则只需索引相应的矩阵即可抓取它们

So if you want all the images for a particular digit, you can simply grab them by indexing the corresponding matrix

x_train_zeros = x_train[y_train == 0]
x_train_ones = x_train[y_train == 1]
# and so on...

请注意,在这种情况下,您无需对数据进行预排序.

Notice that in this case you don't need to pre-sort the data.

这篇关于如何将MNIST数字分类到每个类别标签中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:47