Numpy中的argpartition()方法

每天都有新发现,每天都要新积累

贴个源码里的解释然后给出翻译哈哈哈

@array_function_dispatch(_argpartition_dispatcher)
def argpartition(a, kth, axis=-1, kind='introselect', order=None):
    """
    Perform an indirect partition 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 partitioned order.
	方法沿给定轴执行间接分区由' kind '关键字指定的算法。它返回一个与“a”形状相同的索引数组,即沿着给定的轴按分区顺序排列索引数据。
    .. versionadded:: 1.8.0  Numpy1.8版本新添加的函数

    Parameters函数参数配置
    ----------
    a : array_like 第一个参数理所当然是数组类型参数
        Array to sort.
    kth : int or sequence of ints 这个参数表示要按第几个元素排序,告诉函数这个元素的索引
        Element index to partition by. The k-th element will be in its
        final sorted position and all smaller elements will be moved
        before it and all larger elements behind it. The order all
        elements in the partitions is undefined. If provided with a
        sequence of k-th it will partition all of them into their sorted
        position at once.
    axis : int or None, optional  指定要排序的轴,如果不指定,默认为-1,也就是最后一个轴。当指定为None的时候就会输出一个平铺的索引数组,也就是一维数组,即无视掉原数组的形状。以二维数组为例:当指定为0的时候,也就是同列不同行的元素的索引进行排序,当指定为1的时候,是同行不同列的元素根据元素大小返回排好序的索引数组
        Axis along which to sort. The default is -1 (the last axis). If
        None, the flattened array is used.
    kind : {'introselect'}, optional 指定要用来排序的算法,默认introselect
        Selection algorithm. Default is 'introselect'
    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”是一个定义了字段的数组时,这个参数指定要比较第一个、第二个等的字段。一个单一的字段可以指定为字符串,而不是所有字段都需要指定为字符串,但仍将使用未指定的字段它们在d型中出现的顺序,即断开连接。

    Returns 返回一个排序好的索引数组
    -------
    index_array : ndarray, int
        Array of indices that partition `a` along the specified axis.
        If `a` is one-dimensional, ``a[index_array]`` yields a partitioned `a`.
        More generally, ``np.take_along_axis(a, index_array, axis=a)`` always
        yields the partitioned `a`, irrespective of dimensionality.
	即:np.take_along_axis(a, index_array, axis=a) 适用于任何维度的数组,返回的是排好序的数组a
		a[index_array] 适用于一维数组,返回的是按索引数组排好序的数组a
		效果等价于np.argpartition()
    See Also
    --------
    partition : Describes partition +--algorithms used.
    ndarray.partition : Inplace partition.
    argsort : Full indirect sort

    Notes
    -----
    See `partition` for notes on the different selection algorithms.

    Examples
    --------
    One dimensional array:

    >>> x = np.array([3, 4, 2, 1])
    >>> x[np.argpartition(x, 3)]
    array([2, 1, 3, 4])
    >>> x[np.argpartition(x, (1, 3))]
    array([1, 2, 3, 4])

    >>> x = [3, 4, 2, 1]
    >>> np.array(x)[np.argpartition(x, 3)]
    array([2, 1, 3, 4])
	
	>>> import numpy as np
	>>> a = np.array([[1,2,3,4,5],[10,1,8,7,6]])
	>>> np.argpartition(a,3,None)
	array([0, 6, 1, 2, 3, 4, 5, 7, 8, 9], dtype=int64)
    """
    return _wrapfunc(a, 'argpartition', kth, axis=axis, kind=kind, order=order)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章