0

I have a 1D numpy array of numbers. I want to find the largest k elements in that array along with their indices. How can I do that with numpy? If this is not possible to do with numpy, then what's the most efficient way to do it?

Jack Twain
  • 6,273
  • 15
  • 67
  • 107
  • Indeed very probably a duplicate of that question. The solution is to use `numpy.argsort(arr)[::-1][:k]`, which yields the required indices. – eickenberg May 19 '14 at 09:30
  • 1
    @eickenberg: or `argpartition` (see answer that I just posted under the other question). – Fred Foo May 19 '14 at 09:33
  • Nice. And with that it works in linear time then. This business of sorting the whole array has always bugged me, but I have never been in the situation where the efficacy of this operation was crucial, so I never looked further. Thanks for this. – eickenberg May 19 '14 at 11:12

0 Answers0