時間序列中的季節性、趨勢與雜訊

使用 Python 視覺化時間序列資料

Thomas Vincent

Head of Data Science, Getty Images

時間序列的特性

時間序列的特性

使用 Python 視覺化時間序列資料

時間序列的屬性

  • 季節性:資料是否呈現明顯的週期性?
  • 趨勢:資料是否有一致的上升或下降斜率?
  • 雜訊:是否有離群點或與整體不一致的遺漏值?
使用 Python 視覺化時間序列資料

時間序列分解

import statsmodels.api as sm
import matplotlib.pyplot as plt
from pylab import rcParams

rcParams['figure.figsize'] = 11, 9
decomposition = sm.tsa.seasonal_decompose(
                co2_levels['co2'])
fig = decomposition.plot()

plt.show()
使用 Python 視覺化時間序列資料

CO2 資料的時間序列分解圖

時間序列分解

使用 Python 視覺化時間序列資料

從時間序列分解擷取元件

print(dir(decomposition))
['__class__', '__delattr__', '__dict__',
 ... 'plot', 'resid', 'seasonal', 'trend']
print(decomposition.seasonal)
datestamp
1958-03-29    1.028042
1958-04-05    1.235242
1958-04-12    1.412344
1958-04-19    1.701186
使用 Python 視覺化時間序列資料

時間序列的季節性元件

decomp_seasonal = decomposition.seasonal

ax = decomp_seasonal.plot(figsize=(14, 2))
ax.set_xlabel('Date')
ax.set_ylabel('Seasonality of time series')
ax.set_title('Seasonal values of the time series')

plt.show()
使用 Python 視覺化時間序列資料

時間序列的季節性元件

時間序列的季節性

使用 Python 視覺化時間序列資料

時間序列的趨勢元件

decomp_trend = decomposition.trend

ax = decomp_trend.plot(figsize=(14, 2))
ax.set_xlabel('Date')
ax.set_ylabel('Trend of time series')
ax.set_title('Trend values of the time series')

plt.show()
使用 Python 視覺化時間序列資料

時間序列的趨勢元件

時間序列的趨勢

使用 Python 視覺化時間序列資料

時間序列的雜訊元件

decomp_resid = decomp.resid

ax = decomp_resid.plot(figsize=(14, 2))
ax.set_xlabel('Date')
ax.set_ylabel('Residual of time series')
ax.set_title('Residual values of the time series')

plt.show()
使用 Python 視覺化時間序列資料

時間序列的雜訊元件

時間序列的雜訊

使用 Python 視覺化時間序列資料

一起來練習吧!

使用 Python 視覺化時間序列資料

Preparing Video For Download...