python - np.partition() 如何解释参数 kth?

标签 python arrays sorting numpy

我想弄清楚 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])

我希望在分区之后,数组将拆分为小于一个一个大于一个 的元素。 但是第二个零放在最后一个数组位置,分区后不是正确的位置。

最佳答案

documentation说:

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/

相关文章:

javascript - 根据时间获取最近的物体

javascript - 用引号在javascript中对数组进行排序

python - 尝试使用 python 和 openimageIO 提取子图像失败

python - 使用 Python 的 panda.Dataframe 对多传感器时间序列数据进行子采样

javascript - 将标记从数组添加到带有图层支持的传单中的标记簇中

python - 二维随机游走,Python

python - 对给定列表顺序的数据框进行排序 Python

python - 在 Python 中使用 Selenium 单击下拉菜单

javascript - 用于 JavaScript 的 Python 装饰器

c++ - 类 C++ 黑白棋程序的问题