[Numpy-discussion] how to do this efficiently?

Alan G Isaac alan.isaac at gmail.com
Wed Feb 9 12:17:45 EST 2011


On 2/9/2011 11:39 AM, Bruce Southey wrote:
> np.argmax(x>5) # doesn't appear to be correct


It was an answer to the particular question
of how to do find_first, which it does
(at the cost of a boolean array):
it finds the first element greater than 5.

     >>> x
     array([5, 4, 3, 6, 7, 3, 2, 1])
     >>> np.argmax(x>5)
     3
     >>> x[3]
     6

You can also find the first element less than 4, etc.

     >>> np.argmax(x<4)
     2

The condition should be chosen for the problem.
As Josef pointed out, the usefulness
of this and other approaches
can depend on the monotonicity
of the series.

fwiw,
Alan




More information about the NumPy-Discussion mailing list