
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
NumPy - Searching Arrays
Searching Arrays in NumPy
Searching 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 −
- The where() Function
- The nonzero() Function
- The searchsorted() Function
- The argmax() Function
- The argmin() Function
- The extract() Function
Using the where() Function
The 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.
Example
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" −
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)
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]
Using the nonzero() Function
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.
Example
In the example below, we are retrieving the indices of non-zero elements in the 1D array using the nonzero() function −
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)
This will produce the following result −
Indices of non-zero elements: (array([1, 2, 4, 6]),)
Using the searchsorted() Function
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.
Example
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 −
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)
Following is the output of the above code −
Indices where values should be inserted: [1 2 3]
Using the argmax() Function
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.
Example: Using argmax() Function in a 2D Array
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 −
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)
The output obtained is as shown below −
Indices of the maximum values along axis 1: [1 2]
Example: Using argmax() Function in a Flattened Array
Here, we are finding the index of maximum value in a flattened array using the argmax() function −
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)
After executing the above code, we get the following output −
Index of the maximum value in the flattened array: 5
Using the argmin() Function
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.
Example
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 −
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)
The result produced is as follows −
Indices of the minimum values along axis 1: [2 2]
Using the extract() Function
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.
Example
In the example below, we are using the np.extract() function to filter and return elements from the array that are greater than "25" −
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)
We get the output as shown below −
Elements greater than 25: [30 40 50]
Searching Using Boolean Indexing
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.
Example
Following is a simple example of using boolean indexing in NumPy to filter elements based on a condition −
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)
We get the output as shown below −
Filtered array (elements > 25): [30 40 50]