np.logical_or() in Numpy – Python
Question:
Please anyone can explain me this resulting code
x = np.array([9, 5])
y = np.array([16, 12])
np.logical_or(x < 5, y > 15)
Result -> array([ True, False])
Since:
-
np.logical_or(x < 5, y > 15)
-
x < 5 = [False, False], y > 15 = [True, False]
I think it should be:
[False or False, False or True] = [False, True]
In fact Python giving the result like this:
[False or True, False or False] = [True, False]
Why the result is not [False, True], but in fact python giving the result [True, False]?
It is not match with the order of operation np.logical_or(x < 5, y > 15)
Even I tried ChatGPT explanation I still don’t have clear understanding of this concept.
Please I would be appreciate, if anyone can explain the background of python process in detail step by step.
Answers:
You are interpreting the result wrong. Numpy’s logical_or
can have a few arguments as filters. In your case, the input arguments are x < 5
and y > 15
. It then combines them together with an or
operator.
- The first argument
x < 5
would lead to: [False, False]
- The second argument
y > 15
would lead to: [True, False]
- The final result =
[False or True, False or False] = [True, False]
Please anyone can explain me this resulting code
x = np.array([9, 5])
y = np.array([16, 12])
np.logical_or(x < 5, y > 15)
Result -> array([ True, False])
Since:
-
np.logical_or(x < 5, y > 15)
-
x < 5 = [False, False], y > 15 = [True, False]
I think it should be:
[False or False, False or True] = [False, True]
In fact Python giving the result like this:
[False or True, False or False] = [True, False]
Why the result is not [False, True], but in fact python giving the result [True, False]?
It is not match with the order of operation np.logical_or(x < 5, y > 15)
Even I tried ChatGPT explanation I still don’t have clear understanding of this concept.
Please I would be appreciate, if anyone can explain the background of python process in detail step by step.
You are interpreting the result wrong. Numpy’s logical_or
can have a few arguments as filters. In your case, the input arguments are x < 5
and y > 15
. It then combines them together with an or
operator.
- The first argument
x < 5
would lead to:[False, False]
- The second argument
y > 15
would lead to:[True, False]
- The final result =
[False or True, False or False] = [True, False]