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?
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.
np. argsort returns the index of the sorted array given by the 'kind' (which specifies the type of sorting algorithm).
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.
NumPy's np. argsort is able to do stable sorting through passing kind = 'stable' argument.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With