Подход № 1
Мы можем использовать 1D
свертка -
def nzeros(a, n):
# Define kernel for 1D convolution
k = np.ones(n,dtype=int)
# Get sliding summations for zero matches with that kernel
s = np.convolve(a==0,k)
# Look for summations that are equal to n value, which will occur for
# n consecutive 0s. Remember that we are using a "full" version of
# convolution, so there's one-off offsetting because of the way kernel
# slides across input data. Also, we need to create 1s at places where
# n consective 0s end, so we would need to slice out ending elements.
# Thus, we would end up with the following after int dtype conversion
return (s==n).astype(int)[:-n+1]
Пробный прогон -
In [46]: a
Out[46]: array([0, 0, 0, 0, 1, 0, 0, 0, 1, 1])
In [47]: nzeros(a,3)
Out[47]: array([0, 0, 1, 1, 0, 0, 0, 1, 0, 0])
In [48]: nzeros(a,2)
Out[48]: array([0, 1, 1, 1, 0, 0, 1, 1, 0, 0])
Подход № 2
Еще один способ решения проблемы, который можно рассматривать как вариант подхода свертки 1D
, заключается в использовании erosion
, потому что если вы посмотритена выходах мы можем просто разрушить маску 0s
от стартов до n-1
мест.Таким образом, мы можем использовать scipy.ndimage.morphology's
binary_erosion
, что также позволяет нам указывать часть центра ядра с его аргументом origin
, поэтому мы будем избегать нарезки.Реализация будет выглядеть примерно так -
from scipy.ndimage.morphology import binary_erosion
out = binary_erosion(a==0,np.ones(n),origin=(n-1)//2).astype(int)