How to get the indices of the sorted array using NumPy in Python? Last Updated : 21 Nov, 2022 Comments Improve Suggest changes Like Article Like Report We can get the indices of the sorted elements of a given array with the help of argsort() method. This function is used to perform an indirect sort along the given axis using the algorithm specified by the kind keyword. It returns an array of indices of the same shape as arr that would sort the array. Syntax: numpy.argsort(arr, axis=-1, kind=’quicksort’, order=None) Example 1: Python3 import numpy as np # Original array array = np.array([10, 52, 62, 16, 16, 54, 453]) print(array) # Indices of the sorted elements of a # given array indices = np.argsort(array) print(indices) Output: [ 10 52 62 16 16 54 453] [0 3 4 1 5 2 6] Example 2: Python3 import numpy as np # Original array array = np.array([1, 2, 3, 4, 5]) print(array) # Indices of the sorted elements of # a given array indices = np.argsort(array) print(indices) Output: [1 2 3 4 5] [0 1 2 3 4] Example 3: Python3 import numpy as np # input 2d array in_arr = np.array([[ 2, 0, 1], [ 5, 4, 3]]) print ("Input array :\n", in_arr) # output sorted array indices out_arr1 = np.argsort(in_arr, kind ='mergesort', axis = 0) print ("\nOutput sorted array indices along axis 0:\n", out_arr1) out_arr2 = np.argsort(in_arr, kind ='heapsort', axis = 1) print ("\nOutput sorted array indices along axis 1:\n", out_arr2) Output: Input array : [[2 0 1] [5 4 3]] Output sorted array indices along axis 0: [[0 0 0] [1 1 1]] Output sorted array indices along axis 1: [[1 2 0] [2 1 0]] Comment More infoAdvertise with us Next Article How to get the indices of the sorted array using NumPy in Python? J jyotikayadav2017 Follow Improve Article Tags : Python Python-numpy Python numpy-Sorting Searching Practice Tags : python Similar Reads How to get the number of dimensions of a matrix using NumPy in Python? In this article, we will discuss how to get the number of dimensions of a matrix using NumPy. It can be found using the ndim parameter of the ndarray() method. Syntax: no_of_dimensions = numpy.ndarray.ndim Approach: Create an n-dimensional matrix using the NumPy package.Use ndim attribute available 3 min read How to find the Index of value in Numpy Array ? In this article, we are going to find the index of the elements present in a Numpy array.Using where() Methodwhere() method is used to specify the index of a particular element specified in the condition.Syntax: numpy.where(condition[, x, y])Example 1: Get index positions of a given valueHere, we fi 5 min read How to get index of NumPy multidimensional array in reverse order? In this article, we will see how to get the index of the NumPy multidimensional array in reverse order. Approach First, we import the NumPy library and initialize the necessary parameters which include the matrix which we need to work upon and other required parameters.Now we are going to loop over 3 min read How to rearrange columns of a 2D NumPy array using given index positions? In this article, we will learn how to rearrange columns of a given numpy array using given index positions. Here the columns are rearranged with the given indexes. For this, we can simply store the columns values in lists and arrange these according to the given index list but this approach is very 2 min read How to get the n-largest values of an array using NumPy? Let's see the program for how to get the n-largest values of an array using NumPy library. For getting n-largest values from a NumPy array we have to first sort the NumPy array using numpy.argsort() function of NumPy then applying slicing concept with negative indexing. Syntax: numpy.argsort(arr, ax 2 min read How to get values of an NumPy array at certain index positions? Sometimes we need to remove values from the source Numpy array and add them at specific indices in the target array. In NumPy, we have this flexibility, we can remove values from one array and add them to another array. We can perform this operation using numpy.put() function and it can be applied t 4 min read How to use numpy.argsort in Descending order in Python The numpy.argsort() function is used to conduct an indirect sort along the provided axis using the kind keyword-specified algorithm. It returns an array of indices of the same shape as arr, which would be used to sort the array. It refers to value indices ordered in ascending order. In this article, 4 min read How To Sort The Elements of a Tensor in PyTorch? In this article, we are going to see how to sort the elements of a PyTorch Tensor in Python. To sort the elements of a PyTorch tensor, we use torch.sort() method.  We can sort the elements along with columns or rows when the tensor is 2-dimensional. Syntax: torch.sort(input, dim=- 1, descending=Fals 3 min read How to rank Python NumPy arrays with ties? In this article, we are going to see how to rank Numpy arrays with ties-breakers in Python. The ranking is an essential statistical operation used in numerous fields like data science, sociology, etc. A very brute-force approach would be to sort the indices of the array in order of their correspond 3 min read Compute the inner product of vectors for 1-D arrays using NumPy in Python Python has a popular package called NumPy which used to perform complex calculations on 1-D and multi-dimensional arrays. To find the inner product of two arrays, we can use the inner() function of the NumPy package. Syntax: numpy.inner(array1, array2) Parameters: array1, array2: arrays to be evalu 2 min read Like