I have array:
arr = np.array([1,2,3,2,3,4,3,2,1,2,3,1,2,3,2,2,3,4,2,1])
print (arr)
[1 2 3 2 3 4 3 2 1 2 3 1 2 3 2 2 3 4 2 1]
I would like find this pattern and return booelan mask:
pat = [1,2,3]
N = len(pat)
I use strides
:
#https://stackoverflow.com/q/7100242/2901002
def rolling_window(a, window):
shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
strides = a.strides + (a.strides[-1],)
c = np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
return c
print (rolling_window(arr, N))
[[1 2 3]
[2 3 2]
[3 2 3]
[2 3 4]
[3 4 3]
[4 3 2]
[3 2 1]
[2 1 2]
[1 2 3]
[2 3 1]
[3 1 2]
[1 2 3]
[2 3 2]
[3 2 2]
[2 2 3]
[2 3 4]
[3 4 2]
[4 2 1]]
I find positions of first values only:
b = np.all(rolling_window(arr, N) == pat, axis=1)
c = np.mgrid[0:len(b)][b]
print (c)
[ 0 8 11]
And positions another vals:
d = [i for x in c for i in range(x, x+N)]
print (d)
[0, 1, 2, 8, 9, 10, 11, 12, 13]
Last return mask by in1d
:
e = np.in1d(np.arange(len(arr)), d)
print (e)
[ True True True False False False False False True True
True True True True False False False False False False]
Verify mask:
print (np.vstack((arr, e)))
[[1 2 3 2 3 4 3 2 1 2 3 1 2 3 2 2 3 4 2 1]
[1 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0]]
1 2 3 1 2 3 1 2 3
I think my solution is a bit over-complicated. Is there some better, more pythonic solution?
See Question&Answers more detail:
os