python numpy np.argsort()(返回将对数组进行排序的索引)(不懂区别?)
生活随笔
收集整理的這篇文章主要介紹了
python numpy np.argsort()(返回将对数组进行排序的索引)(不懂区别?)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
from numpy\core\fromnumeric.py
@array_function_dispatch(_argsort_dispatcher) def argsort(a, axis=-1, kind=None, order=None):"""Returns the indices that would sort an array.返回將對數(shù)組進行排序的索引。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.使用“ kind”關(guān)鍵字指定的算法沿給定的軸執(zhí)行間接排序。 它返回一個與`a'形狀相同的索引數(shù)組,該索引數(shù)組沿給定軸按排序順序?qū)?shù)據(jù)進行索引。Parameters----------a : array_likeArray to sort.要進行排序的數(shù)組axis : int or None, optionalAxis along which to sort. The default is -1 (the last axis). If None, the flattened array is used.要排序的軸。 默認(rèn)值為-1(最后一個軸)。 如果為None,則使用扁平化的數(shù)組。kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, optionalSorting algorithm. The default is 'quicksort'. Note that both 'stable' and 'mergesort' use timsort under the covers and, in general, the actual implementation will vary with data type. The 'mergesort' option is retained for backwards compatibility.排序算法。 默認(rèn)值為“快速排序”。 請注意,“穩(wěn)定”和“合并排序”在后臺都使用timsort,并且通常,實際實現(xiàn)會隨數(shù)據(jù)類型而變化。 保留'mergesort'選項是為了向后兼容。.. versionchanged:: 1.15.0.The 'stable' option was added.order : str or list of str, optionalWhen `a` is an array with fields defined, this argument specifies which fields to compare first, second, etc. A single field can be specified as a string, and not all fields need be specified, but unspecified fields will still be used, in the order in which they come up in the dtype, to break ties.當(dāng)“ a”是定義了字段的數(shù)組時,此參數(shù)指定要比較的字段的第一個,第二個等。單個字段可以指定為字符串,并且不需要指定所有字段,但仍將使用未指定的字段, 按照他們在dtype中出現(xiàn)的順序,打破關(guān)系。Returns-------index_array : ndarray, intArray 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.沿指定的“軸”對“ a”進行排序的索引數(shù)組。如果“ a”是一維的,則“ a [index_array]”會產(chǎn)生一個排序的“ a”。更一般而言,“ np.take_along_axis(a,index_array,axis = axis)”始終會產(chǎn)生排序的“ a”,而與維數(shù)無關(guān)。See Also--------sort : Describes sorting algorithms used.描述使用的排序算法。lexsort : Indirect stable sort with multiple keys.具有多個鍵的間接穩(wěn)定排序。ndarray.sort : Inplace sort.就地排序。argpartition : Indirect partial sort.間接部分排序。take_along_axis : Apply ``index_array`` from argsort to an array as if by calling sort.將argsort中的index_array應(yīng)用于數(shù)組,就好像調(diào)用sort一樣。Notes-----See `sort` for notes on the different sorting algorithms.As of NumPy 1.4.0 `argsort` works with real/complex arrays containingnan values. The enhanced sort order is documented in `sort`.有關(guān)不同排序算法的說明,請參見`sort`。從NumPy 1.4.0開始,“ argsort”可用于包含以下內(nèi)容的實/復(fù)雜數(shù)組nan值。 增強的排序順序在`sort`中記錄。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]])>>> xarray([[0, 3],[2, 2]])>>> ind = np.argsort(x, axis=0) # sorts along first axis (down)>>> indarray([[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)>>> indarray([[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])Sorting with keys:>>> x = np.array([(1, 0), (0, 1)], dtype=[('x', '<i4'), ('y', '<i4')])>>> xarray([(1, 0), (0, 1)],dtype=[('x', '<i4'), ('y', '<i4')])>>> np.argsort(x, order=('x','y'))array([1, 0])>>> np.argsort(x, order=('y','x'))array([0, 1])"""return _wrapfunc(a, 'argsort', axis=axis, kind=kind, order=order)參考文章:python numpy np.lexsort()(使用鍵序列執(zhí)行間接穩(wěn)定排序)(具體沒太搞懂區(qū)別?)
總結(jié)
以上是生活随笔為你收集整理的python numpy np.argsort()(返回将对数组进行排序的索引)(不懂区别?)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python numpy np.lexs
- 下一篇: 稳定排序与不稳定排序的定义