且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

numpy:ValueError:具有多个元素的数组的真值是不明确的.使用a.any()或a.all()

更新时间:2021-11-27 18:02:07

zz_tbl[i+1]是一个numpy数组.对于numpy数组,丰富的比较(==<=>=,...)将返回另一个(布尔型)numpy数组.

Either z or z_tbl[i+1] is a numpy array. For numpy arrays, rich comparisons (==, <=, >=, ...) return another (boolean) numpy array.

bool会给您所看到的异常:

bool on a numpy array will give you the exception that you are seeing:

>>> a = np.arange(10)
>>> a == 1
array([False,  True, False, False, False, False, False, False, False, False], dtype=bool)
>>> bool(a == 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Numpy试图告诉您该怎么做:

Numpy is trying to tell you what to do:

>>> (a == 1).any()  # at least one element is true?
True
>>> (a == 1).all()  # all of the elements are true?
False