【问题标题】:numpy.VisibleDeprecationWarning: Creating an ndarray from ragged nested sequencesnumpy.VisibleDeprecationWarning:从参差不齐的嵌套序列创建一个 ndarray
【发布时间】:2021-01-31 15:28:49
【问题描述】:

这是我无法理解的行为示例,也许有人可以分享对其背后逻辑的见解:

ccn = np.ones(1)
bbb = 7
bbn = np.array(bbb)
bbn * ccn # this is OK
    array([7.])
np.prod((bbn,ccn)) # but this is NOT
    Traceback (most recent call last):
    File "C:\Program Files\JetBrains\PyCharm Community Edition 2020.2.2\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<input>", line 1, in <module>
  File "<__array_function__ internals>", line 5, in prod
  File "C:\Users\...\venv\lib\site-packages\numpy\core\fromnumeric.py", line 2999, in prod
    return _wrapreduction(a, np.multiply, 'prod', axis, dtype, out,
  File "C:\Users\...\venv\lib\site-packages\numpy\core\fromnumeric.py", line 87, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
numpy.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

为什么?为什么两个数字的简单相乘会成为问题?就形式代数而言,没有维度问题,没有数据类型问题?结果总是一个数字,它不可能“突然”转动矢量或对象任何类似的东西。 prod(a,b) for ab 是标量或 1by1“矩阵”,MATLAB 或 Octave 不会有任何问题。

我知道我可以关闭这个错误之类的,但为什么它是偶数和错误?

【问题讨论】:

  • 测试np.array((bbn, ccn))。查看 2 个数组的形状,以及结果的 dtype。

标签: numpy numpy-ndarray


【解决方案1】:
In [346]: ccn = np.ones(1)
     ...: bbb = 7
     ...: bbn = np.array(bbb)
In [347]: ccn.shape
Out[347]: (1,)
In [348]: bbn.shape
Out[348]: ()
In [349]: np.array((bbn,ccn))
<ipython-input-349-997419ba7a2f>: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((bbn,ccn))
Out[349]: array([array(7), array([1.])], dtype=object)

你有不同维度的数组,不能组合成一个数值数组。

那个np.prod的表达其实是:

np.multiply.reduce(np.array([bbn,ccn]))

可以从您的回溯中推断出来。

在 Octave 中,两个对象都有形状 (1,1), 2d

>> ccn = ones(1)
ccn =  1
>> ccn = ones(1);
>> size(ccn)
ans =

   1   1

>> bbn = 7;
>> size(bbn)
ans =

   1   1

>> [bbn,ccn]
ans =

   7   1

它没有真正的标量;一切都是 2d 的(甚至 3d 都是最后一维的软糖)。

使用“原始”Python 输入:

In [350]: np.array([1,[1]])
<ipython-input-350-f17372e1b22d>:1: VisibleDeprecationWarning: ...
  np.array([1,[1]])
Out[350]: array([1, list([1])], dtype=object)

object dtype 数组保留输入的类型。

编辑

prod 不是简单的乘法。这是一个归约运算,就像数学中的大 Pi。即使在 Octave 中也不是:

>> prod([[2,3],[3;4]])
error: horizontal dimensions mismatch (1x2 vs 2x1)
>> [2,3]*[3;4]
ans =  18
>> [2,3].*[3;4]
ans =

    6    9
    8   12

numpy 等价物:

In [97]: np.prod((np.array([2,3]),np.array([[3],[4]])))
/usr/local/lib/python3.8/dist-packages/numpy/core/fromnumeric.py:87: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences...
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
ValueError: could not broadcast input array from shape (2,1) into shape (2,)

In [98]: np.array([2,3])@np.array([[3],[4]])
Out[98]: array([18])
In [99]: np.array([2,3])*np.array([[3],[4]])
Out[99]: 
array([[ 6,  9],
       [ 8, 12]])

警告,这里是错误,是通过尝试从(np.array([2,3]),np.array([[3],[4]])) 生成一个数组而产生的。

【讨论】:

  • 哦,我明白了。并不是说我理解为什么像 2 个标量的乘法这样简单的事情(例如,八度音程示例,在 MATLAB 或 C 中也可以工作)必须是 multiply.reduce([x,y]) 这可能有尺寸问题,但是,嘿,Python 是免费,所以不能抱怨。
  • 您发现简单的乘法 bbn * ccn 确实有效。有特殊形状要求的是prod。 Octave 也是如此 - 请参阅我的编辑。
  • np.prod([bbn, ccn])的有用替换有什么建议吗? np.multiply.reduce([bbn, ccn]) 有同样的警告。
  • 对于 Python >= 3.8,math.prod([bbn, ccn]) 似乎可以工作。
猜你喜欢
  • 2021-05-10
  • 2020-11-15
  • 1970-01-01
  • 1970-01-01
  • 2013-08-18
  • 2018-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多