如何使用指数平滑来平滑python中的时间序列?
我试图使用指数平滑来平滑时间序列 假设我的时间序列如下所示:如何使用指数平滑来平滑python中的时间序列?,python,machine-learning,time-series,signal-processing,forecasting,Python,Machine Learning,Time Series,Signal Processing,Forecasting,我试图使用指数平滑来平滑时间序列 假设我的时间序列如下所示: import pandas as pd data = [446.6565, 454.4733, 455.663 , 423.6322, 456.2713, 440.5881, 425.3325, 485.1494, 506.0482, 526.792 , 514.2689, 494.211 ] index= pd.date_range(start='1996', end='2008', freq='A') oil
import pandas as pd
data = [446.6565, 454.4733, 455.663 , 423.6322, 456.2713, 440.5881, 425.3325, 485.1494, 506.0482, 526.792 , 514.2689, 494.211 ]
index= pd.date_range(start='1996', end='2008', freq='A')
oildata = pd.Series(data, index)
我想得到timeseries的平滑版本
如果我做了这样的事
from statsmodels.tsa.api import ExponentialSmoothing
fit1 = SimpleExpSmoothing(oildata).fit(smoothing_level=0.2,optimized=False)
fcast1 = fit1.forecast(3).rename(r'$\alpha=0.2$')
它只输出预测的三个值,但不输出原始timeseries的平滑版本。有没有办法获得我最初的timeseries的平滑版本
如果需要,我很乐意提供更多细节。不是用来平滑时间序列数据的工具,而是一种时间序列预测方法
函数的作用是:返回HoltWintersResults类的一个实例,该类包含已学习的系数。可以调用result对象上的forecast()或predict()函数进行预测
因此,通过调用predict
,该类将使用学习的系数提供预测
但是,为了平滑时间序列,可以使用@smarie指出的fittedvalues
属性
但是,我会使用一个更合适的工具,比如
savgol\u过滤器
:
from scipy.signal import savgol_filter
savgol_filter(oildata, 5, 3)
array([444.87816 , 461.58666 , 444.99296 , 441.70785143,
442.40769143, 438.36852857, 441.50125714, 472.05622571,
512.20891429, 521.74822857, 517.63141429, 493.37037143])
如注释中所述,savgol过滤器对给定的窗口大小(窗口长度
)执行给定的多阶
的局部泰勒近似,并导致时间序列的平滑
以下是使用上述设置时的情况:
plt.plot(oildata)
plt.plot(pd.Series(savgol_filter(oildata, 5, 3), index=oildata.index))
plt.show()
不是平滑时间序列数据的工具,而是一种时间序列预测方法
函数的作用是:返回HoltWintersResults类的一个实例,该类包含已学习的系数。可以调用result对象上的forecast()或predict()函数进行预测
因此,通过调用predict
,该类将使用学习的系数提供预测
但是,为了平滑时间序列,可以使用@smarie指出的fittedvalues
属性
但是,我会使用一个更合适的工具,比如
savgol\u过滤器
:
from scipy.signal import savgol_filter
savgol_filter(oildata, 5, 3)
array([444.87816 , 461.58666 , 444.99296 , 441.70785143,
442.40769143, 438.36852857, 441.50125714, 472.05622571,
512.20891429, 521.74822857, 517.63141429, 493.37037143])
如注释中所述,savgol过滤器对给定的窗口大小(窗口长度
)执行给定的多阶
的局部泰勒近似,并导致时间序列的平滑
以下是使用上述设置时的情况:
plt.plot(oildata)
plt.plot(pd.Series(savgol_filter(oildata, 5, 3), index=oildata.index))
plt.show()
显然,您可以在模型的
fittedvalues
属性中获得平滑值
import pandas as pd
data = [446.6565, 454.4733, 455.663 , 423.6322, 456.2713, 440.5881, 425.3325, 485.1494, 506.0482, 526.792 , 514.2689, 494.211 ]
index= pd.date_range(start='1996', end='2008', freq='A')
oildata = pd.Series(data, index)
from statsmodels.tsa.api import SimpleExpSmoothing
fit1 = SimpleExpSmoothing(oildata).fit(smoothing_level=0.2,optimized=False)
# fcast1 = fit1.forecast(3).rename(r'$\alpha=0.2$')
import matplotlib.pyplot as plt
plt.plot(oildata)
plt.plot(fit1.fittedvalues)
plt.show()
它产生:
各国:
fittedvalues:ndarray
拟合值的数组。采用指数平滑模型拟合
请注意,您还可以使用包含所有值+第一个预测的
fittedfcast
属性,或仅包含预测的fcastvalues
属性。显然,您可以在模型的fittedvalues
属性中获得平滑值
import pandas as pd
data = [446.6565, 454.4733, 455.663 , 423.6322, 456.2713, 440.5881, 425.3325, 485.1494, 506.0482, 526.792 , 514.2689, 494.211 ]
index= pd.date_range(start='1996', end='2008', freq='A')
oildata = pd.Series(data, index)
from statsmodels.tsa.api import SimpleExpSmoothing
fit1 = SimpleExpSmoothing(oildata).fit(smoothing_level=0.2,optimized=False)
# fcast1 = fit1.forecast(3).rename(r'$\alpha=0.2$')
import matplotlib.pyplot as plt
plt.plot(oildata)
plt.plot(fit1.fittedvalues)
plt.show()
它产生:
各国:
fittedvalues:ndarray
拟合值的数组。采用指数平滑模型拟合
请注意,您也可以使用包含所有值+第一个预测的
fittedfcast
属性,或仅包含预测的fcastvalues
属性。非常感谢。拟合值是什么意思?它是timeseries的平滑版本吗?:)这就是我强烈的想法,特别是第一点是一样的,系列长度也是一样的。同时编辑这不是一个数组,而是一个系列。显然,索引是相同的-我包含了一个链接,指向fit
对象类型的文档,这是一个HoltWintersResults
。拟合值是基于拟合参数的模型样本内预测。非常感谢。拟合值是什么意思?它是timeseries的平滑版本吗?:)这就是我强烈的想法,特别是第一点是一样的,系列长度也是一样的。同时编辑这不是一个数组,而是一个系列。显然,索引是相同的-我包含了一个链接,指向fit
对象类型的文档,这是一个HoltWintersResults
。拟合值是基于拟合参数的模型样本内预测。非常感谢。我喜欢你建议使用savgol\u过滤器
。你知道savgol_过滤器和移动平均之间的区别吗?:)我记忆中的Savgol过滤器在给定的窗口大小上执行局部泰勒近似,并导致函数@emjwheals的平滑,移动平均值只是一个平均值,正如它的名字所说的@emjThanks。我发现你在savgol filter中发布的详细信息非常有用。谢谢:)等我有时间再看一看:)@EmJthanks很多。我喜欢你建议使用savgol\u过滤器
。你知道savgol_过滤器和移动平均之间的区别吗?:)我记忆中的Savgol过滤器在给定的窗口大小上执行局部泰勒近似,并导致函数@emjwheals的平滑,移动平均值只是一个平均值,正如它的名字所说的@emjThanks。我发现你在savgol filter中发布的详细信息非常有用。谢谢:)等我有时间再看:)@EmJ