开始使用免费开始使用

RF 模型的性能指标

在前面的练习中,您已经得到了随机森林模型的准确率分数。但在欺诈检测中,准确率可能会产生误导。当数据高度不平衡时,AUROC 曲线 是更可靠的性能指标,可用于比较不同分类器。此外,分类报告 能告诉您模型的精确率和召回率,而 混淆矩阵 则直观展示模型实际能正确识别的欺诈样本数量。现在就来计算这些性能指标。

您将继续使用上一个练习中的同一随机森林模型。您的模型定义为 model = RandomForestClassifier(random_state=5),已在训练数据上完成拟合,并且 X_train, y_train, X_test, y_test 已可用。

本练习是课程的一部分

Python 中的欺诈检测

查看课程

练习说明

  • sklearn.metrics 导入分类报告、混淆矩阵和 ROC 分数相关函数。
  • 基于已训练的随机森林 model 获取二分类预测结果。
  • 运行 predict_proba() 函数以获得预测概率。
  • y_testpredicted 进行比较,得到分类报告和混淆矩阵。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Import the packages to get the different performance metrics
from sklearn.metrics import ____, ____, ____

# Obtain the predictions from our random forest model 
predicted = model.____(X_test)

# Predict probabilities
probs = ____.____(X_test)

# Print the ROC curve, classification report and confusion matrix
print(____(y_test, probs[:,1]))
print(____(____, predicted))
print(____(____, ____))
编辑并运行代码