具有多个元素的数组的真值是不明确的错误?Python [英] The truth value of an array with more than one element is ambiguous error? python
问题描述
from numpy import *
from pylab import *
from math import *
def TentMap(a,x):
if x>= 0 and x<0.5:
return 2.*a*x
elif x>=0.5 and x<=1.:
return 2.*a*(1.-x)
# We set a = 0.98, a typical chaotic value
a = 0.98
N = 1.0
xaxis = arange(0.0,N,0.01)
Func = TentMap
subplot(211)
title(str(Func.func_name) + ' at a=%g and its second iterate' %a)
ylabel('X(n+1)') # set y-axis label
plot(xaxis,Func(a,xaxis), 'g', antialiased=True)
subplot(212)
ylabel('X(n+1)') # set y-axis label
xlabel('X(n)') # set x-axis label
plot(xaxis,Func(a,Func(a,xaxis)), 'bo', antialiased=True)
我的 TentMap
功能无法正常工作.我不断收到错误具有多个元素的数组的真值不明确.使用a.any()或a.all()
我不明白我应该如何使用它们.基本上, TentMap
函数采用值X,并根据X是什么而返回某个值.因此,如果 0<=x<0.5,则返回 2ax,如果 0.5<=x<=1,则返回 2a(1-x).
My TentMap
function isn't working properly. I keep getting the error The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I don't understand how I'm supposed to use those. Basically, the TentMap
function takes a value X and returns a certain value depending on what X is. So if 0<=x<0.5 then it returns 2ax and if 0.5<=x<=1 then it returns 2a(1-x).
推荐答案
您可以使用 np.vectorize
来解决此错误,该错误在将 and
与标量一起使用时发生价值和误入歧途.通话看起来像
You can use np.vectorize
to get around this error which occurs when using and
with a scalar value and arrray. The call looks like
np.vectorize(TentMap)(a,xaxis)
这篇关于具有多个元素的数组的真值是不明确的错误?Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!