ベストな結果を使う
グリッドサーチの結果を分析するのは有益ですが、最終的な目的は実務的です。推定器オブジェクトを使ってテストデータに対して予測を行いたいのです。
このオブジェクトには、グリッドサーチオブジェクトの best_estimator_ プロパティからアクセスできます。
best_estimator_ プロパティの中身を確認し、予測を行い、評価指標を計算してみましょう。まずはデフォルトの predict(クラス予測)を使いますが、roc-auc の計算には確率が必要なため、roc-auc を算出するときは predict ではなく predict_proba を使います。正例クラスの確率を取り出すためにスライス [:,1] を使います。
X_test と y_test のデータセット、および前の演習で作成した grid_rf_class オブジェクトが利用可能です。
この演習はコースの一部です
Pythonでのハイパーパラメータチューニング
演習の手順
best_estimator_プロパティの型を確認します。best_estimator_プロパティを使ってテストセットに対する予測を行います。- 予測結果から混同行列と ROC_AUC スコアを算出します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# See what type of object the best_estimator_ property is
print(____(____.____))
# Create an array of predictions directly using the best_estimator_ property
predictions = grid_rf_class.____._____(X_test)
# Take a look to confirm it worked, this should be an array of 1's and 0's
print(predictions[0:5])
# Now create a confusion matrix
print("Confusion Matrix \n", confusion_matrix(y_test, ______))
# Get the ROC-AUC score
predictions_proba = grid_rf_class.best_estimator_.predict_proba(X_test)[:,1]
print("ROC-AUC Score \n", roc_auc_score(y_test, _____))