lgbm的roc曲线,auc计算

lgbm模型画ROC曲线

1、得到分类的概率

	import numpy as np
	import pandas as pd
	import lightgbm as lgb
	
	from sklearn.model_selection import train_test_split


	X_train, X_test, y_train, y_test = \
        train_test_split(data.iloc[:, 0:-1],  # feature
                         data.iloc[:, -1],  # label
                         test_size=0.2,  # 训练集、测试集分割比例
                         stratify=data.iloc[:, -1]  # 这里保证分割后y的比例分布与原数据一致
                         )

    gbm = lgb.LGBMClassifier(colsample_bytree=0.7, max_depth=6, min_child_weight=0.0, min_child_samples=26,
                             n_estimators=65, num_leaves=28, objective='binary', learning_rate=0.14,
                             subsample=0.6)

    gbm.fit(X_train, y_train)

    gbm_y_pre = gbm.predict(X_test) # 分类的类别
    gbm_y_proba = gbm.predict_proba(X_test) # 分类的概率值

2、计算AUC,画出ROC

	import matplotlib.pyplot as plt
	
	from sklearn.metrics import roc_auc_score, roc_curve


	gbm_auc = roc_auc_score(y_test, gbm_y_proba[:, 1])  # 计算auc
    gbm_fpr, gbm_tpr, gbm_threasholds = roc_curve(y_test, gbm_y_proba[:, 1])  # 计算ROC的值
    plt.title("roc_curve of %s(AUC=%.4f)" % ('gbm', gbm_auc))
    plt.xlabel('Specificity')  # specificity = 1 - np.array(gbm_fpr))
    plt.ylabel('Sensitivity')  # sensitivity = gbm_tpr
    plt.plot(list(1 - np.array(gbm_fpr)), gbm_tpr)
    plt.gca().invert_xaxis()  # 将X轴反转
    plt.show()

在这里插入图片描述

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值