始める無料で始める

ベスト結果の分析

最終的に重要なのは、グリッドサーチの中で最も良い性能を出した「マス(square)」です。幸い、Scikit Learn の gridSearchCv オブジェクトには、ベストなマス(つまり cv_results_ の1行)に関する主要な情報を提供するパラメータがいくつか用意されています。

ここでは次の3つのプロパティを確認します。

  • best_score_ – 最良のマスから得られたスコア(ここでは ROC_AUC)。
  • best_index_ – 最良のマスに関する情報を含む cv_results_ の行インデックス。
  • best_params_ – 最良スコアを出したパラメータの辞書。例: 'max_depth': 10

グリッドサーチオブジェクト grid_rf_class が用意されています。

cv_results_ から作成したデータフレーム(cv_results_df)が6行目にあります。これを使って結果にインデックスアクセスできます。

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

Pythonでのハイパーパラメータチューニング

コースを見る

演習の手順

  • grid_rf_class の中で最良のマスから ROC_AUC のスコアを抽出して表示します。
  • cv_results_dfインデックス指定でアクセスし、最良行から変数を作成します。
  • 最良のマスのパラメータから n_estimators を取り出して best_n_estimators という変数を作成し、表示します。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

# Print out the ROC_AUC score from the best-performing square
best_score = grid_rf_class._____
print(best_score)

# Create a variable from the row related to the best-performing square
cv_results_df = pd.DataFrame(grid_rf_class.cv_results_)
best_row = cv_results_df.loc[[grid_rf_class.____]]
print(best_row)

# Get the n_estimators parameter from the best-performing square and print
best_n_estimators = grid_rf_class.____["_____"]
print(best_n_estimators)
コードを編集して実行