【发布时间】: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 a 和 b 是标量或 1by1“矩阵”,MATLAB 或 Octave 不会有任何问题。
我知道我可以关闭这个错误之类的,但为什么它是偶数和错误?
【问题讨论】:
-
测试
np.array((bbn, ccn))。查看 2 个数组的形状,以及结果的 dtype。
标签: numpy numpy-ndarray