0

Suppose I've got an array a,

a = np.array([50,49,47,55,68,70,65])

And I want the index of the first value that is greater than 65 (i.e.4). What is the fastest way I can do this task in Numpy (since my actual dataset is much larger)?

2 Answers2

0

argmax will find the index of first True, try this np.argmax(a>65)

Lucien
  • 118
  • 1
  • 6
0

I would suggest using numba and it's decorators. Thanks to this library you can achieve better performance scores than numpy (or on par) while using plain Python.

In your case it could be:

import numba

@numba.njit
def find_greater(array, threshold: int):
    for i in range(len(array)):
        if array[i] > threshold:
            return i

This option is better in terms of time complexity, maximum O(n) (usually much less) than approaches like the one suggested in comments.

Szymon Maszke
  • 22,747
  • 4
  • 43
  • 83