Skip to content Skip to sidebar Skip to footer

If Y>0.0 And X -y>=-q1: Valueerror: The Truth Value Of An Array With More Than One Element Is Ambiguous. Use A.any() Or A.all()

I have been trying to get this to work for a while now, but still not finding a way. I am trying to compute the Look ahead estimate density of a piecewise gaussian function. I'm tr

Solution 1:

See all those ValueError questions in the side bar????

This error is produced when a boolean array is used in a scalar boolean context, such as if or or/and.

Try your y or x in this test, or even simpler one. Experiment in a interactive shell.

if y>0.0 and x -y>=-Q1: ....

if y>0: 

(y>0.0) and (x-y>=10)

will all produce this error with your x and y.

Notice also that I edited your question for clarity.

Solution 2:

Error starts with quantecon.LAE(p, X), which expects a vectorized function p. Your function isn't vectorized, which is why everything else doesn't work. You copied some vectorized code, but left a lot of things as sympy style functions which is why the numpy folks were confused about what you wanted.

In this case "vectorized" means transforming two 1D arrays with length n into a 2D n x n array. In this case, you don't want to return 0.0, you want to return out a 2d ndArray which has the value 0.0 in locations out[i,j] where a boolean mask based on a function of x[i], y[j] is false.

You can do this by broadcasting:

def sum_function(x,y):
    return x[:, None] + y[None, :]   # or however you want toadd them, broadcasted to2D

def myFilter(x,y):
    x, y = x.squeeze(), y.squeeze()
    out=np.zeros((x.size,y.size))
    xyDiff = x[:, None] - y[None, :]
    out=np.where(np.bitwise_and(y[None, :] =>0.0, xyDiff >=-Q1), sum_function(x, y), out) # unless the sum functions are different
    out=np.where(np.bitwise_and(y[None, :] <0.0, xyDiff >=-Q2), sum_function(x, y), out)
    returnout

Post a Comment for "If Y>0.0 And X -y>=-q1: Valueerror: The Truth Value Of An Array With More Than One Element Is Ambiguous. Use A.any() Or A.all()"