使用最佳結果
分析格點搜尋的結果很有意思,不過我們的最終目標偏向實務:要用估計器物件在測試集上做出預測。
我們可以透過格點搜尋物件的 best_estimator_ 屬性來取得這個物件。
讓我們查看 best_estimator_ 的內容,進行預測,並產生評估分數。我們會先用預設的 predict(輸出類別預測),接著為了計算 roc-auc,需改用 predict_proba,因為 roc-auc 的計算需要機率分數。我們使用切片 [:,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, _____))