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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章