如何在一张图中画多条ROC线?

示例1:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * (np.pi))  #numpy.linspace(开始,终值(含终值)),个数)
y1 = np.sin(x)
y2 = np.cos(x)

#画图
plt.title('Compare cosx with sinx')  #标题
#plt.plot(x,y)
#常见线的属性有:color,label,linewidth,linestyle,marker等
plt.plot(x, y1, color='cyan', label='sinx')
plt.plot(x, y2, 'b', label='cosx')#'b'指:color='blue'
plt.legend()  #显示上面的label
plt.xlabel('x')
plt.ylabel('f(x)')
plt.axis([0, 2*np.pi, -1, 1])#设置坐标范围axis([xmin,xmax,ymin,ymax])
#plt.ylim(-1,1)#仅设置y轴坐标范围
plt.show()

如何在一张图中画多条ROC线?_第1张图片

示例2:

from sklearn import metrics
import pylab as plt
def ks(y_predicted1, y_true1, y_predicted2, y_true2, y_predicted3, y_true3):
  Font={'size':18, 'family':'Times New Roman'}
  
  label1=y_true1
  label2=y_true2
  label3=y_true3
  fpr1,tpr1,thres1 = metrics.roc_curve(label1, y_predicted1)
  fpr2,tpr2,thres2 = metrics.roc_curve(label2, y_predicted2)
  fpr3,tpr3,thres3 = metrics.roc_curve(label3, y_predicted3)
  roc_auc1 = metrics.auc(fpr1, tpr1)
  roc_auc2 = metrics.auc(fpr2, tpr2)
  roc_auc3 = metrics.auc(fpr3, tpr3)
  
  plt.figure(figsize=(6,6))
  plt.plot(fpr1, tpr1, 'b', label = 'Stacking = %0.3f' % roc_auc1, color='Red')
  plt.plot(fpr2, tpr2, 'b', label = 'XGBoost = %0.3f' % roc_auc2, color='k')
  plt.plot(fpr3, tpr3, 'b', label = 'Random Forest = %0.3f' % roc_auc3, color='RoyalBlue')
  plt.legend(loc = 'lower right', prop=Font)
  plt.plot([0, 1], [0, 1],'r--')
  plt.xlim([0, 1])
  plt.ylim([0, 1])
  plt.ylabel('True Positive Rate', Font)
  plt.xlabel('False Positive Rate', Font)
  plt.tick_params(labelsize=15)
  plt.show()
  return abs(fpr1 - tpr1).max(),abs(fpr2 - tpr2).max(),abs(fpr3 - tpr3).max()
import pandas as pd
r1 = pd.read_csv(r"C:\Users\Royalwen\Desktop\stacking.csv",header=None,names=['用户标识','预测','标签'])
r2 = pd.read_csv(r"C:\Users\Royalwen\Desktop\xgboost.csv",header=None,names=['用户标识','标签','预测'])
r3 = pd.read_csv(r"C:\Users\Royalwen\Desktop\rf.csv",header=None,names=['标签','预测'])

print("线下得分;")
print(ks(r1.预测, r1.标签, r2.预测, r2.标签, r3.预测, r3.标签))

如何在一张图中画多条ROC线?_第2张图片

你可能感兴趣的:(python)