gpt4 book ai didi

python - 使用局部加权回归 (LOESS/LOWESS) 预测新数据

转载 作者:太空狗 更新时间:2023-10-29 17:31:30 24 4
gpt4 key购买 nike

如何在 python 中拟合局部加权回归,以便它可以用于预测新数据?

statsmodels.nonparametric.smoothers_lowess.lowess,但它只返回原始数据集的估计;所以它似乎只能将 fitpredict 放在一起,而不是像我预期的那样分开。

scikit-learn 总是有一个 fit 方法,允许对象稍后在带有 predict 的新数据上使用;但它没有实现 lowess

最佳答案

Lowess 非常适合预测(与插值相结合时)!我认为代码非常简单——如果您有任何问题,请告诉我! Matplolib Figure

import matplotlib.pyplot as plt
%matplotlib inline
from scipy.interpolate import interp1d
import statsmodels.api as sm

# introduce some floats in our x-values
x = list(range(3, 33)) + [3.2, 6.2]
y = [1,2,1,2,1,1,3,4,5,4,5,6,5,6,7,8,9,10,11,11,12,11,11,10,12,11,11,10,9,8,2,13]

# lowess will return our "smoothed" data with a y value for at every x-value
lowess = sm.nonparametric.lowess(y, x, frac=.3)

# unpack the lowess smoothed points to their values
lowess_x = list(zip(*lowess))[0]
lowess_y = list(zip(*lowess))[1]

# run scipy's interpolation. There is also extrapolation I believe
f = interp1d(lowess_x, lowess_y, bounds_error=False)

xnew = [i/10. for i in range(400)]

# this this generate y values for our xvalues by our interpolator
# it will MISS values outsite of the x window (less than 3, greater than 33)
# There might be a better approach, but you can run a for loop
#and if the value is out of the range, use f(min(lowess_x)) or f(max(lowess_x))
ynew = f(xnew)


plt.plot(x, y, 'o')
plt.plot(lowess_x, lowess_y, '*')
plt.plot(xnew, ynew, '-')
plt.show()

关于python - 使用局部加权回归 (LOESS/LOWESS) 预测新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36252434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com