def replace_runs(a, N, replace=2):
    a_copy = a.copy()
pattern = np.ones(N, dtype=int)
M = a_copy.shape[1]
for i, row in enumerate(a_copy):
    conv = np.convolve(row, pattern, mode='same')
match = np.where(conv == N)
a_copy[i][match] = replace
a_copy[i][match[0][match[0] - 1 > 0] - 1] = replace
a_copy[i][match[0][match[0] + 1 < M] + 1] = replace
return a_copy
Removing runs from a 2D numpy array