Python numpy:停止numpy.array()尝试协调元素。从列表创建ndarry,而不尝试合并/协调元素

Python numpy:停止numpy.array()尝试协调元素。从列表创建ndarry,而不尝试合并/协调元素,python,numpy,numpy-ndarray,array-broadcasting,Python,Numpy,Numpy Ndarray,Array Broadcasting,列表中有两个2d矩阵,我想将其转换为numpy数组。下面是三个示例a、b、c >>将numpy作为np导入 >>>a=[np.zero((3,5)),np.zero((2,9))] >>>np.数组(a) >>>数组([数组([[0,0,0,0,0,0.]), [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]]), 数组([[0,0,0,0,0,0,0,0,0,0,0.]), [0,0,0,0,0,0,0,0,0,0,0.]]),数据类型=对象) >>>b=[

列表中有两个2d矩阵,我想将其转换为numpy数组。下面是三个示例a、b、c

>>将numpy作为np导入
>>>a=[np.zero((3,5)),np.zero((2,9))]
>>>np.数组(a)
>>>数组([数组([[0,0,0,0,0,0.]),
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]]),
数组([[0,0,0,0,0,0,0,0,0,0,0.]),
[0,0,0,0,0,0,0,0,0,0,0.]]),数据类型=对象)
>>>b=[np.零((3,5)),np.零((3,9))]
np.数组(b)
回溯(最近一次呼叫最后一次):
文件“C:\Program Files\JetBrains\PyCharm 2019.2.4\helpers\pydev\\u pydevd\u bundle\pydevd\u exec.py”,第3行,在exec中
全局变量、本地变量中的exec exp
文件“”,第1行,在
ValueError:无法将输入数组从形状(3,5)广播到形状(3)
>>>c=[np.零((3,5)),np.零((4,9))]
np.数组(c)
数组([数组([[0,0,0,0,0,0.]),
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]]),
数组([[0,0,0,0,0,0,0,0,0,0,0.]),
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0,0,0,0,0,0,0,0,0,0,0.]]),数据类型=对象)
正如人们可以观察到的那样,a&c起作用,但b不起作用b确实引发异常。不同之处在于,在示例b中,两个矩阵的第一维度匹配

我发现了以下情况,这解释了为什么会发生这种行为

如果只有第一个维度不匹配,则数组仍然匹配,但作为单个对象,不会尝试将它们协调到新的(四维)数组中


我的问题是:我不想让numpy协调矩阵。我只想要第一维度不匹配时的行为。我希望它们被匹配为独立对象,即使它们具有相同的第一维度。如何实现这一点?

Numpy仍然会抱怨,即使您显式地将
对象作为数据类型传递:

>>> np.array(b, dtype=object)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not broadcast input array from shape (3,5) into shape (3)
为了好玩,您可以使用实际的
np.ndarray
类型构造函数,尽管这并不容易:

>>> np.ndarray(dtype=object, shape=len(b), buffer=np.array(list(map(id, b)),dtype=np.uint64))
array([array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]]),
       array([[0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0.]])], dtype=object)

请注意,这依赖于CPython实现细节,
id
只是python对象的地址。因此,我主要是为了好玩而展示它。

Numpy仍然会抱怨,即使您显式地将
对象作为数据类型传递:

>>> np.array(b, dtype=object)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not broadcast input array from shape (3,5) into shape (3)
为了好玩,您可以使用实际的
np.ndarray
类型构造函数,尽管这并不容易:

>>> np.ndarray(dtype=object, shape=len(b), buffer=np.array(list(map(id, b)),dtype=np.uint64))
array([array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]]),
       array([[0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0.]])], dtype=object)

请注意,这依赖于CPython实现细节,
id
只是python对象的地址。因此,我主要只是为了好玩而展示它。

在最新版本中,我们开始看到一个警告:

In [185]: np.__version__                                                                             
Out[185]: '1.19.0'
                                                
In [187]: np.array([np.zeros((3,5)), np.zeros((2,9))])                                               
/usr/local/bin/ipython3: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
  #!/usr/bin/python3
Out[187]: 
array([array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]]),
       array([[0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0.]])], dtype=object)
它仍然会生成对象数据类型数组。在匹配的一维情况下,我们得到警告和错误

In [188]: np.array([np.zeros((3,5)), np.zeros((3,9))])                                               
/usr/local/bin/ipython3: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
  #!/usr/bin/python3
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-188-b6a4475774d0> in <module>
----> 1 np.array([np.zeros((3,5)), np.zeros((3,9))])

ValueError: could not broadcast input array from shape (3,5) into shape (3)

在最新版本中,我们开始看到警告:

In [185]: np.__version__                                                                             
Out[185]: '1.19.0'
                                                
In [187]: np.array([np.zeros((3,5)), np.zeros((2,9))])                                               
/usr/local/bin/ipython3: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
  #!/usr/bin/python3
Out[187]: 
array([array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]]),
       array([[0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0.]])], dtype=object)
它仍然会生成对象数据类型数组。在匹配的一维情况下,我们得到警告和错误

In [188]: np.array([np.zeros((3,5)), np.zeros((3,9))])                                               
/usr/local/bin/ipython3: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
  #!/usr/bin/python3
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-188-b6a4475774d0> in <module>
----> 1 np.array([np.zeros((3,5)), np.zeros((3,9))])

ValueError: could not broadcast input array from shape (3,5) into shape (3)

为什么需要numpy.array对象的numpy数组?为什么不直接使用列表?@juanpa.arrivillaga,因为这样我就可以对a:matric+=1中的矩阵执行:
a+=1
而不是
,为什么需要numpy.array对象的numpy数组?为什么不直接使用列表?@juanpa.arrivillaga,因为这样我就可以做:
a+=1
而不是a:matric+=1