python 绘制ROC图像

# -*- coding: utf-8 -*-
import numpy as np  
import matplotlib.pyplot as plt  
from sklearn import svm, datasets  
from sklearn.metrics import roc_curve, auc  ###计算roc和auc  
from sklearn import cross_validation  
from pandas import DataFrame,Series
import pandas as pd
import os
import re

def ROC(path):
    df=pd.read_csv(path) 
    y_test=df['label']
    y_score=df['probability'] 
    ## Compute ROC curve and ROC area for each class  
    fpr,tpr,threshold = roc_curve(y_test, y_score) ###计算真正率和假正率  
    roc_auc = auc(fpr,tpr) ###计算auc的值
    return fpr,tpr,roc_auc

auc_list=[];tf_list=[]
plt.figure()
lw = 2  
plt.figure(figsize=(10,10))
for root, dirs, files in os.walk("C:/Users/dell/Desktop/ROC/", topdown=False):
    for name in files:
        dir_f=os.path.join(root, name)
        if re.search('val_predict.csv',dir_f):
            fpr,tpr,roc_auc=ROC(path=dir_f)
            tf=re.split(r'\\',re.split('/',dir_f)[-1])[-2]
            print (tf)
            tf_list.append(tf)
            auc_list.append(roc_auc)
            plt.plot(fpr, tpr, color='deepskyblue',lw=lw,linewidth=0.5) ###假正率为横坐标,真正率为纵坐标做曲线  label='ROC curve (area = %0.2f)' % roc_auc 
plt.plot([0, 1], [0, 1], color='silver', lw=lw, linestyle='--')  
plt.xlim([-0.0, 1.0])  
plt.ylim([0.0, 1.0])  
plt.xlabel('False Positive Rate')  
plt.ylabel('True Positive Rate')  
#plt.title('Receiver operating characteristic example')  
plt.legend(loc="lower right")
plt.savefig('C:/Users/dell/Desktop/ROC/roc.eps',dpi=600,bbox_inches='tight')  
plt.show()

plt.figure() 
plt.figure(figsize=(1,3))
plt.ylim([0.75, 1.0])
#plt.boxplot(auc_list,whis=1.5,
#            boxprops = {'color':'black','color':'deepskyblue','linewidth':2}, # 设置箱体属性,填充色和边框色  
#            flierprops = {'marker':'.','markerfacecolor':'black','color':'black'}, # 设置异常值属性,点的形状、填充色和边框色  
#            meanprops = {'marker':'D','markerfacecolor':'indianred'}, # 设置均值点的属性,点的形状、填充色  
#            medianprops = {'linestyle':'-','color':'deepskyblue','linewidth':2})
f=plt.boxplot(auc_list,widths=0.5,flierprops = {'marker':'o','markerfacecolor':'deepskyblue','color':'white'})
for box in f['boxes']:
    box.set( color='deepskyblue', linewidth=2)
    box.set( color = 'deepskyblue' )
for whisker in f['whiskers']:
    whisker.set(color='deepskyblue', linewidth=2)
for cap in f['caps']:
    cap.set(color='deepskyblue', linewidth=3)
for median in f['medians']:
    median.set(color='deepskyblue', linewidth=3)
#for flier in f['fliers']:
#    flier.set(marker='.', color='deepskyblue', alpha=0.8)
plt.xticks([])
plt.ylabel('AUC')
plt.savefig('C:/Users/dell/Desktop/ROC/AUC.eps',dpi=600,bbox_inches='tight')
plt.show()
df_auc=DataFrame()
df_auc['AUC']=Series(auc_list,index=tf_list)
df_auc.to_csv('C:/Users/dell/Desktop/ROC/AUC.csv') 

 

上一篇:[论文阅读笔记] Structural Deep Network Embedding


下一篇:准确率、精确率、召回率、F-Measure、ROC、AUC