出现错误:无法将大小为122304的数组重塑为形状(52,28,28)

akr*_*a81 6 python numpy image-processing reshape cv2

我正在尝试将numpy数组重塑为:

data3 = data3.reshape((data3.shape[0], 28, 28))
Run Code Online (Sandbox Code Playgroud)

在哪里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]]
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

发生了什么事以及如何解决此错误?

更新:

我这样做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])  
Run Code Online (Sandbox Code Playgroud)

imagePaths包含数据集中所有图像的路径。我实际上想将data3转换为flat list of 784-dim vectors,但是

image_to_feature_vector 
Run Code Online (Sandbox Code Playgroud)

函数将其转换为3072暗矢量!

Kau*_*yak 6

您可以重塑 numpy 矩阵数组,使 before(axbx c..n) = after(axbx c..n)。即矩阵中的总元素应与以前相同,在您的情况下,您可以转换它,使转换后的 data3 具有形状 (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)
Run Code Online (Sandbox Code Playgroud)

输出的形式

[[[     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]]]
Run Code Online (Sandbox Code Playgroud)