def replace_runs_bis(arr, search=1, n=3, val=2):
    ret = np.array(arr)
rows, cols = arr.shape
arr_cum = np.cumsum(arr == search, axis=1)
arr_win = np.empty((rows, cols - n + 1), dtype=np.intp)
arr_win[:, (0)] = arr_cum[:, (n - 1)]
arr_win[:, 1:] = arr_cum[:, n:] - arr_cum[:, :-n]
mask_win = arr_win >= n
mask = np.zeros_like(arr, dtype=np.bool)
for j in range(n):
    sl_end = -n + j + 1
sl_end = sl_end if sl_end else None
mask[:, j:sl_end] |= mask_win
ret[mask] = val
return ret
Removing runs from a 2D numpy array