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()
|