-1

I have two arrays. Both are 1D. However, I am getting the following Value Error. Below is what I tried.

R=np.arange(30,50,1)
T=np.arange(70,90,1)

H=[]

if (T > 8) and (R>10): 
   H.append(0.5 * (T + 61. + (T - 68.) * 1.2 + R * 0.094))
else:
   H.append(0 * 2)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
3

3 Answers 3

0

Simple as that.

import numpy as np
R=np.arange(30,50,1)
T=np.arange(70,90,1)

H=[]

if (T > 8).all() and (R>10).all():
   H.append(0.5 * (T + 61. + (T - 68.) * 1.2 + R * 0.094))
else:
   H.append(0 * 2)

print(H)

Here is an explanation.

If you really just want to know if atleast one element falls under the condition you should use any().

Sign up to request clarification or add additional context in comments.

Comments

0

Python struggled to compare the values in the lists T to number.
The error is raised on T > 8.

For Lists

  1. Use all() to check if all items in the list are above value:
all(x > value for x in list)
  1. Use any() to check if any item in the list are above value:
any(x > value for x in list)

For NumPy Array

  1. When having NumPy array, this can be shortened to:
T = np.array([1,2,3])
all(T > 8)

Fix

Fix to all:

H=[]

import numpy as np

T = np.array([30,34,56])
R = np.array([29,500,43])

if all(T > 8) and all(R > 10):
    H.append(0.5 * (T + 61. + (T - 68.) * 1.2 + R * 0.094))
else:
    H.append(0 * 2)

4 Comments

with numpy this can be shortened to just (T > 8).all(), etc.
I checked both T and R using all(T > 80 for T in T) and all (T > 80 for T in T). I got False for both cases.
Then what you are looking for might by .any()
@Chane use: all(T > 8) and all(R > 10) to check all values or any(T > 8) and any(R > 10) to check if any of the values is above threshold
0

Like the above answer states, you can use all or any. The fix using any is:

if any(t > value for t in T) and any(h > value for h in H):
   H.append(0.5 * (T + 61. + (T - 68.) * 1.2 + R * 0.094))
else:
   H.append(0 * 2)

The python interpreter needs 1 value at a time so that it can compare.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.