python - 使用 numpy reshape 数组 - ValueError : cannot reshape array

标签 python arrays numpy reshape

我有一个 float vec 数组,我想对其进行 reshape

vec.shape
>>> (3,)
len(vec[0]) # all 3 rows of vec have 150 columns
>>> 150
np.reshape(vec, (3,150))
>>> ValueError: cannot reshape array of size 3 into shape (3,150)

但我收到上面的错误。

出了什么问题?我怎样才能解决这个问题?谢谢!

最佳答案

vec.shape 表示数组有 3 个项目。但它们是 dtype 对象,即指向内存中其他位置的项目的指针。

显然这些项目本身就是数组。如果维度匹配,concatenatestack 函数之一可以将它们连接到一个数组中。

我建议打印

[x.shape for x in vec]

验证形状。当然,请确保这些子数组本身不是对象数据类型。

<小时/>
In [261]: vec = np.empty(3, object)
In [262]: vec[:] = [np.arange(10), np.ones(10), np.zeros(10)]
In [263]: vec
Out[263]: 
array([array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
       array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.]),
       array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])], dtype=object)
In [264]: vec.reshape(3,10)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-264-cd555975140c> in <module>()
----> 1 vec.reshape(3,10)

ValueError: cannot reshape array of size 3 into shape (3,10)
In [265]: [x.shape for x in vec]
Out[265]: [(10,), (10,), (10,)]
In [266]: np.stack(vec)
Out[266]: 
array([[ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
In [267]: np.concatenate(vec)
Out[267]: 
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.,  1.,  1.,  1.,
        1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.])
In [268]: np.concatenate(vec).reshape(3,10)
Out[268]: 
array([[ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
<小时/>

len 不是一个好的测试;使用形状。例如,如果我将一个数组更改为 2d

In [269]: vec[1]=np.ones((10,1))
In [270]: vec
Out[270]: 
array([array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
       array([[ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.]]),
       array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])], dtype=object)
In [271]: [len(x) for x in vec]
Out[271]: [10, 10, 10]
In [272]: [x.shape for x in vec]
Out[272]: [(10,), (10, 1), (10,)]
In [273]: np.concatenate(vec)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-273-a253d8b9b25d> in <module>()
----> 1 np.concatenate(vec)

ValueError: all the input arrays must have same number of dimensions

关于python - 使用 numpy reshape 数组 - ValueError : cannot reshape array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47335625/

相关文章:

python sql转义正斜杠不起作用

java - 将 HashMap 中的两组值放入两个数组中,快速而简单

python - 计算两组向量之间叉积的有效方法 numpy

python - Keras ImageDataGenerator 和 flow_from_directory class_mode ='input'

python - 如何在 python 中编写查找和执行命令

c - 如何在输入数组中打印出每个字符的地址和内容

python - 为 2D Numpy 数组逐行插值

python - 预期为二维数组,得到一维数组

python - 如何按空格和行尾字符拆分文件内容?

javascript - 规范化数组方法和返回值