joseph tannoury
joseph tannoury

Reputation: 31

i have an error of a.any() and a.all() value error in the if statement

i am getting an error File

"C:/Users/user/tensorEnv/project.py", line 28, in <module>
    if N <= 100 :
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

can someone tell me how to use a.any() or a.all() or help me fix this issue

import matplotlib.pyplot as plt
import numpy as np
r3=np.arange(1.11,10,0.1)
r1=1
r2=1.1
k=5
R1cond=3.36*10**(-3)
R2cond=(1/k)*(0.0727-(0.08/r3))
Rconv=(0.004)/(r3**2)
Rtotal=Rconv+R1cond+R2cond
qloss=975/Rtotal
t=176
Q=qloss*t
L0=[]
L0.append(Q)
L0=[x*10**(-3) for x in L0]
#E is the price of EDL in LBP/NW.h
#Eel is the electric price to pay
#t is the time of working hours of the furnace 22 business days
#E=Eel*qloss*t
L=[]
L1=[]
L2=[]
L3=[]
L4=[]
for N in L0:
        if N <= 100 :
                L.append(35*N)
        if N <= 300 and N > 100:
                L1.append( 55*N)
        if N <=400 and N > 300:
                L2.append(80*N)
        if N <= 500 and N > 400:
                L3.append(120*N)
        if N > 500 :
                L4.append(200*N)
plt.plot(L)
plt.plot(L1)
plt.plot(L2)
plt.plot(L3)
plt.plot(L4)
plt.show()

Upvotes: 0

Views: 751

Answers (1)

Jason K Lai
Jason K Lai

Reputation: 1540

As the code is written, the variable N stores each item of L0 for every iteration. It looks like L0 is a list containing Q based on the code preceding the loop. This is due to the r3 = np.arange() line.

One of the first things I would do is confirm that things are working as expected. For example, adding a simple print(N) inside the loop to confirm that N truly is an array of floating values (i.e. Q):

for N in L0:
    print( N )
    ...

Assuming all this is expected, it is not syntactically correct to compare an array to a single value, which is why the error suggested using either the .all() or .any() functions. This would have a syntax like the following if we are checking if any of the elements in N (and therefore Q) is greater than or equal to 100:

if ( N <= 100 ).any():
    ...

And the following if we want to check if all the elements in N is greater than or equal to 100:

if ( N <= 100 ).all():
    ...

Upvotes: 1

Related Questions