繪製資料的聚合值

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

Thomas Vincent

Head of Data Science, Getty Images

移動平均

  • 在時間序列分析中,移動平均可用於多種情境:
    • 平滑短期波動
    • 移除離群值
    • 突顯長期趨勢或循環。
使用 Python 視覺化時間序列資料

移動平均模型

co2_levels_mean = co2_levels.rolling(window=52).mean()

ax = co2_levels_mean.plot()
ax.set_xlabel("Date")
ax.set_ylabel("The values of my Y axis")
ax.set_title("52 weeks rolling mean of my time series")

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

CO2 資料的移動平均圖

時間序列的移動平均

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

計算時間序列的聚合值

co2_levels.index
DatetimeIndex(['1958-03-29', '1958-04-05',...],
              dtype='datetime64[ns]', name='datestamp', 
              length=2284, freq=None)
print(co2_levels.index.month)
array([ 3,  4,  4, ..., 12, 12, 12], dtype=int32)
print(co2_levels.index.year)
array([1958, 1958, 1958, ..., 2001,
      2001, 2001], dtype=int32)
使用 Python 視覺化時間序列資料

繪製時間序列的聚合值

index_month = co2_levels.index.month
co2_levels_by_month = co2_levels.groupby(index_month).mean()
co2_levels_by_month.plot()

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

繪製時間序列的聚合值

CO2 濃度資料的月度聚合

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

一起來練習吧!

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

Preparing Video For Download...