我想弄清楚 np.partition
函数是如何工作的。
例如,考虑
arr = np.array([5, 4, 1, 0, -1, -3, -4, 0])
如果我调用 np.partition(arr, kth=2)
,我会得到
np.array([-4, -3, -1, 0, 1, 4, 5, 0])
我希望在分区之后,数组将拆分为小于一个、一个 和大于一个 的元素。 但是第二个零放在最后一个数组位置,分区后不是正确的位置。
最佳答案
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.
在您给出的示例中,您选择了排序列表的第 2 个元素(从零开始),即 -1,如果数组已排序,它似乎位于正确的位置。
关于python - np.partition() 如何解释参数 kth?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41484104/