使用 Numpy.argpartition 按排序顺序获取每列的 k 最小值 [英] Geting the k-smallest values of each column in sorted order using Numpy.argpartition

查看:56
本文介绍了使用 Numpy.argpartition 按排序顺序获取每列的 k 最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 np.argpartition,它不会对整个数组进行排序.它只保证第 k 个元素处于排序位置,并且所有较小的元素都将移动到它之前.因此,前 k 个元素将是 k 个最小的元素

<预><代码>>>>数量 = 3>>>myBigArray=np.array([[1,3,2,5,7,0],[14,15,6,5,7,0],[17,8,9,5,7,0]])>>>top = np.argpartition(myBigArray, num, axis=1)[:, :num]>>>印花上衣[[5 0 2][3 5 2][5 3 4]]>>>myBigArray[np.arange(myBigArray.shape[0])[:, None], top][[0 1 2][5 0 6][0 5 7]]

这将返回每列的 k 最小值.请注意,这些可能不是按顺序排列的.我使用这种方法是因为要以这种方式按排序顺序获取前 k 个元素需要 O(n + k log k) 时间我想在不增加时间复杂度的情况下按排序顺序获得每列的 k 最小值.有什么建议么??

解决方案

使用 np.argpartition 并保持排序的顺序,我们需要使用这些元素范围作为 range(k) 而不是仅仅输入标量 kth 参数 -

idx = np.argpartition(myBigArray, range(num), axis=1)[:, :num]out = myBigArray[np.arange(idx.shape[0])[:,None], idx]

Using np.argpartition, it does not sort the entire array. It only guarantees that the kth element is in sorted position and all smaller elements will be moved before it. Thus, the first k elements will be the k-smallest elements

>>> num = 3
>>> myBigArray=np.array([[1,3,2,5,7,0],[14,15,6,5,7,0],[17,8,9,5,7,0]])
>>> top = np.argpartition(myBigArray, num, axis=1)[:, :num]
>>> print top
[[5 0 2]
[3 5 2]
[5 3 4]]
>>> myBigArray[np.arange(myBigArray.shape[0])[:, None], top]
[[0 1 2]
[5 0 6]
[0 5 7]]

This returns the k-smallest values of each column. Note that these may not be in sorted order.I use this method because To get the top-k elements in sorted order in this way takes O(n + k log k) time I want to get the k-smallest values of each column in sorted order, without increasing the time complexity. Any suggestions??

解决方案

To use np.argpartition and maintain the sorted order, we need to use those range of elements as range(k) instead of feeding in just the scalar kth param -

idx = np.argpartition(myBigArray, range(num), axis=1)[:, :num]
out = myBigArray[np.arange(idx.shape[0])[:,None], idx]

这篇关于使用 Numpy.argpartition 按排序顺序获取每列的 k 最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆