混淆矩阵、准确率、精确率、召回率、F值、ROC曲线、AUC、PR曲线-Sklearn.metrics评估方法

https://www.jianshu.com/p/5df19746daf9

image.png

 

image.png

分类准确率 accuracy

  • 所有样本中被预测正确的样本的比率
  • 分类模型总体判断的准确率(包括了所有class的总体准确率)
  • 准确率的API:
from sklearn.metrics import accuracy
accuracy = accuracy_score(y_test, y_predict)

image.png

精确率Precision

  • 预测为正类0的准确率
    TP / ( TP + FP )
from sklearn.metrics import precision_score
precision = precision_score(y_test, y_predict)

召回率 recall

  • 真实为0的准确率

     

    image.png

  • 真实为1的准确率
    Recall = TN/(TN+FP)
  • 召回率API:
from sklearn.metrics import recall_score
recall = recall_score(y_test, y_predict)
#recall得到的是一个list,是每一类的召回率

F1值

  • 用来衡量二分类模型精确度的一种指标。它同时兼顾了分类模型的准确率召回率。F1分数可以看作是模型准确率召回率的一种加权平均,它的最大值是1,最小值是0。

image.png

from sklearn.metrics import f1_score
f1_score(y_test, y_predict)

Roc曲线、AUC

  • TPR FPR

    • 样本中的真实正例类别总数即TP+FN
      TPR即True Positive Rate,TPR = TP/(TP+FN)。
    • TPR:真实的正例0中,被预测为正例的比例
    • 样本中的真实反例类别总数为FP+TN
      FPR即False Positive Rate,FPR=FP/(TN+FP)。
    • FPR:真实的反例1中,被预测为正例的比例
    • 理想分类器TPR=1,FPR=0
  • 截断点thresholds

    机器学习算法对test样本进行预测后,可以输出各test样本对某个类别的相似度概率。比如t1是P类别的概率为0.3,一般我们认为概率低于0.5,t1就属于类别N。这里的0.5,就是”截断点”。

总结一下,对于计算ROC,最重要的三个概念就是TPR, FPR, 截断点。

  • ROC曲线

  • ROC曲线越接近左上角,代表模型越好,即ACU接近1
from sklearn.metrics import roc_auc_score, auc
import matplotlib.pyplot as plt
y_predict = model.predict(x_test)
y_probs = model.predict_proba(x_test) #模型的预测得分
fpr, tpr, thresholds = metrics.roc_curve(y_test,y_probs)
roc_auc = auc(fpr, tpr)  #auc为Roc曲线下的面积
#开始画ROC曲线
plt.plot(fpr, tpr, 'b',label='AUC = %0.2f'% roc_auc)
plt.legend(loc='lower right')
plt.plot([0,1],[0,1],'r--')
plt.xlim([-0.1,1.1])
plt.ylim([-0.1,1.1])
plt.xlabel('False Positive Rate') #横坐标是fpr
plt.ylabel('True Positive Rate')  #纵坐标是tpr
plt.title('Receiver operating characteristic example')
plt.show()

运行结果如下图所示:

 

参考资料:
1.混淆矩阵(Confusion Matrix)
https://www.jianshu.com/p/0fc8a0b784f1
2.ROC与AUC的定义与使用详解
https://blog.csdn.net/shenxiaoming77/article/details/72627882



作者:小歪与大白兔
链接:https://www.jianshu.com/p/5df19746daf9
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的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库的函数,需要先安装该库。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值