NumPy was created to perform scientific computing in Python. The NumPy library enables the user to create N-dimensional arrays, and perform linear algebra operations on NumPy objects.
In this shot, we will explore different ways in which we can extract the required elements from a NumPy array.
where()
methodThe where()
method accepts an expression to extract the required elements from the array. This expression should return a boolean
value.
Let’s see how the where()
method works in the code.
import numpy as np# Create a NumPy arraynumpy_array = np.array([2, 3, 6, 9, 11, 23, 34, 66])# Using where() methodindices = np.where((numpy_array >= 7) & (numpy_array < 11))print("Indices: ", indices)print("Elements satisfied the condition: ",numpy_array[indices])
In line 1, we import the numpy
package.
In line 4, we create an array and specify the number of elements to be 10
, starting from to .
In line 7, we use the where()
method and specify a condition to extract the elements which are greater than or equal to 7 and less than 11. This function returns the indices of the elements which satisfy the condition.
In line 8, we print the indices of the elements that satisfied the condition.
We use these indices and extract and print the elements in line 9.
logical_and()
methodThe logical_and()
method from the numpy
package accepts multiple conditions or expressions as a parameter. Each of the conditions or the expressions should return a boolean
value. These boolean
values are used to extract the required elements from the array. The logical_and()
function will return a list of boolean
elements.
Each boolean
element represents whether the element in the array, at the same index of the boolean
value, satisfies all the conditions or expressions that were passed to the logical_and()
method. The boolean
element is True
only when all the conditions provided to this function are satisfied.
Let’s see how the logical_and()
method works in the code.
import numpy as np# Create a NumPy arraynumpy_array = np.array([2, 3, 6, 9, 11, 23, 34, 66])# using logical_end() methodindices = np.where(np.logical_and(numpy_array >= 7, numpy_array < 11))print("Indices: ", indices)print("Elements satisfied the condition: ",numpy_array[indices])
In line 1, we import the numpy
package.
In line 4, we create an array and specify the number of elements to be 10
, ranging from 1 to 10.
In line 7, we use the logical_and()
method and specify two conditions to extract the elements, which are greater than or equal to 7 and less than 11.
logical_and()
method returns a list of boolean
values.boolean
values represents the index of the element from the array that satisfies the conditions.boolean
values is then passed to the where()
method, where it will be determined which element to consider.In line 8, we print the indices of the elements that satisfied the condition.
We use these indices and extract and print the elements in line 9.
The third way to extract the elements from the NumPy array is to provide all the conditions to the array itself. Let’s see how this will work in the code.
import numpy as np# Create a NumPy arraynumpy_array = np.array([2, 3, 6, 9, 11, 23, 34, 66])# Using condition on NumPy arraycondition_array = numpy_array[(numpy_array >= 7) & (numpy_array <= 14)]print("Elements satisfied the condition: ",condition_array)
In line 1, we import the numpy
package.
In line 4, we create an array and specify the number of elements to be 10
, ranging from 1 to 10.
In line 7, we specify two conditions directly to the array.
tuple
. We are using these conditions to extract the elements which are greater than or equal to 7 and less than or equal to 14.In line 8, we print the extracted elements.