sklearn.metrics求分类评价指标:准确率、精确度、F1值、混淆矩阵、ROC曲线、AUC值

Python_Code

"""
官方文档:https://scikit-learn.org/stable/modules/model_evaluation.html
时间:2021年08月22日15:26:40
作者:陈嘿萌
"""
import matplotlib.pyplot as plt
import sklearn.metrics as metrics
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import roc_curve

if __name__ == '__main__':

    # 1.统计报告:precision recall f1-score accuracy support
    # ================================================================= #
    #       y_true : 真实标签  y_fit = 预测结果 y_prob:预测结果的概率
    # ================================================================= #
    #       target_name : 类标签
    print("#=============================1.support===============================#")
    y_true = [0, 0, 1, 1, 2, 2, 3, 3]
    y_fit = [0, 1, 1, 1, 2, 3, 3, 0]
    y_prob = [0.5, 0.6, 0.7, 0.8, 0.9, 0.4, 0.3, 0.1]
    target_names = ['class1', 'class2', 'class3', 'class4']
    support = classification_report(y_true, y_fit, target_names=target_names)
    print(support)
    #  support是str类型,不方便取值,如果想要取值的话可以 转换成字典类型:output_dict = True
    support_dict = classification_report(y_true, y_fit, target_names=target_names, output_dict=True)
    # 转换成字典取值
    for k, v in support_dict.items():
        print(k, v)
    print("accuracy:", support_dict['accuracy'])
    # 单独求准确率指标
    accuracy = accuracy_score(y_true, y_fit)
    print("accuracy:", accuracy)
    # 2.计算混淆矩阵: confusion_matrix
    # ================================================================= #
    #       参考链接:https://blog.csdn.net/qq_36264495/article/details/88074246
    #       样式设计:https://blog.csdn.net/ztf312/article/details/102474190
    # ================================================================= #
    print("#==========================2.计算混淆矩阵=======================================#")
    matrix = confusion_matrix(y_true, y_fit)
    plt.matshow(matrix, cmap='YlOrRd')
    plt.title("Confusion_Matrix")
    plt.show()
    print("混淆矩阵:\n", matrix)
    # 3.计算ROC曲线和AUC值
    # ================================================================= #
    #       roc_curve(真实标签, 对应类别的概率, pos_label=指定正例样本类别)
    #       metrics.auc(假正类率, 真正类率)
    #       每个类别可以绘制一条ROC曲线求一个AUC值, 把当前类别指定为正例即可
    # =============================参考博客==================================== #
    #       https://blog.csdn.net/sun91019718/article/details/101314545
    #       https://blog.csdn.net/yuxiaosmd/article/details/83046162
    #       https://blog.csdn.net/akadiao/article/details/78788864
    #       roc:https://blog.csdn.net/taotiezhengfeng/article/details/80456110
    # ================================================================= #
    print("#==========================3.ROC曲线_AUC值=======================================#")
    fpr, tpr, threshold = roc_curve(y_true, y_prob, pos_label=0)
    auc = metrics.auc(fpr, tpr)
    print("auc:", auc)
    # 绘制roc曲线
    plt.plot(fpr, tpr)
    plt.title("class=0")
    plt.xlabel("FPR")
    plt.ylabel("TPR")
    plt.show()

我的笔记笔记

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈嘿萌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值