So I have these conditions:
A = 0 to 10 OR 40 to 60
B = 20 to 50
and I have this code:
area1 = N.where((A>0) & (A<10)),1,0) area2 = N.where((B>20) & (B<50)),1,0)
My question is: how do I do 'OR' condition in numpy?
It returns a new numpy array, after filtering based on a condition, which is a numpy-like array of boolean values. For example, if condition is array([[True, True, False]]) , and our array is a = ndarray([[1, 2, 3]]) , on applying a condition to array ( a[:, condition] ), we will get the array ndarray([[1 2]]) .
We can specify multiple conditions inside the numpy. where() function by enclosing each condition inside a pair of parenthesis and using a & operator between them. In the above code, we selected the values from the array of integers values greater than 2 but less than 4 with the np.
To select an element from Numpy Array , we can use [] operator i.e. It will return the element at given index only.
all() in Python. The numpy. all() function tests whether all array elements along the mentioned axis evaluate to True.
If numpy overloads &
for boolean and
you can safely assume that |
is boolean or
.
area1 = N.where(((A>0) & (A<10)) | ((A>40) & (A<60))),1,0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With