[Numpy-discussion] Find groups of true values or sequence of values
Thu Jun 19 22:42:08 CDT 2008
2008/6/18 bevan <firstname.lastname@example.org>:
> I am looking for some pointers that will hopefully get me a round an issue I
> have hit.
> I have a timeseries of river flow and would like to carry out some analysis on
> the recession periods. That is anytime the values are decreasing. I would
> like to restrict (mask) my data to any values that are in a continuous sequence
> of 3 or more (in the example below), that are decreasing in value.
> Hopefully this example helps:
> import numpy as np
> Flow = np.array([15.4,20.5,19.4,18.7,18.6,35.5,34.8,25.1,26.7])
> FlowDiff = np.diff(Flow)
> boolFlowdiff = FlowDiff>0
> MaskFlow = np.ma.masked_array(Flow[1:],boolFlowdiff)
> print MaskFlow
> [-- 19.4 18.7 18.6 -- 34.8 25.1 --]
> The output I would like is
> [-- 19.4 18.7 18.6 -- -- -- --]
> Where the second groups is blanked because the sequence only has 2 members.
I would tackle this in steps: find the decreasing pairs, then find
places where two of them occur in a row, then construct your mask.
d = diff(X)<0
finding two decreases in a row:
t = d[1:] & d[:-1]
creating the right mask:
m = np.zeros(n,dtype=np.bool)
m[:n-2] |= t
m[1:n-1] |= t
m[2:n] |= t
For finding longer runs you would want other tricks.
More information about the Numpy-discussion