numpy:ValueError:具有多个元素的数组的真值是不明确的.使用a.any()或a.all() [英] Numpy: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
问题描述
我一直在尝试在Python上使用numpy来绘制一些数据.但是我遇到了一个我不明白的错误:
I've been trying to use numpy on Python to plot some data. However I'm getting an error I don't understand:
ValueError:具有多个元素的数组的真值不明确.使用a.any()或a.all()
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
这是应该导致错误的行(第三行):
And this is the line supposed to cause the error (the third line):
def T(z):
for i in range(3):
if (z <= z_tbl[i+1]):
return T0_tbl[i]+a_tbl[i]*(z-z_tbl[i])
return 0
这些列表只是一些整数列表,而z也是整数
Those lists are just some lists of integers, and z is an integer too
我该如何解决?
推荐答案
z
或z_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
这篇关于numpy:ValueError:具有多个元素的数组的真值是不明确的.使用a.any()或a.all()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!