我想从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)
答案 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中的切片。