季节性在python中分解

时间:2017-12-02 15:39:48

标签: python matplotlib machine-learning time-series statsmodels

我有一个CSV文件,其中包含近5年的平均温度。使用seasonal_decompose中的statsmodels.tsa.seasonal函数进行分解后,得到了以下结果。的确,结果不显示任何季节性!但是,我在趋势中看到了明确的sin!我想知道为什么会这样,我该如何纠正呢?谢谢。

nresult = seasonal_decompose(nseries, model='additive', freq=1)
nresult.plot()
plt.show()

enter image description here

1 个答案:

答案 0 :(得分:5)

您的freq似乎已关闭。

import numpy as np
import pandas as pd
from statsmodels.tsa.seasonal import seasonal_decompose

# Generate some data
np.random.seed(0)
n = 1500
dates = np.array('2005-01-01', dtype=np.datetime64) + np.arange(n)
data = 12*np.sin(2*np.pi*np.arange(n)/365) + np.random.normal(12, 2, 1500)
df = pd.DataFrame({'data': data}, index=dates)

# Reproduce the example in OP
seasonal_decompose(df, model='additive', freq=1).plot()

enter image description here

# Redo the same thing, but with the known frequency
seasonal_decompose(df, model='additive', freq=365).plot()

enter image description here