如何在一张图中画多条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()

在这里插入图片描述

示例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.标签))

在这里插入图片描述

  • 19
    点赞
  • 106
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Python中绘制多条ROC曲线的方法如下: 1. 导入所需的库:`import matplotlib.pyplot as plt` 2. 准备数据:获取多个分类器的真正率(True Positive Rate, TPR)和假正率(False Positive Rate, FPR)曲线数据,这些数据可以通过调用分类器的评估函数获得。 3. 创建图像和子图对象:`fig, ax = plt.subplots()` 4. 循环绘制曲线:使用`ax.plot()`函数,一次绘制每个分类器的ROC曲线,传入分类器的TPR和FPR数据作为参数。 5. 添加标题和标签:使用`ax.set()`函数,设置图像的标题、X轴和Y轴的标签。 6. 设置图例:如果需要,使用`ax.legend()`函数,设置图例以显示每个分类器的标识。 7. 显示图像:使用`plt.show()`函数,显示绘制好的ROC曲线图像。 下面是一个简单的示例代码,其中包含了绘制两条ROC曲线的基本步骤: ```python import matplotlib.pyplot as plt # 准备数据 classifier_1_tpr = [0.1, 0.2, 0.4, 0.6, 0.8] # 分类器1的TPR数据 classifier_1_fpr = [0.2, 0.3, 0.5, 0.7, 0.9] # 分类器1的FPR数据 classifier_2_tpr = [0.2, 0.3, 0.5, 0.7, 0.9] # 分类器2的TPR数据 classifier_2_fpr = [0.1, 0.2, 0.4, 0.6, 0.8] # 分类器2的FPR数据 # 创建图像和子图对象 fig, ax = plt.subplots() # 绘制曲线 ax.plot(classifier_1_fpr, classifier_1_tpr, label='Classifier 1') ax.plot(classifier_2_fpr, classifier_2_tpr, label='Classifier 2') # 添加标题和标签 ax.set(title='ROC Curve', xlabel='False Positive Rate', ylabel='True Positive Rate') # 设置图例 ax.legend() # 显示图像 plt.show() ``` 以上代码中,我们首先准备了两个分类器的TPR和FPR数据,然后使用`ax.plot()`函数绘制了两条ROC曲线,接着用`ax.set()`函数设置了标题和标签,最后使用`ax.legend()`函数设置了图例,最终使用`plt.show()`函数显示了绘制好的ROC曲线图像。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值