如何在python中使用lowess平滑具有年度数据的时间序列
我有一些数据每年记录如下如何在python中使用lowess平滑具有年度数据的时间序列,python,machine-learning,time-series,seaborn,smoothing,Python,Machine Learning,Time Series,Seaborn,Smoothing,我有一些数据每年记录如下 mydata = [0.6619346141815186, 0.7170140147209167, 0.692265510559082, 0.6394098401069641, 0.6030995845794678, 0.6500746607780457, 0.6013327240943909, 0.6273292303085327, 0.5865356922149658, 0.6477396488189697, 0.5827181339263916, 0.649602
mydata = [0.6619346141815186, 0.7170140147209167, 0.692265510559082, 0.6394098401069641, 0.6030995845794678, 0.6500746607780457, 0.6013327240943909, 0.6273292303085327, 0.5865356922149658, 0.6477396488189697, 0.5827181339263916, 0.6496025323867798, 0.6589270234107971, 0.5498126149177551, 0.48638370633125305, 0.5367399454116821, 0.517595648765564, 0.5171639919281006, 0.47503289580345154, 0.6081966757774353, 0.5808742046356201, 0.5856912136077881, 0.5608134269714355, 0.6400936841964722, 0.6766082644462585]
corresponding_year = [1970,1971,1972,1973,1974,1975,1976,1977,1978,1979,1980,1981,1982,1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994]]
import statsmodels.api as sm
lowess = sm.nonparametric.lowess
z = lowess(x, y, frac= 1./3, it=3)
[[1.96000000e+03, 6.95703548e-01],
[1.96100000e+03, 6.81750671e-01],
[1.96200000e+03, 6.68002318e-01],
[1.96300000e+03, 6.55138324e-01],
[1.96400000e+03, 6.38960761e-01],
[1.96500000e+03, 6.25042177e-01],
[1.96600000e+03, 6.18586936e-01],
[1.96700000e+03, 6.17026334e-01],
[1.96800000e+03, 6.14565102e-01],
[1.96900000e+03, 6.17610340e-01],
[1.97000000e+03, 6.20404414e-01],
[1.97100000e+03, 6.10193222e-01],
[1.97200000e+03, 5.90100648e-01],
[1.97300000e+03, 5.70935248e-01],
[1.97400000e+03, 5.47818726e-01],
[1.97500000e+03, 5.25788570e-01],
[1.97600000e+03, 5.18661218e-01],
[1.97700000e+03, 5.28921300e-01],
[1.97800000e+03, 5.42783400e-01],
[1.97900000e+03, 5.55425915e-01],
[1.98000000e+03, 5.71486587e-01],
[1.98100000e+03, 5.91539778e-01],
[1.98200000e+03, 6.13021691e-01],
[1.98300000e+03, 6.34508409e-01],
[1.98400000e+03, 6.57703989e-01]]
我使用statsmodels
python包计算lowess,如下所示
mydata = [0.6619346141815186, 0.7170140147209167, 0.692265510559082, 0.6394098401069641, 0.6030995845794678, 0.6500746607780457, 0.6013327240943909, 0.6273292303085327, 0.5865356922149658, 0.6477396488189697, 0.5827181339263916, 0.6496025323867798, 0.6589270234107971, 0.5498126149177551, 0.48638370633125305, 0.5367399454116821, 0.517595648765564, 0.5171639919281006, 0.47503289580345154, 0.6081966757774353, 0.5808742046356201, 0.5856912136077881, 0.5608134269714355, 0.6400936841964722, 0.6766082644462585]
corresponding_year = [1970,1971,1972,1973,1974,1975,1976,1977,1978,1979,1980,1981,1982,1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994]]
import statsmodels.api as sm
lowess = sm.nonparametric.lowess
z = lowess(x, y, frac= 1./3, it=3)
[[1.96000000e+03, 6.95703548e-01],
[1.96100000e+03, 6.81750671e-01],
[1.96200000e+03, 6.68002318e-01],
[1.96300000e+03, 6.55138324e-01],
[1.96400000e+03, 6.38960761e-01],
[1.96500000e+03, 6.25042177e-01],
[1.96600000e+03, 6.18586936e-01],
[1.96700000e+03, 6.17026334e-01],
[1.96800000e+03, 6.14565102e-01],
[1.96900000e+03, 6.17610340e-01],
[1.97000000e+03, 6.20404414e-01],
[1.97100000e+03, 6.10193222e-01],
[1.97200000e+03, 5.90100648e-01],
[1.97300000e+03, 5.70935248e-01],
[1.97400000e+03, 5.47818726e-01],
[1.97500000e+03, 5.25788570e-01],
[1.97600000e+03, 5.18661218e-01],
[1.97700000e+03, 5.28921300e-01],
[1.97800000e+03, 5.42783400e-01],
[1.97900000e+03, 5.55425915e-01],
[1.98000000e+03, 5.71486587e-01],
[1.98100000e+03, 5.91539778e-01],
[1.98200000e+03, 6.13021691e-01],
[1.98300000e+03, 6.34508409e-01],
[1.98400000e+03, 6.57703989e-01]]
我得到的结果如下
mydata = [0.6619346141815186, 0.7170140147209167, 0.692265510559082, 0.6394098401069641, 0.6030995845794678, 0.6500746607780457, 0.6013327240943909, 0.6273292303085327, 0.5865356922149658, 0.6477396488189697, 0.5827181339263916, 0.6496025323867798, 0.6589270234107971, 0.5498126149177551, 0.48638370633125305, 0.5367399454116821, 0.517595648765564, 0.5171639919281006, 0.47503289580345154, 0.6081966757774353, 0.5808742046356201, 0.5856912136077881, 0.5608134269714355, 0.6400936841964722, 0.6766082644462585]
corresponding_year = [1970,1971,1972,1973,1974,1975,1976,1977,1978,1979,1980,1981,1982,1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994]]
import statsmodels.api as sm
lowess = sm.nonparametric.lowess
z = lowess(x, y, frac= 1./3, it=3)
[[1.96000000e+03, 6.95703548e-01],
[1.96100000e+03, 6.81750671e-01],
[1.96200000e+03, 6.68002318e-01],
[1.96300000e+03, 6.55138324e-01],
[1.96400000e+03, 6.38960761e-01],
[1.96500000e+03, 6.25042177e-01],
[1.96600000e+03, 6.18586936e-01],
[1.96700000e+03, 6.17026334e-01],
[1.96800000e+03, 6.14565102e-01],
[1.96900000e+03, 6.17610340e-01],
[1.97000000e+03, 6.20404414e-01],
[1.97100000e+03, 6.10193222e-01],
[1.97200000e+03, 5.90100648e-01],
[1.97300000e+03, 5.70935248e-01],
[1.97400000e+03, 5.47818726e-01],
[1.97500000e+03, 5.25788570e-01],
[1.97600000e+03, 5.18661218e-01],
[1.97700000e+03, 5.28921300e-01],
[1.97800000e+03, 5.42783400e-01],
[1.97900000e+03, 5.55425915e-01],
[1.98000000e+03, 5.71486587e-01],
[1.98100000e+03, 5.91539778e-01],
[1.98200000e+03, 6.13021691e-01],
[1.98300000e+03, 6.34508409e-01],
[1.98400000e+03, 6.57703989e-01]]
但是,我不清楚在statsmodel
中得到的两个值是什么。是不是我做错了什么。此外,我还想知道这两个参数frac
和it
的作用是什么
此外,我还想使用seaborn
绘制平滑的时间序列。似乎seaborn支持lowess
。但是,它没有frac
和it
参数。请参阅下面的代码
import numpy as np
import seaborn as sns
x = np.arange(0, 10, 0.01)
ytrue = np.exp(-x / 5) + 2 * np.sin(x / 3)
y = ytrue + np.random.normal(size=len(x))
sns.regplot(x, y, lowess=True)
在这种情况下,是否可以使用statmodels
输出在seaborn
中绘制regplot
如果需要,我很乐意提供更多详细信息。结果可以按照下面的代码绘制。请注意,lowess()
第一个参数是y
-值(endog
),第二个参数是x
(exog
)。默认结果的z[:,0]
为排序后的x
-值,而z[:,1]
为相应的估计y
-值
导入matplotlib.pyplot作为plt
将statsmodels.api作为sm导入
将numpy作为np导入
mydata=[0.6619346141815186, 0.7170140147209167, 0.692265510559082, 0.6394098401069641, 0.6030995845794678, 0.6500746607780457, 0.6013327240943909, 0.6273292303085327, 0.5865356922149658, 0.6477396488189697, 0.5827181339263916, 0.6496025323867798, 0.6589270234107971, 0.5498126149177551, 0.48638370633125305, 0.5367399454116821, 0.517595648765564, 0.5171639919281006, 0.47503289580345154, 0.6081966757774353, 0.5808742046356201, 0.5856912136077881, 0.5608134269714355, 0.6400936841964722, 0.6766082644462585]
对应年份=[1970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994]
x=np.数组(对应的年)
y=np.数组(mydata)
z=sm.非参数lowess(y,x,分形=1./3,it=3)
plt.plot(x,y,color='dodgerblue')
plt.绘图(z[:,0],z[:,1],'ro-')
plt.show()
PS:要与同一绘图上的seabornregplot
进行比较,请将其称为:
sns.regplot(x, y, lowess=True, ax=plt.gca())
结果可以如下面的代码所示绘制。请注意,lowess()
第一个参数是y
-值(endog
),第二个参数是x
(exog
)。默认结果是z[:,0]
作为排序后的x
-值和z[:,1]
相应的估计值y
-值
导入matplotlib.pyplot作为plt
将statsmodels.api作为sm导入
将numpy作为np导入
mydata=[0.6619346141815186, 0.7170140147209167, 0.692265510559082, 0.6394098401069641, 0.6030995845794678, 0.6500746607780457, 0.6013327240943909, 0.6273292303085327, 0.5865356922149658, 0.6477396488189697, 0.5827181339263916, 0.6496025323867798, 0.6589270234107971, 0.5498126149177551, 0.48638370633125305, 0.5367399454116821, 0.517595648765564, 0.5171639919281006, 0.47503289580345154, 0.6081966757774353, 0.5808742046356201, 0.5856912136077881, 0.5608134269714355, 0.6400936841964722, 0.6766082644462585]
对应年份=[1970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994]
x=np.数组(对应的年)
y=np.数组(mydata)
z=sm.非参数lowess(y,x,分形=1./3,it=3)
plt.plot(x,y,color='dodgerblue')
plt.绘图(z[:,0],z[:,1],'ro-')
plt.show()
PS:要与同一绘图上的seabornregplot
进行比较,请将其称为:
sns.regplot(x, y, lowess=True, ax=plt.gca())
谢谢你的回答。请让我知道这两个参数
frac
和it
的作用是什么?期待你的回复:)frac=1/3
似乎意味着在评估一个点的估计值时,要考虑所有数据的三分之一(默认值frac=2/3
).it=3
意味着权重提高了3倍。和中的更多信息。感谢您的回答。请让我知道frac
和it
这两个参数的作用是什么?期待您的回复:)frac=1/3
似乎意味着在评估一个点(默认值frac=2/3
)。it=3
表示权重提高了3倍。和中的更多信息。