我正在尝试将numpy数组重塑为:
data3 = data3.reshape((data3.shape[0], 28, 28))
其中data3
是:
[[54 68 66 ..., 83 72 58]
[63 63 63 ..., 51 51 51]
[41 45 80 ..., 44 46 81]
...,
[58 60 61 ..., 75 75 81]
[56 58 59 ..., 72 75 80]
[ 4 4 4 ..., 8 8 8]]
data3.shape
是(52, 2352 )
但我一直收到以下错误:
ValueError: cannot reshape array of size 122304 into shape (52,28,28)
Exception TypeError: TypeError("'NoneType' object is not callable",) in <function _remove at 0x10b6477d0> ignored
发生了什么以及如何解决此错误?
更新:
我这样做是为了获得上面使用的data3
:
def image_to_feature_vector(image, size=(28, 28)):
return cv2.resize(image, size).flatten()
data3 = np.array([image_to_feature_vector(cv2.imread(imagePath)) for imagePath in imagePaths])
imagePaths包含数据集中所有图像的路径。我实际上想将data3转换为flat list of 784-dim vectors
,但是
image_to_feature_vector
函数将其转换为3072-dim vector !!
答案 0 :(得分:1)
您可以重新形成numpy矩阵数组,以便在(a x b x c..n)= after(a x b x c..n)之前。即矩阵中的总元素应与之前相同,在您的情况下,您可以将其转换为转换后的数据3 有形状(156,28,28)或简单: -
import numpy as np
data3 = np.arange(122304).reshape(52, 2352 )
data3 = data3.reshape((data3.shape[0]*3, 28, 28))
print(data3.shape)
输出格式为
[[[ 0 1 2 ..., 25 26 27]
[ 28 29 30 ..., 53 54 55]
[ 56 57 58 ..., 81 82 83]
...,
[ 700 701 702 ..., 725 726 727]
[ 728 729 730 ..., 753 754 755]
[ 756 757 758 ..., 781 782 783]]
...,
[122248 122249 122250 ..., 122273 122274 122275]
[122276 122277 122278 ..., 122301 122302 122303]]]
答案 1 :(得分:0)
首先,输入图像的元素数量应与所需特征向量中的元素数量相匹配。
假设满足以上条件,则以下内容应该有效:
# Reading all the images to a one numpy array. Paths of the images are in the imagePaths
data = np.array([np.array(cv2.imread(imagePaths[i])) for i in range(len(imagePaths))])
# This will contain the an array of feature vectors of the images
features = data.flatten().reshape(1, 784)