始める無料で始める

複数のローリング特徴量を一度に作成する

基本的な特徴量作成に慣れてきたところで、もう少し複雑な内容に進みましょう。時系列データに対して複数の特徴量をまとめて計算し、時間とともにどのように変化するかを可視化します。これは、多くの時系列モデルの処理に近い流れです。

この演習はコースの一部です

Pythonで学ぶMachine Learningによる時系列データ解析

コースを見る

演習の手順

  • 計算する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()
コードを編集して実行