Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get original indices of a sorted Numpy array

Tags:

python

numpy

I have an array of distances a = np.array([20.5 ,5.3 ,60.7 ,3.0 ], 'double') and I need the indices of the sorted array (for example [3, 1, 0, 2], for a.sort()). Is there a function in Numpy to do that?

like image 893
Adrian Martínez Vargas Avatar asked Sep 07 '11 23:09

Adrian Martínez Vargas


People also ask

How do I get the indices of a sorted NumPy array?

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.

What does NP Argsort return?

np. argsort returns the index of the sorted array given by the 'kind' (which specifies the type of sorting algorithm).

How do I use Argsort in NumPy?

Syntax : numpy. argsort(arr, axis=-1, kind='quicksort', order=None) Parameters : arr : [array_like] Input array. axis : [int or None] Axis along which to sort. If None, the array is flattened before sorting.

Is NP Argsort stable?

NumPy's np. argsort is able to do stable sorting through passing kind = 'stable' argument.


1 Answers

Yes, there's the x = numpy.argsort(a) function or x = numpy.ndarray.argsort(a) method. It does exactly what you're asking for. You can also call argsort as a method on an ndarray object like so: a.argsort().

Here's a link to the documentation: http://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html#numpy.argsort

like image 162
Phillip Cloud Avatar answered Oct 01 '22 09:10

Phillip Cloud