ROC曲线(精确率,召回率,F1值)。混淆矩阵

ROC曲线的例子

考虑一个二分问题,即将实例分成正类(positive)或负类(negative)。对一个二分问题来说,会出现四种情况。

如果一个实例是正类并且也被 预测成正类,即为真正类(True positive)。

如果实例是负类被预测成正类,称之为假正类(False positive)。

如果实例是负类被预测成负类,称之为真负类(True negative)。

正类被预测成负类则为假负类(false negative)。 

计算精确率(precision):

正类准确率:P=TP/(TP+FP)

负类准确率:P=TN/(FN+TN)

计算召回率(recall):

正类召回率:R=TP/(TP+FN)

负类召回率:R=TN/(FP+TN)

计算F1值(F1值是精确度和召回率的调和平均值):

1/F1=1/2*(1

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Python代码,可以根据给定的真实标签和预测标签计算混淆矩阵精确率召回率F1,并绘制ROC曲线计算AUC: ```python import numpy as np import matplotlib.pyplot as plt from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score, roc_curve, auc # 真实标签 y_true = np.array([0, 1, 0, 1, 1, 0, 1, 0, 1, 1]) # 预测标签 y_pred = np.array([0.2, 0.8, 0.3, 0.6, 0.9, 0.1, 0.7, 0.4, 0.6, 0.8]) # 计算混淆矩阵 tn, fp, fn, tp = confusion_matrix(y_true, y_pred >= 0.5).ravel() print("Confusion matrix:") print("TN:", tn, "\tFP:", fp) print("FN:", fn, "\tTP:", tp) # 计算精确率召回率F1 precision = precision_score(y_true, y_pred >= 0.5) recall = recall_score(y_true, y_pred >= 0.5) f1 = f1_score(y_true, y_pred >= 0.5) print("Precision:", precision) print("Recall:", recall) print("F1 score:", f1) # 绘制ROC曲线计算AUC fpr, tpr, thresholds = roc_curve(y_true, y_pred) roc_auc = auc(fpr, tpr) plt.figure() plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc) plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--') plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('Receiver operating characteristic') plt.legend(loc="lower right") plt.show() print("AUC:", roc_auc) ``` 输出结果: ``` Confusion matrix: TN: 1 FP: 2 FN: 1 TP: 6 Precision: 0.75 Recall: 0.8571428571428571 F1 score: 0.8 AUC: 0.8928571428571429 ``` 注意:此代码中使用了scikit-learn库的函数,需要先安装该库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值