in CODE4ALL

deComposition time series in python

Hướng dẫn phân tích chuỗi data time series

Bước 1: chuẩn bị data chuyển đổi dạng csv với first line is title

Bước 2: code python

# insert các thư viện cần thiết

import pandas as pd
import numpy as np
import matplotlib.pylab as plt
from matplotlib.pylab import rcParams
from statsmodels.tsa.seasonal import seasonal_decompose

dateparse = lambda dates: pd.datetime.strptime(dates, ‘%Y-%m’)
data2 = pd.read_csv(‘CO2SP.csv’, parse_dates=[‘Month’], index_col=’Month’,date_parser=dateparse)
ts2=data2[‘CO2SouthPole’]
data = pd.read_csv(‘CO2LOA.csv’, parse_dates=[‘Month’], index_col=’Month’,date_parser=dateparse)
ts=data[‘CO2_ppm’]

decomposition = seasonal_decompose(ts)

trend = decomposition.trend
seasonal = decomposition.seasonal
residual = decomposition.resid

plt.subplot(411)
plt.plot(ts, label=”Origin”)
plt.legend(loc=’best’)
plt.subplot(412)
plt.plot(trend, label=’Trend’)
plt.legend(loc=’best’)
plt.subplot(413)
plt.plot(seasonal,label=’Seasonality’)
plt.legend(loc=’best’)
plt.subplot(414)
plt.plot(residual, label=’Residuals’)
plt.legend(loc=’best’)
plt.tight_layout()

plt.show()

Write a Comment

Comment