如何从Keras提供的MNIST数据集中仅选择特定数量的样本?

时间:2017-03-06 04:44:35

标签: python deep-learning keras

我目前正在使用Keras在MNIST数据集上训练卷积神经网络。我使用格式

加载数据集
X_train

但是为了减少迭代所有数据,我想只从{0}的每个0-9类中选择前10000个样本,同样从Y_train中选择。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

MNIST数据集说它返回:

Return:

    2 tuples:
        X_train, X_test: uint8 array of grayscale image data with shape (nb_samples, 28, 28).
        y_train, y_test: uint8 array of digit labels (integers in range 0-9) with shape (nb_samples,).

所以你需要切片你想要保留的部分。我相信pandas / numpy的语法类似于:

X_train = X_train[:10000,:,:]
X_test = X_test[:10000,:,:]
y_train = y_train[:10000]
y_test  = y_test[:10000]

答案 1 :(得分:0)

from keras.datasets import mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train[:1000,:,:]
x_test = x_test[:500,:,:]
y_train = y_train[:1000]
y_test  = y_test[:500]


print(len(x_train))
print(len(y_train))
print(len(x_test))
print(len(y_test))

#输出

> 1000 
> 1000 
> 500 
> 500