Python 包含多个元素的数组的真值是不明确的。使用 a.any() 或 a.all()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31126893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
提问by JuanGiraldo
although I know there's various questions looking for solutions to this error message, I've yet to find an answer that helps me solve my code to get the comparison working, I have the code
虽然我知道有各种问题在寻找此错误消息的解决方案,但我还没有找到可以帮助我解决代码以使比较正常工作的答案,我有代码
def f(x,d,h,L):
ans=0.
if ((0.<=x) & (x<d)):
ans=h*(x/d)
elif ((d<=x) & (x<=L)):
ans=((L-x)/(L-d))
return ans
x=np.linspace(0,10,1000)
h=5*10**(-3)
d=16*10**(-2)
L=64.52*10**(-2)
func=f(x,d,h,L)
But when I try running it I get an error pointing to the if
line with the error code in the title, I've tried the proposed solutions given in similar questions such as using np.logical_and
or and
instead of &
but all three yield the same error, please help me out
但是,当我尝试运行它时,我收到一个错误,指向if
标题中带有错误代码的行,我尝试了类似问题中给出的建议解决方案,例如使用np.logical_and
或and
代替,&
但所有三个都会产生相同的错误,请帮助我出去
回答by Ami Tavory
回答by Dunes
The error is related to the fact that an array contains more than value. For instance a < 0
where a = 1
has a definitive truth value (false). However, what if a is an array. eg [-1, 0, 1]
, some elements are less than zero and some are greater than or equal to zero. So what what should the truth value be? To be able to create a truth value you have to specify if you want allthe values to be less than zero, or for at least one value to be less than zero (anyvalue).
该错误与数组包含多个值的事实有关。例如a < 0
wherea = 1
有一个确定的真值(false)。但是,如果 a 是一个数组呢?例如[-1, 0, 1]
,有些元素小于零,有些元素大于或等于零。那么真值应该是什么?为了能够创建真值,您必须指定是希望所有值都小于零,还是至少有一个值小于零(任何值)。
Since mathematical operators on numpy arrays return arrays themselves we can call all
or any
on those arrays to see if all or at least one value is truthful. You would rewrite your if statement as:
由于 numpy 数组上的数学运算符本身返回数组,我们可以在这些数组上调用all
或any
以查看是否所有或至少一个值是真实的。您将 if 语句重写为:
if (0 <= x).all() and (x < d).all():
...
# alternatively
if 0 <= x.min() and x.max() < d:
...
回答by hpaulj
Others have answered assuming that you want to apply one calculation or other depending on whether all/any values of x
meet the respective conditions. I'll make a different assumption - that you want to apply f
to each element of x
individually.
其他人已经回答假设您要根据所有/任何值是否x
满足相应条件来应用一种计算或其他计算。我会做一个不同的假设——你想单独应用f
到每个元素x
。
Applied element by element I get:
逐个元素应用我得到:
In [226]: x=np.linspace(0,1,20)
In [227]: [f(z,d,h,L) for z in x]
Out[227]:
[0.0,
0.0016447368421052631,
0.0032894736842105261,
0.0049342105263157892,
0.89586497157981526,
0.78739098364212268,
0.6789169957044302,
0.57044300776673762,
0.46196901982904509,
0.35349503189135251,
0.24502104395365998,
0.13654705601596731,
0.028073068078274897,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0]
The vectorized equivalent:
矢量化等价物:
In [238]: I = (0<=x) & (x<d)
In [239]: J=(d<=x) & (x<=L)
In [240]: out=np.zeros_like(x)
In [241]: out[I]=h*(x[I]/d)
In [242]: out[J]=(L-x[J])/(L-d)
In [243]: out
Out[243]:
array([ 0. , 0.00164474, 0.00328947, 0.00493421, 0.89586497,
0.78739098, 0.678917 , 0.57044301, 0.46196902, 0.35349503,
0.24502104, 0.13654706, 0.02807307, 0. , 0. ,
0. , 0. , 0. , 0. , 0. ])
I'll let you package that as a function.
我会让你把它打包成一个函数。
With the parameters as given (including the full x
), np.all(I)
and np.all(J)
both are False
, meaning f
would return 0.0
if applied to x
as a whole.
对于给定的参数(包括 full x
),np.all(I)
并且np.all(J)
两者都是False
,如果将其作为一个整体应用,则含义f
将返回。0.0
x
def f(x, d, h, L):
I = (0<=x) & (x<d)
J=(d<=x) & (x<=L)
out=np.zeros_like(x)
out[I]=h*(x[I]/d)
out[J]=(L-x[J])/(L-d)
return out
回答by Pedro López-Adeva
Use numpy.where. Optionally, use exponential notation for floating point numbers.
使用 numpy.where。(可选)对浮点数使用指数表示法。
import numpy as np
def f(x, d, h, L):
return np.where(x < d, h*(x/d), (L - x)/(L - d))
x = np.linspace(0,10,1000)
h = 5e-3
d = 16e-2
L = 64.52e-2
func = f(x, d, h, L)
回答by JuanGiraldo
I was able to solve my problem by defining x
as an array an creating a cycle for evaluating every x
individually, not sure if it's the most efficient way of doing it but I'm only working with 1000 iterations so it works well, here's what I did:
我能够通过定义x
一个数组来解决我的问题,并创建一个循环来评估每个x
人,不确定这是否是最有效的方法,但我只使用 1000 次迭代,所以它运行良好,这就是我所做的:
def f(a,d,h,L):
ans2=[]
for i in range(1000):
if (0.<=a[i]) & (a[i]<d):
ans=x[i]*(h/d)
ans2.append(ans)
elif ((d<=a[i]) & (a[i]<=L)):
ans=h*((L-a[i])/(L-d))
ans2.append(ans)
return ans2
L=64.52*10**(-2)
x=np.linspace(0,L,1000)
h=5*10**(-3)
d=16*10**(-2)
plot.plot(x,f(x,d,h,L))
Hope it solves somebody else's problem as well, and if it can be optimized to have be faster, I'd love to learn how.
希望它也能解决其他人的问题,如果它可以优化为更快,我很想学习如何。
回答by Romain
def f(x,d,h,L):
ans=0.
if ((0.<=x) & (x<d)):
ans=h*(x/d)
elif ((d<=x) & (x<=L)):
ans=((L-x)/(L-d))
return ans
#A ajouter
f_vec = numpy.vectorize(f)
#et c'est tout^^
x=np.linspace(0,10,1000)
h=5*10**(-3)
d=16*10**(-2)
L=64.52*10**(-2)
func=f_vec(x,d,h,L) #ici il faut tout de même ajouter _vec