как сделать цикл ndarray блоком - PullRequest
0 голосов
/ 03 июля 2019

я хочу сделать фильтрацию на ndarray, как scipy.signal.medfilt

x = np.arange(1, 26).reshape((5, 5))
y = np.copy(x)

kernel_size = 3
radius = kernel_size // 2
for index, val in np.ndenumerate(x):
    top = 0 if index[0] < radius else index[0] - radius
    bottom = x.shape[0] if index[0] + radius >= x.shape[0] \
        else index[0] + radius + 1
    left = 0 if index[1] < radius else index[1] - radius
    right = x.shape[1] if index[1] + radius >= x.shape[1]\
        else index[1] + radius + 1

    print(f"{x[top:bottom, left:right]}")

    y[index] = x[top:bottom, left:right].mean()

итерация может быть векторизована?или какой-нибудь более эффективный код?

...