python auc /precision_recall_vs_threshold 曲线绘制

本文介绍如何使用Python绘制ROC AUC曲线以及Precision-Recall曲线,用于评估二分类模型的性能。通过示例代码展示了如何计算并绘制这些曲线,帮助理解模型在不同阈值下的表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from matplotlib import pyplot as plt
%matplotlib inline
def plot_precision_recall_vs_threshold (precisions, recalls, thresholds) :

#precision_recall_curve函数的源码中,precision : array, shape = [n_thresholds + 1],the last element is 1
#之所以要通过precisions[: -1 ]使准确率数组中的最后一个值删掉,是因为,随着阈值的不断提高,对反例的识别率不断提高,最终的准确率趋近于1
#但此时的阈值可能太大导致阈值数组的范围太大不好表示,所以干脆就省略了这个阈值,直接令准确率数组的最后一个值等于1
    plt.plot(thresholds, precisions[: -1 ],  "b--" , label= "Precision" )
#recalls[: -1 ]意义同上
    plt.plot(thresholds, recalls[: -1 ],  "g-" , label= "Recall" )

    plt.xlabel( "Threshold" )

    plt.legend(loc= "upper left" )

    plt.ylim([ 0 ,  1 ])
    
    plt.show()



from  sklearn.metrics  import  roc_curve

#绘制图像
def plot_roc_curve (y_true,pre, label=None) :
    
    fpr, tpr, thresholds = roc_curve(y_true,pre)
    
    diff=tpr-fpr #array
    
    index=lis
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值