0

I would like to identify state transitions in a data set. An example is: "At which index does the sin(x) fall below 0.5?"

The only way I could think of is like this:

a = np.arange(0,10,.01)
b= np.sin(a)
c = np.roll(b,1)
c[:1] = 0
print(a[(.5 > b) & (.5< c)])
[2.62 8.91]

Can I do that without an additional array c? How can I detect cases like "At which index does sin(x) fall below 0.5 and stay there for 7 samples?" Would i need 7 additional arrays?

1 Answer 1

1

For 7 steps, you can just build an intermediate array:

mask = (b < 0.5)
window = 7

# count the number of times the value is below thresh in the window
below_thresh = np.sum([mask[i:len(mask)-window+i] for i in range(window)], axis=0)

mask1 = below_thresh == window

a[window + 1:][mask1[1:] & (~mask1[:-1])]
# out 
# array([2.62, 8.91])

For higher number of steps, you can build the below_thresh faster with as_strided.

Sign up to request clarification or add additional context in comments.

4 Comments

genius! ... and how do I get the respective indices?
the first mask and the second are independent from each other, right?
The indices are where mask[1:] & (~mask[:-1]) is True and shifted by window + 1. So something np.where(mask[1:] & (~mask[:-1])) + window + 1?
@AndreasSchuldei yes, just reuse the same variable to save some memory. See updates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.