0

New to python and need some help. I have a numpy array tuple with a shape of (1, 8760) with numbers within each of the 8760 locations. I've been trying to calculate if the values is between -2 and 2 then my new variable will be 0 else just keep the same value in the new variable. Here is what I tried and many others but I probably don't understand the array concept fully.

for x in flow:
    if 2 > x < -2:
        lflow = 0
    else:
        lflow = flow

I get this error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

From what I read those functions gives me a true or false but I want to calculate of value instead of telling me if it matches or not. Please help.

Thanks

2
  • 1
    Just to be clear, you want the new value to be 0 if x is in the range (-2, 2)? Because 2 > x < -2 means x is less than -2 and x is less than 2. Commented Jan 4, 2016 at 19:01
  • Maybe I made a typo. If x is between -2 and 2 then I want the new value to be zero or else just keep the value within the array. Within the 8760 are set values outputted from a software so I want to evaluate those numbers and set as zero if necessary. Thanks. Commented Jan 4, 2016 at 20:15

2 Answers 2

1

If your shape is (1,8760) an array of 8760 elements is assigned to x in your iteration, because the loop iterates the first axis containing one element of size 8760. Furthermore I'd suggest to use "where" function instead of a loop:

# create a random array with 100 values in the range [-5,5]
a = numpy.random.random(100)*10 - 5
# return an array with all elements within that range set to 0
print numpy.where((a < -2) | (a > 2), a, 0)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your comment. I'm not sure if random is what I'm looking for as I want to evaluate each of the 8760 and set as zero if its between -2 and 2. I will try out the second line of code you replied with. Thanks.
@New2Python The random() function is just to conveniently create an array of numbers for the shake of demonstration. The second line is what you want. If the condition (a < -2) | (a > 2) is true (translates to a less than -2 or a greater than 2), you get the original value of a, else, you get a 0. However, that means you get a 0 for the range [-2, 2], inclusive. If you want 0 when a < 2 and a > -2, then you need to change the above condition to (a <= -2) | (a >= 2).
@Reti43: yes of course your are right concerning the "&". Thanks!
0

Numpy uses boolean masks to select or assign values to arrays in bulk. For example, given the array

A = np.array([-3,-1,-2,0,1,5,2])

This mask represents all the values in A that are less than -2 or greater than 2

mask = (A < -2) | (A > 2)

Then use it to assign those values to 0

A[mask] = 0

This is much faster than using a regular loop in python, since numpy will perform this operation in c or fortran code

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.