python auc /precision_recall_vs_threshold 曲线绘制

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=list(diff).index(max(tpr-fpr))
    
    yuzhi=thresholds[index]
    
    fp_1=fpr[index]
    
    tp_1=tpr[index]
    
    
    plt.plot(fpr, tpr, linewidth= 2 , label=label)
    
    plt.plot(fpr,diff,c='g',linestyle='dashed',label='ks')

    plt.plot([ 0 ,  1 ], [ 0 ,  1 ],  'k--' )
    
    plt.plot( fp_1,tp_1 ,'ro')
    
    plt.text(fp_1, tp_1, (tp_1, fp_1,max(tpr-fpr),yuzhi),ha='center', va='bottom', fontsize=10)
    
    plt.vlines(fp_1, 0, 1, colors = "c", linestyles = "dashed")

    plt.axis([ 0 ,  1 ,  0 ,  1 ])

    plt.xlabel( 'False Positive Rate' )

    plt.ylabel( 'True Positive Rate' )

plot_roc_curve (y_true,pre, label=None)

 

你可能感兴趣的:(python)