I've been trying to use numpy on Python to plot some data. However I'm getting an error I don't understand:
我一直尝试在Python中使用numpy来绘制一些数据。但是我有一个错误,我不明白:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
ValueError:具有多个元素的数组的真值是不明确的。使用a.any()或所有()
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
Those lists are just some lists of integers, and z is an integer too
这些列表只是一些整数的列表,z也是一个整数。
How can i fix it?
我怎样才能修好它?
1 个解决方案
#1
1
Either z
or z_tbl[i+1]
is a numpy array. For numpy arrays, rich comparisons (==
, <=
, >=
, ...) return another (boolean) numpy array.
z或z_tbl[i+1]是一个numpy数组。对于numpy数组,rich比较(==,<=,>=,…)返回另一个(boolean) numpy数组。
bool
on a numpy array will give you the exception that you are seeing:
在numpy数组中bool将会给您一个您正在看到的异常:
>>> 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 is trying to tell you what to do:
Numpy试图告诉你该做什么:
>>> (a == 1).any() # at least one element is true?
True
>>> (a == 1).all() # all of the elements are true?
False
#1
1
Either z
or z_tbl[i+1]
is a numpy array. For numpy arrays, rich comparisons (==
, <=
, >=
, ...) return another (boolean) numpy array.
z或z_tbl[i+1]是一个numpy数组。对于numpy数组,rich比较(==,<=,>=,…)返回另一个(boolean) numpy数组。
bool
on a numpy array will give you the exception that you are seeing:
在numpy数组中bool将会给您一个您正在看到的异常:
>>> 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 is trying to tell you what to do:
Numpy试图告诉你该做什么:
>>> (a == 1).any() # at least one element is true?
True
>>> (a == 1).all() # all of the elements are true?
False