【问题标题】:np.array() is not working when i gave a list including two ndarray with different shape当我给出一个包含两个不同形状的 ndarray 的列表时,np.array() 不起作用
【发布时间】:2019-05-22 01:11:28
【问题描述】:

这是一个代码。我做了一个列表,包括两个不同形状的 ndarray。

d = []

a = np.arange(183).reshape(3,61)
b = np.arange(51).reshape(3,17)

d = [a,b]

np.array(d)

错误如下。

 File "C:\Program Files\JetBrains\PyCharm 2019.1.1\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "D:/dev/workspace/rl/test/npcopy.py", line 10, in <module>
    np.array(d)
ValueError: could not broadcast input array from shape (3,61) into shape (3)

np.copy() 在两个 ndarray 的第一个 shpae 不同时起作用。但如果不是,它就不能像上面那样工作。

如果我如下更改此代码,

import numpy as np

d = []

a = np.arange(183).reshape(4, 61)
b = np.arange(51).reshape(3, 17)

d = [a,b]

np.array(d)

有效!!好奇怪!!

【问题讨论】:

  • 为什么不只使用a+b
  • 试试:np.concatenate(d, axis=1)
  • 它的预期。您不能将两个不同维度的矩阵组合成一个 n 维数组
  • 您要寻找什么样的最终形状? 3x78 ?
  • d = [a,b]就够了,为什么np.array(d)呢?

标签: python numpy


【解决方案1】:

由于矩阵的维度不同

> a = np.arange(183).reshape(3,61) b = np.arange(51).reshape(3,17)
> d=[np.array(a),np.array(b)] 
>  print(d) for output
> 
> or  d=[a,b]
>  np.concatenate(d, axis=1)

【讨论】:

    【解决方案2】:

    当您尝试从数组创建数组时,可能会出现三种结果:

    如果数组具有相同的形状,则结果是更高维度的数组:

    In [295]: np.array((np.zeros((2,3),int),np.ones((2,3),int)))                 
    Out[295]: 
    array([[[0, 0, 0],
            [0, 0, 0]],
    
           [[1, 1, 1],
            [1, 1, 1]]])
    In [296]: _.shape                                                            
    Out[296]: (2, 2, 3)
    

    如果数组的形状不同,则结果可能是对象 dtype 数组(类似于 list):

    In [298]: np.array((np.zeros((2,3),int),np.ones((3,3),int)))                 
    Out[298]: 
    array([array([[0, 0, 0],
           [0, 0, 0]]),
           array([[1, 1, 1],
           [1, 1, 1],
           [1, 1, 1]])], dtype=object)    # shape (2,)
    

    但是对于一些形状的组合,结果是错误的:

    In [301]: np.array((np.zeros((2,3),int),np.ones((2,4),int)))                 
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-301-d67e6685581d> in <module>
    ----> 1 np.array((np.zeros((2,3),int),np.ones((2,4),int)))
    
    ValueError: could not broadcast input array from shape (2,3) into shape (2)
    

    在错误情况下,第一个维度匹配,就像在第一个情况下一样。

    有时要创建一个对象数组,您必须从一个“空”数组开始,然后填充它。这比np.array(...) 方法更可靠。

    In [303]: arr = np.empty(2, object)                                          
    In [304]: arr[:] = np.zeros((2,3),int),np.ones((2,4),int)                    
    In [305]: arr                                                                
    Out[305]: 
    array([array([[0, 0, 0],
           [0, 0, 0]]),
           array([[1, 1, 1, 1],
           [1, 1, 1, 1]])], dtype=object)
    In [306]: arr[:] = np.zeros((2,3),int),np.ones((2,3),int)                    
    In [307]: arr                                                                
    Out[307]: 
    array([array([[0, 0, 0],
           [0, 0, 0]]),
           array([[1, 1, 1],
           [1, 1, 1]])], dtype=object)
    

    【讨论】:

    • 感谢您的回答!所以这是一个麻木的错误?你提出了解决方法?
    • 我不会将其描述为错误。正常的、预期的行为是第一种情况。从某种意义上说,其他人是处理不良输入的方法。我希望看到np.array 在这两种情况下都会抛出错误,除非指定了对象 dtype。我建议了制作对象 dtype 数组的最可靠方法。
    【解决方案3】:

    我没有使用 np.copy(),而是选择使用 copy.deepcopy()。即使列表中两个项目的第一个形状相同,它也可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      • 2018-10-29
      • 2021-12-11
      • 2019-11-30
      相关资源
      最近更新 更多