開始使用免費開始

分析最佳結果

在實務上,我們最在乎的是網格搜尋中表現最好的「方格」。幸好,Scikit Learn 的 gridSearchCv 物件提供多個參數,可以直接取得最佳方格(或 cv_results_ 中對應那一列)的關鍵資訊。

你將會探索三個屬性:

  • best_score_:最佳方格的分數(此處為 ROC_AUC)。
  • best_index_cv_results_ 中包含最佳方格資訊的那一列索引。
  • best_params_:產生最佳分數的參數字典,例如 'max_depth': 10

已為你提供網格搜尋物件 grid_rf_class

第 6 行已替你從 cv_results_ 建立一個資料框(cv_results_df)。這能幫助你用索引存取結果。

本練習屬於課程

Python 超參數調校

檢視課程

練習說明

  • grid_rf_class 中表現最佳的方格,擷取並列印 ROC_AUC 的分數
  • 透過索引存取 cv_results_df,建立一個對應至最佳那一列的變數。
  • 建立變數 best_n_estimators,從 grid_rf_class 的最佳方格中擷取 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)
編輯並執行程式碼