我有一个数组,我想在不改变边值的情况下使用Scipy的gaussian_filter1d应用1d高斯滤波器:
>>> from scipy.ndimage.filters import gaussian_filter1d
>>> arr = [1.,2.,3.,4.,100.,5.,6.,7.,8.,9.,10.]
然而,这无法保留边缘值1和10:
>>> gaussian_filter1d(arr, 5, mode="nearest")
>>> array([ 8.26708045, 9.54770677, 10.72077793, 11.70511557,
12.43900171, 12.89021341, 13.06040738, 12.98285813,
12.71434815, 12.32350223, 11.87856682])
答案 0 :(得分:0)
这种方法似乎不必要地复杂化,但是这会使梯度平滑而不是值本身:
# Convolve with sobel filter
grad = convolve(arr, [1,-1,0], mode="nearest")[:-1]
# Smooth gradient
smooth_grad = smooth_grad = gaussian_filter1d(grad, 5)
# Integrate
smoothed = [arr[0] + sum(smooth_grad[:x]) for x in range(len(arr))]
产:
[1.0, 3.6, 6.0, 9.6, 10.7, 11.3, 11.5,11.2,10.7,10.0]