Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple conditions using 'or' in numpy array

Tags:

python

numpy

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?

like image 893
rudster Avatar asked Apr 30 '12 00:04

rudster


People also ask

How can we use conditions in numpy within an array?

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]]) .

Where can I use and in multiple conditions in NP?

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.

How do you select an element in a numpy array?

To select an element from Numpy Array , we can use [] operator i.e. It will return the element at given index only.

What does .all do in numpy?

all() in Python. The numpy. all() function tests whether all array elements along the mentioned axis evaluate to True.


1 Answers

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) 
like image 102
ThiefMaster Avatar answered Sep 22 '22 06:09

ThiefMaster