我需要创建两个数据集,其中一个数据集包含类别0到4,另一个数据集包含类别5到9的CIFAR10
数据集,但我在尝试时遇到了这个错误:"boolean index did not match indexed array along dimension 1; dimension is 32 but corresponding boolean dimension is 1"
这是我到目前为止尝试过的方法
import keras from keras.datasets import cifar10 (x_train, y_train), (x_test, y_test) = cifar10.load_data() print('x_train shape:', x_train.shape) x_train shape: (50000, 32, 32, 3) 在此处遇到错误 x_train = x_train[y_train < 5]
回答:
打印出y_train.shape得到(50000, 1)。为了正确地使用y_train来索引x_train的第一维度,你需要去掉y_train的第二维度。
x_train = x_train[y_train[:, 0] < 5]
[:, 0]
表示返回第一维度的所有元素,但仅返回第二维度的第一个元素。
现在x_train.shape
得到(25000, 32, 32, 3)。