0

I am trying to work with a combination of arrays and if statements. I am checking if two elements in two different 1-D arrays return True. (I've gotten that far) Then I am trying to pass that True / False into an if statement - but for every result in the array (not all, or any). I'm not sure how to structure the if statements I'm trying to create.

The below code is what I'm to pass into the if statement. When printed alone, it works as I believe it should, returning a boolean array.

l0, l1, l2, l3 are arrays of numbers, I've checked their outputs individually

test = np.array(np.greater_equal(l0,l1))
print(test)

[[False]
 [False]
 [False]
 [False]
 [False]
 [False]
 [False]
 [False]
 [False]
 [False]
 [False]
 [ True]
 [ True]
 [ True]
 [ True]
 [ True]
  . . . . continues

Seen below, when I try to use the output of the array in a normal If statement, I encounter an error citing ambiguity. I am not trying to return true if "all" or "any" of the elements in the array are >= each other, I want to go through one at a time and assign the variables as written below in either case.

test = np.array(np.greater_equal(l0,l1))
if test:
    cu1 = l0 - l1
    cd1 = 0
else:
    cd1 = l1 - l0
    cu1 = 0

Error: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() Any ideas on proper construction in a way I can pass the indexed position into the if statement and perform further calculations with the resulting variable assignments? (below)

full context of what I'm working with if it helps

if np.array(np.greater_equal(l0,l1)):
    cu1 = l0 - l1
    cd1 = 0
else:
    cd1 = l1 - l0
    cu1 = 0

if np.array(np.greater_equal(l1,l2)):
    cu2 = cu1 + l1 - l2
    cd2 = cd1
else:
    cd2 = cd1 + l2 - l1
    cu2 = cu1

if np.array(np.greater_equal(l2,l3)):
    cu = cu2 + l2 - l3
    cd = cd2
else:
    cu = cu2
    cd = cd2 + l3 - l2

if np.array(np.not_equal(np.sum(cu + cd),0)): 
    x = cu / (cu + cd) 
else: 
    x = 0

x = np.array(x)
2
  • Python for loops are just as basic constructs as if/else ones. if is not a iterator, it's a simple true/false switch. Commented Dec 9, 2022 at 6:29
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Dec 9, 2022 at 7:47

1 Answer 1

0

Hello and welcome to the Python community.

Unfortunately, the python builtin if can only check one True or False at the same time, so it is not possible to use the if construct in your way, unless you decide to iterate over each element separately.

Thankfully, your problem is one that occurs frequently and numpy conveniently provides a function for the exact problem: np.where.

Here is an example of how you could use it:

import numpy as np

# Your arrays here; note that it might be convenient to
# convert your input lists to arrays, which allows you
# to simply use the ">=" operator instead of the more 
# verbose np.greater_equal.
l0 = np.array([3, 1, 4, 3, 5, 2])
l1 = np.array([1, 6, 4, 8, 5, 0])

cu1 = np.where(l0 >= l1, l0 - l1, 0)
cd1 = np.where(l0 >= l1, 0, l1 - l0)

which is equivalent to your first (though faulty) example. I've added two np.where statements because you have 2 arrays that you want to calculate in each if. A single out = np.where(conditions, value_if_true, value_if_false) would, in your language, be something like:

if conditions:
    out = value_if_true
else:
    out = value_if_false

Finally, (apologies if this is unsolicited advice), I find that numpy is a step ahead of me a lot of the time. For instance, in your first example, instead of choosing between l0 - l1 and 0 depending on whether l0 is larger than l1, you could instead limit l0 - l1 using a lower bound of 0. Take a look at np.clip, or in your case, np.maximum does the trick:

cu1 = np.maximum(l0 - l1, 0)
cd1 = np.maximum(l1 - l0, 0)
Sign up to request clarification or add additional context in comments.

1 Comment

Beautiful, I didn't think about using np.where() here. Thank you for the help.

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.