Returns the indices that would sort an array. 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 `a` that index data along the given axis in sorted order. Parameters -------
(a, axis=-1, kind=None, order=None)
| 1024 | |
| 1025 | @array_function_dispatch(_argsort_dispatcher) |
| 1026 | def argsort(a, axis=-1, kind=None, order=None): |
| 1027 | """ |
| 1028 | Returns the indices that would sort an array. |
| 1029 | |
| 1030 | Perform an indirect sort along the given axis using the algorithm specified |
| 1031 | by the `kind` keyword. It returns an array of indices of the same shape as |
| 1032 | `a` that index data along the given axis in sorted order. |
| 1033 | |
| 1034 | Parameters |
| 1035 | ---------- |
| 1036 | a : array_like |
| 1037 | Array to sort. |
| 1038 | axis : int or None, optional |
| 1039 | Axis along which to sort. The default is -1 (the last axis). If None, |
| 1040 | the flattened array is used. |
| 1041 | kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, optional |
| 1042 | Sorting algorithm. The default is 'quicksort'. Note that both 'stable' |
| 1043 | and 'mergesort' use timsort under the covers and, in general, the |
| 1044 | actual implementation will vary with data type. The 'mergesort' option |
| 1045 | is retained for backwards compatibility. |
| 1046 | |
| 1047 | .. versionchanged:: 1.15.0. |
| 1048 | The 'stable' option was added. |
| 1049 | order : str or list of str, optional |
| 1050 | When `a` is an array with fields defined, this argument specifies |
| 1051 | which fields to compare first, second, etc. A single field can |
| 1052 | be specified as a string, and not all fields need be specified, |
| 1053 | but unspecified fields will still be used, in the order in which |
| 1054 | they come up in the dtype, to break ties. |
| 1055 | |
| 1056 | Returns |
| 1057 | ------- |
| 1058 | index_array : ndarray, int |
| 1059 | Array of indices that sort `a` along the specified `axis`. |
| 1060 | If `a` is one-dimensional, ``a[index_array]`` yields a sorted `a`. |
| 1061 | More generally, ``np.take_along_axis(a, index_array, axis=axis)`` |
| 1062 | always yields the sorted `a`, irrespective of dimensionality. |
| 1063 | |
| 1064 | See Also |
| 1065 | -------- |
| 1066 | sort : Describes sorting algorithms used. |
| 1067 | lexsort : Indirect stable sort with multiple keys. |
| 1068 | ndarray.sort : Inplace sort. |
| 1069 | argpartition : Indirect partial sort. |
| 1070 | take_along_axis : Apply ``index_array`` from argsort |
| 1071 | to an array as if by calling sort. |
| 1072 | |
| 1073 | Notes |
| 1074 | ----- |
| 1075 | See `sort` for notes on the different sorting algorithms. |
| 1076 | |
| 1077 | As of NumPy 1.4.0 `argsort` works with real/complex arrays containing |
| 1078 | nan values. The enhanced sort order is documented in `sort`. |
| 1079 | |
| 1080 | Examples |
| 1081 | -------- |
| 1082 | One dimensional array: |
| 1083 |
no test coverage detected