简体   繁体   English

具有多个元素的数组的真值是模棱两可的错误? 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)  

My TentMap function isn't working properly.我的TentMap功能无法正常工作。 I keep getting the error The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()我不断收到错误The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 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. The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()我不明白我应该如何使用它们。 Basically, the TentMap function takes a value X and returns a certain value depending on what X is.基本上, TentMap函数接受一个值 X 并根据 X 是什么返回某个值。 So if 0<=x<0.5 then it returns 2 a x and if 0.5<=x<=1 then it returns 2 a (1-x).因此,如果 0<=x<0.5 则返回 2 a x,如果 0.5<=x<=1 则返回 2 a (1-x)。

If you compare a numpy array with a number, you'll get another array:如果您将一个 numpy 数组与一个数字进行比较,您将得到另一个数组:

>>> from numpy import arange
>>> xaxis = arange(0.0, 0.04, 0.01)
>>> xaxis
array([ 0.  ,  0.01,  0.02,  0.03])
>>> xaxis <= .02
array([ True,  True,  True, False], dtype=bool)

The problem starts when you want to and this with something else, or use it in a boolean context:当您想要将它and其他东西一起使用,或者在布尔上下文中使用它时,问题就开始了:

>>> xaxis <= .02 and True
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()

>>> bool(xaxis <= .02)
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()

And that's what you're trying to do in the constructor of your TentMap .这就是您在TentMap的构造函数中尝试做的事情。 Are you sure you don't need to use a where you're using x ?您确定不需要在使用x a地方使用 a 吗?

You can use np.vectorize to get around this error which occurs when using and with a scalar value and arrray.您可以使用np.vectorize来解决在使用标量值and arrray 时发生的错误。 The call looks like电话看起来像

np.vectorize(TentMap)(a,xaxis)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有多个元素的数组的真值是模棱两可的Python错误 - The truth value of an array with more than one element is ambiguous Python error “具有多个元素的数组的真值不明确”错误 [Python] - "The truth value of an array with more than one element is ambiguous" error [Python] 值错误:具有多个元素的数组的真值不明确 - Value error: The truth value of an array with more than one element is ambiguous python 中的“具有多个元素的数组的真值不明确” - “The truth value of an array with more than one element is ambiguous” in python 具有多个元素的数组的真值是模棱两可的错误? - The truth value of an array with more than one element is ambiguous error? 错误:具有多个元素的数组的真值不明确 - Error: The truth value of an array with more than one element is ambiguous 错误:具有多个元素的数组的真值不明确 - Error :The truth value of an array with more than one element is ambiguous Tensorflow 错误:一个元素以上的数组的真值不明确 - Tensorflow error: The truth value of an array with more than one element is ambiguous Python ValueError:具有多个元素的数组的真值不明确。 似乎没有答案适用于我的错误 - Python ValueError: The truth value of an array with more than one element is ambiguous. No answer seems to apply to my error Python 错误:包含多个元素的数组的真值不明确。 使用 a.any() 或 a.all() - Python Error : the truth value of an array with more than one element is ambiguous. use a.any() or a.all()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM