-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTL.py
More file actions
38 lines (25 loc) · 896 Bytes
/
Copy pathSTL.py
File metadata and controls
38 lines (25 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.seasonal import STL
from statsmodels.datasets import co2
def STLDecopose(x, season):
result = stl.fit(x, season)
return result.trend, result.seasonal
if __name__ == '__main__':
data = co2.load_pandas().data
data['co2'].interpolate(method='linear', inplace=True)
# STL分解
stl = STL(data['co2'], seasonal=13)
result = stl.fit()
# 绘图展示结果
fig, axes = plt.subplots(4, figsize=(8, 6))
axes[0].plot(data['co2'], label='Original')
axes[0].legend(loc='upper left')
axes[1].plot(result.trend, label='Trend')
axes[1].legend(loc='upper left')
axes[2].plot(result.seasonal, label='Seasonal')
axes[2].legend(loc='upper left')
axes[3].plot(result.resid, label='Residual')
axes[3].legend(loc='upper left')
plt.tight_layout()
plt.show()