Table of Contents
ToggleSearching arrays in NumPy refers to the process of locating elements in an array that meet specific criteria or retrieving their indices.
NumPy provides various functions to perform searches, even in large multi-dimensional arrays, they are as follows −
where() Functionnonzero() Functionsearchsorted() Functionargmax() Functionargmin() Functionextract() FunctionThe NumPy where() function is used to find the indices of elements in an array that satisfy a given condition. The function can also be used to replace elements based on a condition. Following is the syntax −
np.where(condition, [x, y])
Where,
condition − It is the condition to be checked.x (Optional) − It is the values to use where the condition is true.y (Optional) − It is the values to use where the condition is false.In the following example, we are using the where() function to retrieve the indices of elements that are greater than “25” in the array and also to replace elements in the array that are less than or equal to “25” with “0” −
# Open Compiler
import numpy as np
array = np.array([10, 20, 30, 40, 50])
indices = np.where(array > 25)
print("Indices where array elements are greater than 25:", indices)
# Replacing elements based on condition
modified_array = np.where(array > 25, array, 0)
print("Array after replacing elements <= 25 with 0:", modified_array)
Output:
Following is the output obtained −
Indices where array elements are greater than 25: (array([2, 3, 4]),)
Array after replacing elements <= 25 with 0: [ 0 0 30 40 50]
The NumPy nonzero() function is used to find the indices of all non-zero elements in an array. It returns a tuple of arrays, where each array contains the indices of non-zero elements along a specific dimension.
This function is useful when you want to filter out zero elements or identify the location of significant elements in sparse arrays. Following is the syntax −
numpy.nonzero(a)
Where, a is the input array for which you want to find the indices of non-zero elements.
In the example below, we are retrieving the indices of non-zero elements in the 1D array using the nonzero() function −
# Open Compiler
import numpy as np
array = np.array([0, 1, 2, 0, 3, 0, 4])
nonzero_indices = np.nonzero(array)
print("Indices of non-zero elements:", nonzero_indices)
Output:
This will produce the following result −
Indices of non-zero elements: (array([1, 2, 4, 6]),)
The NumPy searchsorted() function is used to find the indices where elements should be inserted to maintain order in a sorted array.
This function is useful in algorithms that require maintaining a sorted order while dynamically inserting elements. Following is the syntax −
np.searchsorted(sorted_array, values, side='left')
Where,
sorted_array − It is the sorted array to search.values − It is the values to insert.side − If 'left', the index of the first suitable location is given. If 'right', the index of the last suitable location is given.In this example, we retrieve the indices at which the values "2", "4", and "6" should be inserted in the sorted array to maintain order −
# Open Compiler
import numpy as np
sorted_array = np.array([1, 3, 5, 7, 9])
values = np.array([2, 4, 6])
indices = np.searchsorted(sorted_array, values)
print("Indices where values should be inserted:", indices)
Output:
Following is the output of the above code −
Indices where values should be inserted: [1 2 3]
The argmax() function in NumPy is used to find the indices of the maximum value along a specified axis in an array. If no axis is specified, it returns the index of the maximum value in the flattened array. Following is the syntax −
numpy.argmax(a, axis=None, out=None)
Where,
a − It is the input array.axis (Optional) − It is the axis along which to find the maximum. If not specified, the array is flattened before performing the operation.out (Optional) − It is a location into which the result is stored. If provided, it must have the same shape as the expected output.In the following example, we are using the argmax() function to find the index of maximum value along a specified axis in a 2D array −
# Open Compiler
import numpy as np
array = np.array([[10, 15, 5], [7, 12, 20]])
index_of_max_along_axis = np.argmax(array, axis=1)
print("Indices of the maximum values along axis 1:", index_of_max_along_axis)
Output:
The output obtained is as shown below −
Indices of the maximum values along axis 1: [1 2]
Here, we are finding the index of maximum value in a flattened array using the argmax() function −
# Open Compiler
import numpy as np
array = np.array([[10, 15, 5], [7, 12, 20]])
index_of_max_flattened = np.argmax(array)
print("Index of the maximum value in the flattened array:", index_of_max_flattened)
Output:
After executing the above code, we get the following output −
Index of the maximum value in the flattened array: 5
The argmin() function in NumPy is used to find the indices of the minimum value along a specified axis in an array. If no axis is specified, it returns the index of the minimum value in the flattened array. Following is the syntax −
numpy.argmin(a, axis=None, out=None)
Where,
a − It is the input array.axis (Optional) − It is the axis along which to find the minimum. If not specified, the array is flattened before performing the operation.out (Optional) − It is a location into which the result is stored. If provided, it must have the same shape as the expected output.In the following example, we are using the argmin() function to find the index of minimum value along a specified axis in a 2D array −
# Open Compiler
import numpy as np
array = np.array([[10, 15, 5], [7, 12, 2]])
index_of_min_along_axis = np.argmin(array, axis=1)
print("Indices of the minimum values along axis 1:", index_of_min_along_axis)
Output:
The result produced is as follows −
Indices of the minimum values along axis 1: [2 2]
The extract() function in NumPy is used to extract elements from an array based on a boolean condition. It returns a 1D array containing only the elements of the input array that correspond to True values in the boolean condition.
Unlike np.where() function, which returns indices, np.extract() function directly returns the elements that meet the condition. Following is the syntax −
numpy.extract(condition, arr)
Where,
condition − It is a boolean array or condition that specifies which elements to extract. It must be of the same shape as arr.arr − It is the input array from which elements are to be extracted.In the example below, we are using the np.extract() function to filter and return elements from the array that are greater than "25" −
# Open Compiler
import numpy as np
array = np.array([10, 20, 30, 40, 50])
condition = array > 25
extracted_elements = np.extract(condition, array)
print("Elements greater than 25:", extracted_elements)
Output:
We get the output as shown below −
Elements greater than 25: [30 40 50]
Boolean indexing in NumPy is used for searching and filtering arrays based on specific conditions. It involves creating a boolean array (or mask) where each value is True or False based on whether a condition is satisfied.
This boolean array is then used to index into the original array, extracting only those elements where the condition is True.
Following is a simple example of using boolean indexing in NumPy to filter elements based on a condition −
# Open Compiler
import numpy as np
array = np.array([10, 20, 30, 40, 50])
boolean_mask = array > 25
filtered_array = array[boolean_mask]
print("Filtered array (elements > 25):", filtered_array)
Output:
We get the output as shown below −
Filtered array (elements > 25): [30 40 50]
Key Takeaway: Master searching arrays in NumPy with Vista Academy!
