パフォーマンスの評価
最後に、いつものようにベストモデルのパフォーマンスを評価し、予測の精度を確認しましょう。本来はバックテストが理想的ですが、このコースで扱うには複雑すぎるため、ここでは省略します。
R\(^2\) スコアはすでに確認しましたが、次は matplotlib を使って予測値と実際の値の散布図を描いてみましょう。予測が完璧な場合、点は左下から右上への対角線上に並びます。
この演習はコースの一部です
Python による金融のための Machine Learning
演習の手順
- 前の演習で見つけた
max_featuresの最適値(4)をRandomForestRegressor(rfr)に設定します。 train_featuresとtest_featuresを使ってモデルで予測を行います。- 実際のターゲット値(
train/test_targets)と予測値(train/test_predictions)の散布図を描き、データセットにそれぞれtrain、testというラベルを付けます。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Use the best hyperparameters from before to fit a random forest model
rfr = RandomForestRegressor(n_estimators=200, max_depth=3, max_features=____, random_state=42)
rfr.fit(train_features, train_targets)
# Make predictions with our model
train_predictions = rfr.predict(____)
test_predictions = ____
# Create a scatter plot with train and test actual vs predictions
plt.scatter(train_targets, train_predictions, label='train')
plt.scatter(____)
plt.legend()
plt.show()