I have an 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 to find this template and return booelan mask:
pat = [1,2,3] N = len(pat)
I am using strides :
#https:
I find only the positions of the first values:
b = np.all(rolling_window(arr, N) == pat, axis=1) c = np.mgrid[0:len(b)][b] print (c) [ 0 8 11]
And positioned 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 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]
Check 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 decision is a bit more complicated. Is there an even better, more pythonic solution?