[Numpy-discussion] how to do this efficiently?

josef.pktd at gmail.com josef.pktd at gmail.com
Wed Feb 9 13:56:43 EST 2011


On Wed, Feb 9, 2011 at 1:18 PM, Zachary Pincus <zachary.pincus at yale.edu> wrote:
>>
>>>> In a 1-d array, find the first point where all subsequent points
>>>> have values
>>>> less than a threshold.
>>
>> This doesn't imply monotonicity.
>> Suppose with have a sin curve, and I want to find the last trough. Or
>> a business cycle and I want to find the last recession.
>>
>> Unless my english deteriorated recently.
>>
>
> Not sure that I follow? I read the statement as:
> find the smallest non-negative index i such that array[j] < t for all
> i <= j < len(array)
>
> for which the various solutions proposed work, except for the corner
> case where array.min() >= t.
>
> I'm not sure where monotonicity comes into play, unless one interprets
> the "all subsequent points" clause as "some number of subsequent
> points" or something...
>
> For those sort of problems, I usually wind up smoothing the array
> [optional], and then using scipy.ndimage.maximum to calculate peak
> values within a given window, and then find the points that equal the
> peak values -- this works pretty robustly for peak/trough detection in
> arbitrary dimension.

(Sorry, I should reply when I'm not able to actually pay attention)

your code works fine with a hump, I didn't read the example carefully enough

>>> x = np.arange(10,0,-1)
>>> x2 = 25-(x-5)**2
>>> x2
array([ 0,  9, 16, 21, 24, 25, 24, 21, 16,  9])
>>> T=20
>>> np.argmax(x2<T)
0
>>> np.arange(x2.shape[0])[x2 >= T][-1]+1
8
>>> len(x)-np.argmax(x2[::]>T) + 1
8

Josef


> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



More information about the NumPy-Discussion mailing list