python画ROC曲线并标记AUC

定义画图ROC函数

def roc(name): # 生成ROC曲线
    plt.figure(dpi=150,figsize=(3,3))
    plt.plot(fpr_test,tpr_test,'b:',label=f'测试集AUC={round(metrics.auc(fpr_test,tpr_test),4)}')
    plt.plot(fpr_train,tpr_train,'r-.',label=f'训练集AUC={round(metrics.auc(fpr_train,tpr_train),4)}')
    plt.legend();plt.title(f'ROC曲线-{name}');plt.xlabel('假正率');plt.ylabel('真正率')
    # plt.grid()
    plt.plot([0, 1], [0, 1], 'k--',linewidth = 0.5)
x2_train, x2_test, y2_train, y2_test = train_test_split(x1, y1, test_size=0.3,random_state=3)
from sklearn.linear_model import LogisticRegression as LR
model = LR()
model.fit(x2_train,y2_train)
# roc
fpr_test,tpr_test,th_test = metrics.roc_curve(y2_test,model.predict_proba(x2_test)[:,1]) # 测试集
fpr.append(fpr_test); tpr.append(tpr_test); auc.append(round(metrics.auc(fpr_test,tpr_test),4))
fpr_train,tpr_train,th_train = metrics.roc_curve(y2_train,model.predict_proba(x2_train)[:,1]) # 训练集
roc('逻辑回归')

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值