如何在python中使用条件过滤numpy数组 [英] How to filter a numpy array using a condition in python
本文介绍了如何在python中使用条件过滤numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在如下使用我的numpy数组v
来删除< = 1的元素,然后选择numpy数组中前3个元素的索引.
I am using my numpy array v
as follows to remove elements that are <=1 and then select the indexes of the top 3 elements in the numpy array.
for ele in v.toarray()[0].tolist():
if ele <= 1:
useless_index = v.toarray()[0].tolist().index(ele)
temp_list.append(useless_index)
#take top 3 words from each document
indexes =v.toarray()[0].argsort()[-3:]
useful_list = list(set(indexes) - set(temp_list))
但是,我正在使用的当前代码非常慢(因为我有数百万个numpy数组)并且需要几天的时间才能运行.有什么有效的方法可以在python中做同样的事情?
However, the current code I am using is very slow (as I have millions of numpy arrays) and take days to run. Is there any efficient way of doing the same thing in python?
推荐答案
v = v[v > 1]
indices = np.argpartition(v, -3)[-3:]
values = v[indices]
如此处所述,argpartition
在O(n + k log k)
时间运行.您的情况是n = 1e6
,k=3
.
As mentioned here, argpartition
runs in O(n + k log k)
time. In your case, n = 1e6
, k=3
.
这篇关于如何在python中使用条件过滤numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文