Python Numpy : Select elements or indices by conditions from Numpy Array
In this article we will discuss how to select elements or indices from a Numpy array based on multiple conditions.
Similar to arithmetic operations when we apply any comparison operator to Numpy Array, then it will be applied to each element in the array and a new bool Numpy Array will be created with values True or False.
Suppose we have a Numpy Array i.e.
1 2 
#Create an Numpy Array containing elements from 5 to 30 but at equal interval of 2 arr = np.arange(5, 30, 2) 
It’s contents are,
1 
[ 5 7 9 11 13 15 17 19 21 23 25 27 29] 
Let’s select elements from it.
Select elements from a Numpy array based on Single or Multiple Conditions
Let’s apply < operator on above created numpy array i.e.
1 2 
# Comparison Operator will be applied to all elements in array boolArr = arr < 10 
Comparison Operator will be applied to each element in array and number of elements in returned bool Numpy Array will be same as original Numpy Array. But but for every element that satisfies the condition there will be True in array and False for Others in the returned array.
contents of boolArr are,
1 
[ True True True False False False False False False False False False False] 
If we pass this bool Numpy Array to subscript operator [] of original array then it will returns a new Numpy Array containing elements from Original array for which there was True in bool Numpy Array i.e.
1 2 
# Select elements with True at corresponding value in bool array newArr = arr[boolArr] 
We can do all that in a single line by passing complete comparing expression in [] operator i.e.
1 
newArr = arr[arr < 10] 
Just like above, it will return a new Numpy Array with elements < 10 from original Numpy Array i.e.
1 
[5 7 9] 
Let’s checkout some other examples,
Select elements from Numpy Array which are divisible by 3 :
Contents of Numpy Array arr,
1 
[ 5 7 9 11 13 15 17 19 21 23 25 27 29] 
Now lets’ select elements from this Numpy array which are divisible by 3 i.e.
1 
newArr = arr[arr%3==0] 
Contents of Numpy array newArr are,
1 
[ 9 15 21 27] 
Select elements from Numpy Array which are greater than 5 and less than 20:
Here we need to check two conditions i.e. element > 5 and element < 20. But python keywords and , or doesn’t works with bool Numpy Arrays. Instead of it we should use & ,  operators i.e.
1 2 
#Select elements from Numpy Array which are greater than 5 and less than 20 newArr = arr[(arr > 5) & (arr < 20)] 
arr > 5 returns a bool numpy array and arr < 20 returns an another bool numpy array. Now applying & operator on both the bool Numpy Arrays will generate a new bool array newArr.
Contents of Numpy array newArr are,
1 
[ 7 9 11 13 15 17 19] 
Complete example is as follows,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 
import numpy as np def main(): print('Select elements from Numpy Array based on conditions') #Create an Numpy Array containing elements from 5 to 30 but at equal interval of 2 arr = np.arange(5, 30, 2) print('Contents of the Numpy Array : ' , arr) # Comparision OPerator will be applied to all elements in array boolArr = arr < 10 print('Contents of the Bool Numpy Array : ', boolArr) # Select elements with True at corresponding value in bool array newArr = arr[boolArr] print('Contents of the New Numpy Array : ', newArr) newArr = arr[arr < 10] print('Contents of the New Numpy Array : ', newArr) print('*** Select elements from Numpy Array which are divisible by 3 ***') newArr = arr[arr%3==0] print('Contents of the Numpy Array : ', newArr) print('*** Select elements from Numpy Array which are greater than 5 and less than 20 ***') newArr = arr[(arr > 5) & (arr < 20)] print('Contents of the Numpy Array : ', newArr) if __name__ == '__main__': main() 
Output
1 2 3 4 5 6 7 8 9 10 
Select elements from Numpy Array based on conditions Contents of the Numpy Array : [ 5 7 9 11 13 15 17 19 21 23 25 27 29] Contents of the Bool Numpy Array : [ True True True False False False False False False False False False False] Contents of the New Numpy Array : [5 7 9] Contents of the New Numpy Array : [5 7 9] *** Select elements from Numpy Array which are divisible by 3 *** Contents of the Numpy Array : [ 9 15 21 27] *** Select elements from Numpy Array which are greater than 5 and less than 20 *** Contents of the Numpy Array : [ 7 9 11 13 15 17 19] 
Python Recommendations:
C++ & C++11 Recommendations:

Best C++ Courses for beginners to learn from scratch

5 Top Courses to learn C++11 / C+14

Best books to learn C++11
Leave a Reply