重塑numpy数组引发ValueError

ajsxfq5m  于 2023-03-23  发布在  其他
关注(0)|答案(1)|浏览(371)

我有一个NumPy数组如下:
arr = np.array([np.zeros(s) for s in range(2, 10)])
我想把每个子数组从形状(s)整形为形状(1, s),所以我写了这个:
arr = np.array([np.zeros(s).reshape(1, s) for s in range(2, 10)])
但是,会引发ValueError:
ValueError: could not broadcast input array from shape (2,) into shape (1,)
我该怎么补救呢?

g0czyy6m

g0czyy6m1#

在numpy 1.23中
你的list comprehension会产生大小不同的数组:

In [87]: [np.zeros(s).reshape(1, s) for s in range(2, 10)]
Out[87]: 
[array([[0., 0.]]),
 array([[0., 0., 0.]]),
 array([[0., 0., 0., 0.]]),
 array([[0., 0., 0., 0., 0.]]),
 array([[0., 0., 0., 0., 0., 0.]]),
 array([[0., 0., 0., 0., 0., 0., 0.]]),
 array([[0., 0., 0., 0., 0., 0., 0., 0.]]),
 array([[0., 0., 0., 0., 0., 0., 0., 0., 0.]])]

尝试从生成警告(你看到了吗?)和错误的数组:

In [88]: np.array(_)
C:\Users\paul\AppData\Local\Temp\ipykernel_6648\2978863899.py:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  np.array(_)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[88], line 1
----> 1 np.array(_)

ValueError: could not broadcast input array from shape (2,) into shape (1,)

即使使用object dtype,我们也会得到错误:

In [91]: np.array(_87, dtype=object)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[91], line 1
----> 1 np.array(_87, dtype=object)

ValueError: could not broadcast input array from shape (2,) into shape (1,)

但是如果我们去掉前面的大小为1的形状,我们可以创建一个“不规则”的对象dtype数组:

In [92]: np.array([np.zeros(s).reshape(s) for s in range(2, 10)], object)
Out[92]: 
array([array([0., 0.]), array([0., 0., 0.]), array([0., 0., 0., 0.]),
       array([0., 0., 0., 0., 0.]), array([0., 0., 0., 0., 0., 0.]),
       array([0., 0., 0., 0., 0., 0., 0.]),
       array([0., 0., 0., 0., 0., 0., 0., 0.]),
       array([0., 0., 0., 0., 0., 0., 0., 0., 0.])], dtype=object)

从这些(1,s)形状创建一个对象dtype数组需要一个更间接的构造--创建一个np.empty(n, object)数组,然后用列表填充它。

In [94]: res = np.empty(8,object); res[:]=_87
In [95]: res
Out[95]: 
array([array([[0., 0.]]), array([[0., 0., 0.]]),
       array([[0., 0., 0., 0.]]), array([[0., 0., 0., 0., 0.]]),
       array([[0., 0., 0., 0., 0., 0.]]),
       array([[0., 0., 0., 0., 0., 0., 0.]]),
       array([[0., 0., 0., 0., 0., 0., 0., 0.]]),
       array([[0., 0., 0., 0., 0., 0., 0., 0., 0.]])], dtype=object)

numpy 1.24

在最新版本中,错误消息有所不同,但问题仍然存在:

In [1]: np.__version__
Out[1]: '1.24.2'

In [2]: np.array([np.zeros(s).reshape(1, s) for s in range(2, 10)])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[2], line 1
----> 1 np.array([np.zeros(s).reshape(1, s) for s in range(2, 10)])

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (8, 1) + inhomogeneous part.

和对象dtype:

In [3]: np.array([np.zeros(s).reshape(1, s) for s in range(2, 10)], object)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[3], line 1
----> 1 np.array([np.zeros(s).reshape(1, s) for s in range(2, 10)], object)

ValueError: could not broadcast input array from shape (2,) into shape (1,)

相关问题