python numpy np.argsort()(返回将对数组进行排序的索引)(不懂区别?)

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.
    返回将对数组进行排序的索引。

    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”关键字指定的算法沿给定的轴执行间接排序。 它返回一个与`a'形状相同的索引数组,该索引数组沿给定轴按排序顺序对数据进行索引。

    Parameters
    ----------
    a : array_like
        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.
        要排序的轴。 默认值为-1(最后一个轴)。 如果为None,则使用扁平化的数组。
    kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, optional
        Sorting 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.
        排序算法。 默认值为“快速排序”。 请注意,“稳定”和“合并排序”在后台都使用timsort,并且通常,实际实现会随数据类型而变化。 保留'mergesort'选项是为了向后兼容。

        .. versionchanged:: 1.15.0.
           The 'stable' option was added.
    order : str or list of str, optional
        When `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.
        当“ a”是定义了字段的数组时,此参数指定要比较的字段的第一个,第二个等。单个字段可以指定为字符串,并且不需要指定所有字段,但仍将使用未指定的字段, 按照他们在dtype中出现的顺序,打破关系。

    Returns
    -------
    index_array : ndarray, int
        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.
        沿指定的“轴”对“ a”进行排序的索引数组。
         如果“ a”是一维的,则“ a [index_array]”会产生一个排序的“ a”。
         更一般而言,“ np.take_along_axis(a,index_array,axis = axis)”始终会产生排序的“ a”,而与维数无关。

    See Also
    --------
    sort : Describes sorting algorithms used.
    描述使用的排序算法。
    lexsort : Indirect stable sort with multiple keys.
    具有多个键的间接稳定排序。
    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应用于数组,就好像调用sort一样。

    Notes
    -----
    See `sort` for notes on the different sorting algorithms.

    As of NumPy 1.4.0 `argsort` works with real/complex arrays containing
    nan values. The enhanced sort order is documented in `sort`.
    
    有关不同排序算法的说明,请参见`sort`。

     从NumPy 1.4.0开始,“ argsort”可用于包含以下内容的实/复杂数组
     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]])
    >>> 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])

    Sorting with keys:

    >>> x = np.array([(1, 0), (0, 1)], dtype=[('x', '<i4'), ('y', '<i4')])
    >>> x
    array([(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()(使用键序列执行间接稳定排序)(具体没太搞懂区别?)

发布了857 篇原创文章 · 获赞 49 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/Dontla/article/details/104490238