Hey, folks! In this article, we will be focusing on the working of Python numpy.where() method.
Working of numpy.where() function
Python NumPy module contains many built-in functions to create and manipulate the array elements altogether.
The numpy.where() function
is used to return the array elements based on certain conditions.
Syntax:
numpy.where(condition,a,b)
condition
: The manipulation condition to be applied on the array needs to mentioned.a
: If the condition is met i.e. the condition turns out to be True, then the function yields a.b
: If the condition is not met, this value is returned by the function.
Example 1:
import numpy as np
data = np.array([[10,20,30], [40,50,60], [0,1,2]])
print(np.where(data<20,True,False))
In the above example, for all the array elements whose data value is < 20, those data values are replaced by True. And, for all the array elements whose data values is > 20 i.e. the values which do not satisfy the condition are replaced by False.
Output:
[[ True False False]
[False False False]
[ True True True]]
Example 2:
import numpy as np
data = np.array([[10,20,30], [40,50,60], [0,1,2]])
data_set = np.where(data<20)
print("Data elements less than 20:\n")
print(data[data_set])
In the above example, we have displayed all the array elements that are less than 20.
Output:
Data elements less than 20:
[10 0 1 2]
Python numpy.where() function with Multiple conditions
Multiple condition can be applied along with the numpy.where() function
to manipulate the array elements against multiple conditions.
Syntax:
numpy.where((condition1)&(condition2))
OR
numpy.where((condition1)|(condition2))
Example 1:
import numpy as np
data = np.array([[10,20,30], [40,50,60], [0,1,2]])
data_set = np.where((data!=20)&(data<40))
print(data[data_set])
In this example, we have displayed all the array elements whose data values are less than 40 and not equal to 20.
Output:
[10 30 0 1 2]
Example 2:
import numpy as np
data = np.array([[10,20,30], [40,50,60], [0,1,2]])
data_set = np.where((data<20)|(data>40))
print(data[data_set])
In the above piece of code, all the data values satisfying either of the mentioned condition gets displayed i.e. array elements less than 20 as well as array elements greater than 40 are displayed.
Output:
[10 50 60 0 1 2]
Replacing the array values using numpy.where() function
Using numpy.where() function, we can replace the values depending on fulfilment of certain condition.
Syntax:
numpy.where(condition,element1,element2)
Example:
import numpy as np
data = np.random.randn(2,3)
print("Data before manipulation:\n")
print(data)
data_set = np.where((data>0),data,0)
print("\nData after manipulation:\n")
print(data_set)
In this example, we have replaced all the array elements with 0 whose data values is less than 0 i.e. not satisfying the mentioned condition.
Output:
Data before manipulation:
[[ 0.47544941 -0.35892873 -0.28972221]
[-0.9772084 1.04305061 1.84890316]]
Data after manipulation:
[[0.47544941 0. 0. ]
[0. 1.04305061 1.84890316]]
Conclusion
Thus, in this article, we have understood the working of Python numpy.where() function across various inputs.
References
- Python numpy.where() function — JournalDev
- Python numpy.where() function — Documentation