
- 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 - Sort, Search & Counting Functions
NumPy provides a variety of functions for sorting, searching, and counting elements in arrays. These functions can be extremely useful for data manipulation and analysis.
NumPy offers several sorting algorithms, each with its characteristics. Following is a comparison of three common sorting algorithms −
kind | speed | worst case | work space | stable |
---|---|---|---|---|
'quicksort' | 1 | O(n^2) | 0 | no |
'mergesort' | 2 | O(n*log(n)) | ~n/2 | yes |
'heapsort' | 3 | O(n*log(n)) | 0 | no |
The numpy.sort() Function
The sort() function returns a sorted copy of the input array. It can sort arrays along any specified axis and supports different sorting algorithms. Following is the syntax −
numpy.sort(a, axis, kind, order)
Where,
Sr.No. | Parameter & Description |
---|---|
1 |
a Array to be sorted |
2 |
axis The axis along which the array is to be sorted. If none, the array is flattened, sorting on the last axis |
3 |
kind Default is quicksort |
4 |
order If the array contains fields, the order of fields to be sorted |
Example
In the following example, we are sorting a 2D NumPy array both by default and along a specific axis. We also demonstrate sorting a structured array by a specific field, such as 'name' −
import numpy as np # Create a 2D array a = np.array([[3, 7], [9, 1]]) print("Our array is:",a) # Default sort print("Applying sort() function:",np.sort(a)) # Sort along axis 0 print("Sort along axis 0:",np.sort(a, axis=0)) # Order parameter in sort function dt = np.dtype([('name', 'S10'), ('age', int)]) a = np.array([("raju", 21), ("anil", 25), ("ravi", 17), ("amar", 27)], dtype=dt) print("Our array is:",a) print("Order by name:",np.sort(a, order='name'))
It will produce the following output −
Our array is: [[3 7] [9 1]] Applying sort() function: [[3 7] [1 9]] Sort along axis 0: [[3 1] [9 7]] Our array is: [('raju', 21) ('anil', 25) ('ravi', 17) ('amar', 27)] Order by name: [('amar', 27) ('anil', 25) ('raju', 21) ('ravi', 17)]
The numpy.argsort() Function
The numpy.argsort() function performs an indirect sort on input array, along the given axis and using a specified kind of sort to return the array of indices of data. This indices array is used to construct the sorted array.
Example
In this example, we are retrieving indices, which are the positions of the sorted elements in the original array using the argsort() function. Using these indices, you can reconstruct the sorted array −
import numpy as np # Create an array x = np.array([3, 1, 2]) print("Our array is:",x) # Get indices that would sort the array y = np.argsort(x) print("Applying argsort() to x:",y) # Reconstruct the sorted array using the indices print("Reconstruct original array in sorted order:",x[y]) # Reconstruct the original array using a loop print("Reconstruct the original array using loop:") for i in y: print(x[i], end=' ')
It will produce the following output −
Our array is: [3 1 2] Applying argsort() to x: [1 2 0] Reconstruct original array in sorted order: [1 2 3] Reconstruct the original array using loop: 1 2 3
The numpy.lexsort() Function
The NumPy lexort() function performs an indirect sort using a sequence of keys. The keys can be seen as a column in a spreadsheet. The function returns an array of indices, using which the sorted data can be obtained. Note, that the last key happens to be the primary key of sort.
Example
In this example, we are using np.lexsort() function to sort a dataset based on multiple keys, where the last key "nm" is the primary sorting criterion. The sorted indices are then used to display the sorted data by combining the names and corresponding fields −
import numpy as np # Define keys nm = ('raju', 'anil', 'ravi', 'amar') dv = ('f.y.', 's.y.', 's.y.', 'f.y.') # Get indices for sorted order ind = np.lexsort((dv, nm)) print("Applying lexsort() function:",ind) # Use indices to get sorted data print("Use this index to get sorted data:",[nm[i] + ", " + dv[i] for i in ind])
It will produce the following output −
Applying lexsort() function: [3 1 0 2] Use this index to get sorted data: ['amar, f.y.', 'anil, s.y.', 'raju, f.y.', 'ravi, s.y.']
NumPy provides functions to find indices of maximum, minimum, and non-zero elements, as well as elements satisfying a condition.
The numpy.argmax() and numpy.argmin() Functions
The NumPy argmax() and argmin() functions return the indices of maximum and minimum elements respectively along the given axis.
Example
In this example, we are using np.argmax() and np.argmin() functions to find the indices of maximum and minimum values in a 2D array, both in the flattened array and along specific axes −
import numpy as np # Create a 2D array a = np.array([[30, 40, 70], [80, 20, 10], [50, 90, 60]]) print("Our array is:",a) # Apply argmax() function print("Applying argmax() function:",np.argmax(a)) # Index of maximum number in flattened array print("Index of maximum number in flattened array:",a.flatten()) # Array containing indices of maximum along axis 0 print("Array containing indices of maximum along axis 0:") maxindex = np.argmax(a, axis=0) print(maxindex) # Array containing indices of maximum along axis 1 print("Array containing indices of maximum along axis 1:") maxindex = np.argmax(a, axis=1) print(maxindex) # Apply argmin() function print("Applying argmin() function:") minindex = np.argmin(a) print(minindex) # Flattened array print("Flattened array:",a.flatten()[minindex]) # Flattened array along axis 0 print("Flattened array along axis 0:") minindex = np.argmin(a, axis=0) print(minindex) # Flattened array along axis 1 print("Flattened array along axis 1:") minindex = np.argmin(a, axis=1) print(minindex)
The output includes indices for these extrema, demonstrating how to access and interpret these positions within the array −
Our array is: [[30 40 70] [80 20 10] [50 90 60]] Applying argmax() function: 7 Index of maximum number in flattened array [30 40 70 80 20 10 50 90 60] Array containing indices of maximum along axis 0: [1 2 0] Array containing indices of maximum along axis 1: [2 0 1] Applying argmin() function: 5 Flattened array: 10 Flattened array along axis 0: [0 1 1] Flattened array along axis 1: [0 2 0]
The numpy.nonzero() Function
The numpy.nonzero() function returns the indices of non-zero elements in the input array.
Example
In the example below, we are retrieving the indices of non-zero elements in the array "a" using the nonzero() function −
import numpy as np a = np.array([[30,40,0],[0,20,10],[50,0,60]]) print ('Our array is:',a) print ('Applying nonzero() function:',np.nonzero (a))
It will produce the following output −
Our array is: [[30 40 0] [ 0 20 10] [50 0 60]] Applying nonzero() function: (array([0, 0, 1, 1, 2, 2]), array([0, 1, 1, 2, 0, 2]))
The numpy.where() Function
The where() function returns the indices of elements in an input array where the given condition is satisfied as shown in the example below −
import numpy as np x = np.arange(9.).reshape(3, 3) print ('Our array is:',x) print ('Indices of elements > 3') y = np.where(x > 3) print (y) print ('Use these indices to get elements satisfying the condition',x[y])
It will produce the following output −
Our array is: [[ 0. 1. 2.] [ 3. 4. 5.] [ 6. 7. 8.]] Indices of elements > 3 (array([1, 1, 2, 2, 2]), array([1, 2, 0, 1, 2])) Use these indices to get elements satisfying the condition [ 4. 5. 6. 7. 8.]
The numpy.extract() Function
The extract() function returns the elements satisfying any condition as shown in the example below −
import numpy as np x = np.arange(9.).reshape(3, 3) print ('Our array is:',x) # define a condition condition = np.mod(x,2) == 0 print ('Element-wise value of condition',condition) print ('Extract elements using condition',np.extract(condition, x))
It will produce the following output −
Our array is: [[ 0. 1. 2.] [ 3. 4. 5.] [ 6. 7. 8.]] Element-wise value of condition [[ True False True] [False True False] [ True False True]] Extract elements using condition [ 0. 2. 4. 6. 8.]