使用最佳结果
分析网格搜索的结果固然有趣,但我们的最终目标是实用:使用估计器对象在测试集上进行预测。
我们可以通过网格搜索对象的 best_estimator_ 属性访问该对象。
让我们查看 best_estimator_ 属性,进行预测,并生成评估分数。我们先使用默认的 predict(给出类别预测),但要计算 roc-auc 分数,则需要使用 predict_proba 而不是 predict,因为 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, _____))