一次打造多個滾動特徵
你已經練習過一些簡單的特徵工程了,現在來進一步挑戰更進階的內容。你將為時間序列資料計算一組特徵,並把它們隨時間的變化視覺化。這個流程與許多時間序列模型的運作方式相當類似。
本練習屬於課程
Python 的時間序列資料機器學習
練習說明
- 定義一個清單,依序包含你要計算的 4 個特徵:最小值、最大值、平均數與標準差。
- 使用我們為你定義的滾動視窗(
prices_perc_rolling),從features_to_calculate計算這些特徵。 - 使用提供的程式碼,將結果與原始時間序列一起隨時間繪圖展示。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Define a rolling window with Pandas, excluding the right-most datapoint of the window
prices_perc_rolling = prices_perc.rolling(20, min_periods=5, closed='right')
# Define the features you'll calculate for each window
features_to_calculate = [np.min, ____, ____, ____]
# Calculate these features for your rolling window object
features = prices_perc_rolling.____(features_to_calculate)
# Plot the results
ax = features.loc[:"2011-01"].plot()
prices_perc.loc[:"2011-01"].plot(ax=ax, color='k', alpha=.2, lw=3)
ax.legend(loc=(1.01, .6))
plt.show()