Numpy argsort - what is it doing?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
NumPy, a powerful library for numerical computations in Python, provides a myriad of functions and utilities for handling arrays. Among these is `argsort`, a function widely employed for sorting operations based on array element values, while returning the indices that would sort an array. Let's delve into the intricacies of `argsort`, examine its operation, and how it can be leveraged effectively.
Understanding `numpy.argsort`
`numpy.argsort(a, axis=-1, kind='quicksort', order=None)` is a function that returns the indices that would sort an array. It enables sorting operations without actually altering the original array, making it a useful tool for indexed-based sorting and efficient manipulations.
`Parameters`
• `a`: array_like
The input array to sort.
• `axis`: int or None, optional
The axis along which to apply the sorting. Default is -1 (last axis). If `None`, the flattened array is used.
• `kind`: {'quicksort', 'mergesort', 'heapsort', 'stable'}, optional
The sorting algorithm to use. The default is 'quicksort'. Other options include 'mergesort', 'heapsort', and 'stable', each with its pros and cons in terms of time complexity and stability.
• `order`: str or list of str, optional
If the array is structured, this parameter specifies the fields to compare first, second, etc.
Returns
• `indices`: ndarray of ints
Array of indices that sort `a` along the specified axis.
Examples
Let's explore some concrete examples to illustrate `argsort` in action.
Example 1: Basic Usage
• Ranking and Sorting Problems: `argsort` is instrumental in sorting data sequences, such as student grades or stock prices, without changing the original data set. • Index-based Data Retrieval: Often used in data science for accessing and manipulating arrays based on sorted criteria, `argsort` facilitates efficient data querying. • Multidimensional Data Sorting: Vital for handling multi-dimensional datasets like images, video streams, and multidimensional numeric data, where sorting along a particular dimension is necessitated. • `quicksort`: Fastest but unstable. • `mergesort`: A bit slower but stable, which preserves the order of equal elements. • `heapsort`: Requires minimal additional space. • `stable`: Guarantees a stable sort.
Related reading
- Numpy array dimensions
- NumPy array initialization fill with identical values
- NumPy array is not JSON serializable
- Numpy array to TFrecord
- O1 algorithm to determine if node is descendant of another node in a multiway tree?
- Obtain forest out of tree with even number of nodes
- numpy convert categorical string arrays to an integer array
- Numpy custom Cumsum function with upper/lower limits?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.