Numpy.VisibleDeproationWarning:从不整齐的嵌套序列创建ndarray [英] numpy.VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences
本文介绍了Numpy.VisibleDeproationWarning:从不整齐的嵌套序列创建ndarray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里有一个我无法理解的行为例子,也许有人可以分享一下它背后的逻辑:
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 FilesJetBrainsPyCharm Community Edition 2020.2.2pluginspython-cehelperspydev\_pydevd_bundlepydevd_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...venvlibsite-packages
umpycorefromnumeric.py", line 2999, in prod
return _wrapreduction(a, np.multiply, 'prod', axis, dtype, out,
File "C:Users...venvlibsite-packages
umpycorefromnumeric.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)
对于a
和b
是标量或1x1;矩阵;这是MatLab或Octave不会遇到的问题。
我知道我可以关闭此错误等,但为什么它是偶数和错误?
推荐答案
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)
您有不同维度的数组,不能合并为一个数值数组。
<2-3]>表达式实际上是:np.multiply.reduce(np.array([bbn,ccn]))
可以从您的回溯中推断。
在八度音阶中,两个对象都有形状(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)
对象dtype数组保留输入的类型。
编辑
prod
不是简单的乘法。这是一个减法运算,就像数学中的大圆周率。即使在八度音阶中也不是:
>> 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]]))
生成一个数组时生成的。
这篇关于Numpy.VisibleDeproationWarning:从不整齐的嵌套序列创建ndarray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文