scipy.ndimage.generic_filter1d 不工作

scipy.ndimage.generic_filter1d not working

我第一次尝试使用 scipy.ndimage.generic_filter1d,但并不顺利。这就是我正在尝试的

import numpy as np
from scipy.ndimage import generic_filter1d

def test(x):
    return x.sum()

im_cube = np.zeros((100,100,100))
calc = generic_filter1d(im_cube, test, filter_size=5, axis=2)

但是我得到这个错误:

TypeError: test() takes 1 positional argument but 2 were given

我正在使用 scipy 1.4.1 我做错了什么?

对于这个功能,我也尝试了 np.mean 但后来我得到了这个:

TypeError: only integer scalar arrays can be converted to a scalar index

根据 documentation,回调接受两个参数:对输入行的引用和对输出行的引用。它不会 return 输出,而是就地修改提供的缓冲区。

如果您想实施滚动总和过滤器,您需要手动进行一些操作。例如:

def test(x, out)
    out[:] = np.lib.stride_tricks.as_strided(x, strides=x.strides * 2, shape=(5, x.size - 4)).sum(axis=0)

要使其成为平均值,请在末尾添加 / 5

genetic_filter1d的目的是格式化边,运行外环。它实际上并没有为您实现滚动过滤器。您仍然需要自己实现整个内部循环。