包含多个元素的数组的真值是不明确的。使用a.any()或a.all()
我得到了这段代码的上述错误。几个月前我没有犯过这样的错误。但我现在明白了。我怎么才能解决这个问题?
t = np.linspace(np.zeros((2, 2)), np.ones((2, 2)), 20)---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-19-187bbe847597> in <module>
----> 1 t = np.linspace(np.zeros((2, 2)), np.ones((2, 2)), 20)
~\Anaconda3\lib\site-packages\numpy\core\function_base.py in linspace(start, stop, num, endpoint, retstep, dtype)
122 if num > 1:
123 step = delta / div
--> 124 if step == 0:
125 # Special handling for denormal numbers, gh-5437
126 y /= div
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()发布于 2019-07-30 10:38:37
我可以在1.16之前的numpy版本中复制此错误。在此之前,np.linspace不接受stop和start的ndarray输入。
@JHY确认当前版本为1.14.5。尝试更新numpy
发布于 2019-07-30 10:27:51
仅仅因为np.zeros((2,2))和np.ones((2,2))创建了4个值的数组,并且您试图用20示例创建一个数组。
t = np.linspace(np.zeros((2, 2)), np.ones((2, 2)), 1)
不会产生问题,仅仅是因为数组的长度。
结果:
array([[0., 0.], [0., 0.]])
发布于 2019-07-30 10:57:52
更新你的小矮子。
for python 2
pip install numpy --upgrade --user也需要更新表
pip install tables --upgradefor python 3
pip3 install numpy --upgrade --user类似地,对于python 3
pip3 install tables --upgrade要完成numpy更新后的工作,您需要将表更新到。在运行您的代码之后,错误将被删除。
https://stackoverflow.com/questions/57269217
复制相似问题