Creates a copy of the array with its elements rearranged in such a way that
the value of the element in kth position is in the position it would be in
a sorted array. All elements smaller than the kth element are moved before
this element and all equal or greater are moved behind it. The ordering of
the elements in the two partitions is undefined.
documentation上写着:
在您给出的示例中,您选择了已排序列表的第2个元素(从零开始),即-1,如果对数组进行排序,它似乎位于正确的位置。在
np.partition
首先对提供的数组中的元素进行排序。在这种情况下,原始数组是:分类后,我们有:
^{pr2}$因此,调用
np.partition(arr, kth=2)
,实际上将kth
作为arr_sorted
的2
位置的元素,而不是arr
。元素被正确地选为-1
。在相关问题 更多 >
编程相关推荐