如何在一张图中画不同模型的roc曲线(python)

本文详细介绍了如何使用Python在一张图表上绘制多个机器学习模型的ROC曲线,包括必要的库导入、数据处理和绘图步骤,对于理解和比较模型性能极具帮助。
摘要由CSDN通过智能技术生成
import pandas as pd
import warnings
warnings.filterwarnings('ignore')

from   matplotlib import pyplot as plt
from sklearn.metrics import roc_auc_score, roc_curve,auc
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import BaggingClassifier
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.model_selection import train_test_split as sp
from xgboost import XGBClassifier
from lightgbm import  LGBMClassifier
from sklearn import datasets
from sklearn.model_selection import StratifiedKFold
from sklearn.linear_model import LogisticRegression as LR

def multi_models_roc(names, sampling_methods, colors, train, test, save=True, dpin=100):
    plt.figure(figsize=(20, 20)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
不同模型ROC 曲线在同一张,可以使用 Python 的 Matplotlib 库。以下是一个简单的例子,假设你有两个模型的预测概率值和对应的标签: ```python import numpy as np import matplotlib.pyplot as plt from sklearn.metrics import roc_curve, auc # 第一个模型的预测概率值和真实标签 y_pred_1 = np.array([0.2, 0.5, 0.7, 0.8, 0.1]) y_true_1 = np.array([0, 0, 1, 1, 0]) # 第二个模型的预测概率值和真实标签 y_pred_2 = np.array([0.1, 0.4, 0.6, 0.9, 0.3]) y_true_2 = np.array([0, 1, 1, 1, 0]) # 计算 ROC 曲线和 AUC 值 fpr_1, tpr_1, thresholds_1 = roc_curve(y_true_1, y_pred_1) auc_1 = auc(fpr_1, tpr_1) fpr_2, tpr_2, thresholds_2 = roc_curve(y_true_2, y_pred_2) auc_2 = auc(fpr_2, tpr_2) # ROC 曲线 plt.plot(fpr_1, tpr_1, color='blue', lw=2, label='Model 1 (AUC = %0.2f)' % auc_1) plt.plot(fpr_2, tpr_2, color='red', lw=2, label='Model 2 (AUC = %0.2f)' % auc_2) # 设置像属性 plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('Receiver Operating Characteristic') plt.legend(loc="lower right") plt.show() ``` 在上面的代码,我们使用 `roc_curve` 函数计算每个模型ROC 曲线和 AUC 值,并使用 `plot` 函数绘制它们。`xlabel` 和 `ylabel` 函数分别设置 X 轴和 Y 轴的标签,`title` 函数设置像标题,`legend` 函数添加例,`show` 函数显示像。你可以根据需要修改这些属性以及其他属性来自定义你的像。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值