python numpy np.argsort()(返回将对数组进行排序的索引)(不懂区别?)
生活随笔
收集整理的這篇文章主要介紹了
python numpy np.argsort()(返回将对数组进行排序的索引)(不懂区别?)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
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.返回將對(duì)數(shù)組進(jìn)行排序的索引。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í)行間接排序。 它返回一個(gè)與`a'形狀相同的索引數(shù)組,該索引數(shù)組沿給定軸按排序順序?qū)?shù)據(jù)進(jìn)行索引。Parameters----------a : array_likeArray to sort.要進(jìn)行排序的數(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(最后一個(gè)軸)。 如果為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)值為“快速排序”。 請(qǐng)注意,“穩(wěn)定”和“合并排序”在后臺(tái)都使用timsort,并且通常,實(shí)際實(shí)現(xiàn)會(huì)隨數(shù)據(jù)類型而變化。 保留'mergesort'選項(xiàng)是為了向后兼容。.. 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í),此參數(shù)指定要比較的字段的第一個(gè),第二個(gè)等。單個(gè)字段可以指定為字符串,并且不需要指定所有字段,但仍將使用未指定的字段, 按照他們?cè)赿type中出現(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.沿指定的“軸”對(duì)“ a”進(jìn)行排序的索引數(shù)組。如果“ a”是一維的,則“ a [index_array]”會(huì)產(chǎn)生一個(gè)排序的“ a”。更一般而言,“ np.take_along_axis(a,index_array,axis = axis)”始終會(huì)產(chǎn)生排序的“ a”,而與維數(shù)無(wú)關(guān)。See Also--------sort : Describes sorting algorithms used.描述使用的排序算法。lexsort : Indirect stable sort with multiple keys.具有多個(gè)鍵的間接穩(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)不同排序算法的說(shuō)明,請(qǐng)參見(jiàn)`sort`。從NumPy 1.4.0開(kāi)始,“ argsort”可用于包含以下內(nèi)容的實(shí)/復(fù)雜數(shù)組nan值。 增強(qiáng)的排序順序在`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)定排序)(具體沒(méi)太搞懂區(qū)別?)
總結(jié)
以上是生活随笔為你收集整理的python numpy np.argsort()(返回将对数组进行排序的索引)(不懂区别?)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python numpy np.lexs
- 下一篇: python matplotlib.py