按模式查找布尔掩码

jez*_*ael 5 python arrays boolean numpy stride

我有阵列:

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]
Run Code Online (Sandbox Code Playgroud)

我想找到这个模式并返回booelan mask:

pat = [1,2,3]
N = len(pat)
Run Code Online (Sandbox Code Playgroud)

我用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]]
Run Code Online (Sandbox Code Playgroud)

我只找到第一个值的位置:

b = np.all(rolling_window(arr, N) == pat, axis=1)
c = np.mgrid[0:len(b)][b]
print (c)
[ 0  8 11]
Run Code Online (Sandbox Code Playgroud)

并定位另一个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]
Run Code Online (Sandbox Code Playgroud)

最后返回掩码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]
Run Code Online (Sandbox Code Playgroud)

验证面具:

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   
Run Code Online (Sandbox Code Playgroud)

我认为我的解决方案有点过于复杂.有没有更好,更pythonic的解决方案?

Div*_*kar 3

最后我们可以使用 Scipy 支持的二进制膨胀来简化事情 -

from scipy.ndimage.morphology import binary_dilation

m = (rolling_window(arr, len(pat)) == pat).all(1)
m_ext = np.r_[m,np.zeros(len(arr) - len(m), dtype=bool)]
out = binary_dilation(m_ext, structure=[1]*N, origin=-(N//2))
Run Code Online (Sandbox Code Playgroud)

为了提高性能,我们可以引入 OpenCV 及其模板匹配功能,因为我们在这里基本上做同样的事情,就像这样 -

import cv2

tol = 1e-5
pat_arr = np.asarray(pat, dtype='uint8')
m = (cv2.matchTemplate(arr.astype('uint8'),pat_arr,cv2.TM_SQDIFF) < tol).ravel()
Run Code Online (Sandbox Code Playgroud)