如何使用部分数据?

时间:2018-05-13 11:21:36

标签: python-3.x keras dataset mnist

我想从MNIST数据集中随机使用一部分。你能帮我吗?现在输出形状(即Out)是60000,但我想得到大约2000:

import matplotlib.pyplot as plt
from keras.datasets import mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(60000, 784) / 255
x_test = x_test.reshape(10000, 784) / 255

x_train.shape  # Out: (60000, 748)

1 个答案:

答案 0 :(得分:2)

只需一片x_train

new_x_train = x_train[:2000]

如果x_train中的数据是有序的(即1级数字,然后是2级数字等),那么你应该首先对数据进行洗牌,然后对其进行切片:

import numpy as np
indices = np.arange(x_train.shape[0])
np.random.shuffle(indices)
x_train = x_train[indices]

详细了解numpy documentation中的切片。