例如,我有一组点(x,y)作为两个向量x,y。
from pylab import *
x = sorted(random(30))
y = random(30)
plot(x,y, 'o-')
现在我想用一个高斯来平滑这些数据,并且只在x轴上的某些点(有规律的间隔)上进行评估,比如说:
x_eval = linspace(0,1,11)
我得到的提示是这种方法被称为 “高斯和滤波器”,但到目前为止,我还没有在numpyscipy中找到任何实现,尽管它乍一看像是一个标准问题.由于x值不是等距的,我不能使用scipy.ndimage.gaussian_filter1d.
通常这种平滑是通过furrier空间并与内核相乘来完成的,但我真的不知道这对不规则间距的数据是否可行。
谢谢你的任何想法
解决方案:
这对于非常大的数据集来说是个大问题,但是你所要求的正确计算方法是这样的。
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0) # for repeatability
x = np.random.rand(30)
x.sort()
y = np.random.rand(30)
x_eval = np.linspace(0, 1, 11)
sigma = 0.1
delta_x = x_eval[:, None] - x
weights = np.exp(-delta_x*delta_x / (2*sigma*sigma)) / (np.sqrt(2*np.pi) * sigma)
weights /= np.sum(weights, axis=1, keepdims=True)
y_eval = np.dot(weights, y)
plt.plot(x, y, 'bo-')
plt.plot(x_eval, y_eval, 'ro-')
plt.show()