[Numpy-discussion] how to do this efficiently?
Wed Feb 9 12:56:43 CST 2011
On Wed, Feb 9, 2011 at 1:18 PM, Zachary Pincus <firstname.lastname@example.org> 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
array([ 0, 9, 16, 21, 24, 25, 24, 21, 16, 9])
>>> np.arange(x2.shape)[x2 >= T][-1]+1
>>> len(x)-np.argmax(x2[::]>T) + 1
> NumPy-Discussion mailing list
More information about the NumPy-Discussion