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))

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?

3
  • Your indenting is a bit strange Commented Feb 11, 2018 at 23:21
  • What about v[v > 1]? Commented Feb 11, 2018 at 23:24
  • @MateenUlhaq Edited the question. Commented Feb 11, 2018 at 23:28

1 Answer 1

4
v = v[v > 1]
indices = np.argpartition(v, -3)[-3:]
values = v[indices]

As mentioned here, argpartition runs in O(n + k log k) time. In your case, n = 1e6, k=3.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.