
- 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 - Sorting Along an Axis
Sorting Along an Axis in NumPy
In NumPy, arrays can be multi-dimensional, and sorting can be performed along any of these dimensions (axes). Sorting along an axis means arranging the elements of the array in a specific order based on the values along that axis.
In NumPy, the different axes are: 0th axis (rows), 1st axis (columns), and higher axes (depth or additional dimensions).
The np.sort() Function
The np.sort() function sorts the elements of an array and returns a new array containing the sorted elements. Sorting can be done along a specified axis, or if no axis is specified, the function defaults to sorting along the last axis. Following is the syntax −
numpy.sort(a, axis=-1, kind=None, order=None)
Where,
- a: It is the array to be sorted.
- axis: It is the axis along which to sort. Default is -1, which means sorting along the last axis.
- kind: It is the sorting algorithm to use. Options include 'quicksort', 'mergesort', 'heapsort', and 'stable'.
- order: It is used when sorting a structured array to define which fields to compare.
Sorting Along Axis 0 (Rows)
Sorting along axis 0 in NumPy refers to sorting the array elements along the vertical axis, which corresponds to rows in a 2D array.
This operation is useful when you want to order the rows of a matrix based on the values in each column.
In a 2D array −
- Axis 0 corresponds to the rows.
- Axis 1 corresponds to the columns.
When sorting along axis 0, NumPy sorts the elements column-wise. For each column, the values are arranged in ascending order, and the rows are reordered accordingly.
Example
In the following example, we are using the sort() function to sort the elements in each column −
import numpy as np # Create a 2D array arr = np.array([[3, 6, 4], [5, 1, 2]]) # Sort along axis 0 (rows) sorted_arr_axis0 = np.sort(arr, axis=0) print("Original Array:\n", arr) print("Sorted Along Axis 0:\n", sorted_arr_axis0)
The output will have the rows reordered such that the values in each column are sorted in ascending order −
Original Array: [[3 6 4] [5 1 2]] Sorted Along Axis 0: [[3 1 2] [5 6 4]]
Sorting Along Axis 1 (Columns)
Sorting along axis 1 in NumPy refers to sorting the array elements along the horizontal axis, which corresponds to columns in a 2D array. This operation is useful when you want to order the elements in each row based on their values.
When sorting along axis 1, NumPy sorts the elements row-wise. For each row, the values are arranged in ascending order, and the order of columns is adjusted accordingly.
Example
In this example, we are using the sort() function to sort the elements in each row −
import numpy as np # Create a 2D array arr = np.array([[3, 2, 1], [6, 5, 4]]) # Sort along axis 1 (columns) sorted_arr_axis1 = np.sort(arr, axis=1) print("Original Array:\n", arr) print("Sorted Along Axis 1:\n", sorted_arr_axis1)
The output will have the columns reordered within each row such that the values are sorted in ascending order −
Original Array: [[3 2 1] [6 5 4]] Sorted Along Axis 1: [[1 2 3] [4 5 6]]
Sorting Multi-dimensional Arrays
Sorting multi-dimensional arrays in NumPy involves organizing the elements of the array along one or more specific axes.
Sorting along specific axes in a multi-dimensional array is similar to sorting in 2D arrays but extends to higher dimensions. Here is how sorting works along different axes −
- Axis 0 (First Dimension): Sorting along axis 0 affects rows.
- Axis 1 (Second Dimension): Sorting along axis 1 affects columns.
- Axis 2 (Third Dimension): In 3D arrays, sorting along axis 2 affects the depth slices.
Example
In the example below, we are sorting a 3D array along different axes: depth (axis 0), rows (axis 1), and columns (axis 2). Each sorting operation arranges the elements within the specified dimension, resulting in differently ordered arrays −
import numpy as np # Create a 3D array arr = np.array([[[3, 2, 1], [6, 5, 4]], [[9, 8, 7], [12, 11, 10]]]) # Sort along axis 0 (depth) sorted_arr_axis0 = np.sort(arr, axis=0) # Sort along axis 1 (rows) sorted_arr_axis1 = np.sort(arr, axis=1) # Sort along axis 2 (columns) sorted_arr_axis2 = np.sort(arr, axis=2) print("Original Array:\n", arr) print("Sorted Along Axis 0:\n", sorted_arr_axis0) print("Sorted Along Axis 1:\n", sorted_arr_axis1) print("Sorted Along Axis 2:\n", sorted_arr_axis2)
Following is the output of the above code −
Original Array: [[[ 3 2 1] [ 6 5 4]] [[ 9 8 7] [12 11 10]]] Sorted Along Axis 0: [[[ 3 2 1] [ 6 5 4]] [[ 9 8 7] [12 11 10]]] Sorted Along Axis 1: [[[ 3 2 1] [ 6 5 4]] [[ 9 8 7] [12 11 10]]] Sorted Along Axis 2: [[[ 1 2 3] [ 4 5 6]] [[ 7 8 9] [10 11 12]]]
Sorting with Different Algorithms
The NumPy sort() function supports several sorting algorithms, each with its own strengths −
- Quicksort
- Mergesort
- Heapsort
- Stable Sort
These algorithms can be chosen by setting the kind parameter in the np.sort() function. By default, NumPy uses the "quicksort" algorithm.
Sorting algorithms are methods for arranging elements in a specific order. Different algorithms have different performance characteristics and are optimized for various types of data and array sizes.
Example
In this example, the array is sorted using "quicksort" and "mergesort" algorithms along axis "1". Different sorting algorithms can affect performance and stability −
import numpy as np # Create a 2D array arr = np.array([[3, 2, 1], [6, 5, 4]]) # Sort using different algorithms sorted_quicksort = np.sort(arr, axis=1, kind='quicksort') sorted_mergesort = np.sort(arr, axis=1, kind='mergesort') print("Sorted with Quicksort:\n", sorted_quicksort) print("Sorted with Mergesort:\n", sorted_mergesort)
The output obtained is as shown below −
Sorted with Quicksort: [[1 2 3] [4 5 6]] Sorted with Mergesort: [[1 2 3] [4 5 6]]