【问题标题】:Reshape array using numpy - ValueError: cannot reshape array使用 numpy 重塑数组 - ValueError:无法重塑数组
【发布时间】:2017-11-16 17:19:39
【问题描述】:

我有一组浮点数 vec,我想对其进行整形

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)

但我得到了上面的错误。

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

【问题讨论】:

  • np.concatenate(vec).reshape(3,150)?
  • vec.dtype 产生dtype('O')
  • type(vec[0]) 是什么?
  • @B.M. numpy.ndarray

标签: python arrays numpy reshape


【解决方案1】:

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

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

我建议打印

[x.shape for x in vec]

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


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 不是一个好的测试;使用shape。例如,如果我将一个数组更改为 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-13
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 2016-02-10
    • 2016-05-22
    相关资源
    最近更新 更多