評估最佳樹模型
在這個練習中,你將評估 grid_dt 的最佳模型在測試集上的 ROC AUC 分數。
為了做到這點,你會先為測試集中的每個觀測值,取得其屬於正標籤的機率。你可以使用 sklearn 分類器的 predict_proba() 方法,計算一個 2D 陣列,欄方向分別為負類與正類標籤的機率。
資料集已替你載入並處理完成(數值特徵已標準化);資料以 80% 作為訓練集、20% 作為測試集進行切分。X_test、y_test 已可在你的工作空間中使用。另外,我們也載入了你在前一個練習建立並訓練好的 GridSearchCV 物件 grid_dt。請注意,grid_dt 的訓練方式如下:
grid_dt.fit(X_train, y_train)
本練習屬於課程
Machine Learning with Tree-Based Models in Python
練習說明
從
sklearn.metrics匯入roc_auc_score。從
grid_dt取出.best_estimator_屬性並指定給best_model。預測測試集中取得正類的機率為
y_pred_proba。計算
best_model在測試集上的 ROC AUC 分數test_roc_auc。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import roc_auc_score from sklearn.metrics
____
# Extract the best estimator
best_model = ____
# Predict the test set probabilities of the positive class
y_pred_proba = ____
# Compute test_roc_auc
test_roc_auc = ____
# Print test_roc_auc
print('Test set ROC AUC score: {:.3f}'.format(test_roc_auc))