mxnet.np.argsort¶
-
argsort
(a, axis=-1, descending=False, stable=True)¶ Returns the indices that sort an array x along a specified axis.
Notes
argsort is a standard API in https://data-apis.org/array-api/latest/API_specification/generated/signatures.sorting_functions.argsort.html instead of an official NumPy operator.
- Parameters
a (ndarray) – Array to sort.
axis (int or None, optional) – Axis along which to sort. The default is -1 (the last axis). If None, the flattened array is used.
descending (bool, optional) – sort order. If True, the returned indices sort x in descending order (by value). If False, the returned indices sort x in ascending order (by value).Default: False.
stable (bool, optional) – sort stability. If True, the returned indices must maintain the relative order of x values which compare as equal. If False, the returned indices may or may not maintain the relative order of x values which compare as equal. Default: True.
- Returns
index_array – Array of indices that sort a along the specified axis. If a is one-dimensional,
a[index_array]
yields a sorted a. More generally,np.take_along_axis(a, index_array, axis=axis)
always yields the sorted a, irrespective of dimensionality.- Return type
ndarray, int
Notes
This operator does not support different sorting algorithms.
Examples
One dimensional array:
>>> x = np.array([3, 1, 2]) >>> np.argsort(x) array([1, 2, 0])
Two-dimensional array:
>>> x = np.array([[0, 3], [2, 2]]) >>> x array([[0, 3], [2, 2]]) >>> ind = np.argsort(x, axis=0) # sorts along first axis (down) >>> ind array([[0, 1], [1, 0]]) >>> np.take_along_axis(x, ind, axis=0) # same as np.sort(x, axis=0) array([[0, 2], [2, 3]]) >>> ind = np.argsort(x, axis=1) # sorts along last axis (across) >>> ind array([[0, 1], [0, 1]]) >>> np.take_along_axis(x, ind, axis=1) # same as np.sort(x, axis=1) array([[0, 3], [2, 2]])
Indices of the sorted elements of a N-dimensional array:
>>> ind = np.unravel_index(np.argsort(x, axis=None), x.shape) >>> ind (array([0, 1, 1, 0]), array([0, 0, 1, 1])) >>> x[ind] # same as np.sort(x, axis=None) array([0, 2, 2, 3])