首页 > 解决方案 > ValueError:无法将大小为 6744500 的数组重塑为形状 (574,1,64,47)

问题描述

我正在使用来自 sklearn 的 fetch_lfw_people 来识别面部,但是当我 trat to reshape II 得到这个错误 ValueError: cannot reshape array of size 6744500 into shape (574,1,64,47)但是对于我拥有的数据它似乎是正确的参数,是 我知道的文档文档的图像大小 574 *1*64*47 不是 6744500 而是我拥有的数据

lfw_people = fetch_lfw_people(min_faces_per_person=200, resize=1)
X = lfw_people.data
y = lfw_people.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)

X_train = X_train.reshape(X_train.shape[0], 1, 64, 47).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 1, 64, 47).astype('float32')

标签: pythonscikit-learn

解决方案


尝试这个:

获取有关图像大小的信息:

In [46]: print(lfw_people.images.shape)
(766, 125, 94)

即整个数据集有766张图片。每张图片都有形状:(125, 94)

重塑:

In [47]: X_train = X_train.reshape((X_train.shape[0],) + lfw_people.images.shape[1:])

结果:

In [48]: X_train.shape
Out[48]: (574, 125, 94)

推荐阅读