パーセンタイルとpartial関数
この演習では、関数の引数をあらかじめ指定して、実行方法を事前に設定するやり方を練習します。これを使って、numpy の同じ percentile() 関数から、データの複数のパーセンタイルを計算します。
この演習はコースの一部です
Pythonで学ぶMachine Learningによる時系列データ解析
演習の手順
functoolsからpartialをインポートします。partial()関数を使い、リスト内包表記でデータのパーセンタイルを計算する複数の特徴量生成器を作成します。- あらかじめ用意したローリングウィンドウ(
prices_perc_rolling)を使い、percentile_functionsで分位点を計算します。 - 付与されたコードで結果を可視化します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Import partial from functools
____
percentiles = [1, 10, 25, 50, 75, 90, 99]
# Use a list comprehension to create a partial function for each quantile
percentile_functions = [____(np.percentile, q=percentile) for percentile in percentiles]
# Calculate each of these quantiles on the data using a rolling window
prices_perc_rolling = prices_perc.rolling(20, min_periods=5, closed='right')
features_percentiles = prices_perc_rolling.____(____)
# Plot a subset of the result
ax = features_percentiles.loc[:"2011-01"].plot(cmap=plt.cm.viridis)
ax.legend(percentiles, loc=(1.01, .5))
plt.show()