GBDT+LR

利用GBDT对特征进行变换

https://wenku.baidu.com/view/7319fc2c960590c69ec376c0.html


https://blog.csdn.net/shine19930820/article/details/71713680/


http://scikit-learn.org/stable/auto_examples/ensemble/plot_feature_transformation.html#example-ensemble-plot-feature-transformation-py


xgboost + LR

来源:https://blog.csdn.net/asdfghjkl1993/article/details/78606268

[python]  view plain  copy
  1. #!/usr/bin python  
  2. #-*- coding:utf-8 -*-  
  3.   
  4. import numpy as np  
  5. import matplotlib.pyplot as plt  
  6. from sklearn.datasets import make_classification  
  7. from sklearn.linear_model import LogisticRegression  
  8. from sklearn.ensemble import (RandomTreesEmbedding, RandomForestClassifier,  
  9.                               GradientBoostingClassifier)  
  10. from sklearn.preprocessing import OneHotEncoder  
  11. from sklearn.model_selection import train_test_split  
  12. from sklearn.metrics import roc_curve, roc_auc_score  
  13. from sklearn.pipeline import make_pipeline  
  14. import xgboost as xgb  
  15. from xgboost.sklearn import XGBClassifier  
  16.   
  17. np.random.seed(10)  
  18. n_estimator = 10  
  19.   
  20. X, y = make_classification(n_samples=80000)  
  21. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5)  
  22. #To avoid overfitting  
  23. X_train, X_train_lr, y_train, y_train_lr = train_test_split(X_train, y_train, test_size=0.5)  
  24.   
  25. def RandomForestLR():  
  26.     rf = RandomForestClassifier(max_depth=3, n_estimators=n_estimator)  
  27.     rf_enc = OneHotEncoder()  
  28.     rf_lr = LogisticRegression()  
  29.     rf.fit(X_train, y_train)  
  30.     rf_enc.fit(rf.apply(X_train))  
  31.     rf_lr.fit(rf_enc.transform(rf.apply(X_train_lr)), y_train_lr)  
  32.     y_pred_rf_lr = rf_lr.predict_proba(rf_enc.transform(rf.apply(X_test)))[:, 1]  
  33.     fpr_rf_lr, tpr_rf_lr, _ = roc_curve(y_test, y_pred_rf_lr)  
  34.     auc = roc_auc_score(y_test, y_pred_rf_lr)  
  35.     print("RF+LR:", auc)  
  36.     return fpr_rf_lr, tpr_rf_lr  
  37.   
  38. def GdbtLR():  
  39.     grd = GradientBoostingClassifier(n_estimators=n_estimator)  
  40.     grd_enc = OneHotEncoder()  
  41.     grd_lr = LogisticRegression()  
  42.     grd.fit(X_train, y_train)  
  43.     grd_enc.fit(grd.apply(X_train)[:, :, 0])  
  44.     grd_lr.fit(grd_enc.transform(grd.apply(X_train_lr)[:, :, 0]), y_train_lr)  
  45.     y_pred_grd_lr = grd_lr.predict_proba(grd_enc.transform(grd.apply(X_test)[:, :, 0]))[:, 1]  
  46.     fpr_grd_lr, tpr_grd_lr, _ = roc_curve(y_test, y_pred_grd_lr)  
  47.     auc = roc_auc_score(y_test, y_pred_grd_lr)   
  48.     print("GDBT+LR:", auc)  
  49.     return fpr_grd_lr, tpr_grd_lr  
  50.   
  51. def Xgboost():  
  52.     xgboost = xgb.XGBClassifier(nthread=4, learning_rate=0.08,\  
  53.             n_estimators=50, max_depth=5, gamma=0, subsample=0.9, colsample_bytree=0.5)  
  54.     xgboost.fit(X_train, y_train)  
  55.     y_xgboost_test = xgboost.predict_proba(X_test)[:, 1]  
  56.     fpr_xgboost, tpr_xgboost, _ = roc_curve(y_test, y_xgboost_test)  
  57.     auc = roc_auc_score(y_test, y_xgboost_test)  
  58.     print("Xgboost:", auc)  
  59.     return fpr_xgboost, tpr_xgboost  
  60.   
  61. def Lr():  
  62.     lm = LogisticRegression(n_jobs=4, C=0.1, penalty='l1')  
  63.     lm.fit(X_train, y_train)  
  64.     y_lr_test = lm.predict_proba(X_test)[:, 1]  
  65.     fpr_lr, tpr_lr, _ = roc_curve(y_test, y_lr_test)  
  66.     auc = roc_auc_score(y_test, y_lr_test)  
  67.     print("LR:", auc)  
  68.     return fpr_lr, tpr_lr  
  69.   
  70. def XgboostLr():  
  71.     xgboost = xgb.XGBClassifier(nthread=4, learning_rate=0.08,\  
  72.                                 n_estimators=50, max_depth=5, gamma=0, subsample=0.9, colsample_bytree=0.5)  
  73.     xgb_enc = OneHotEncoder()  
  74.     xgb_lr = LogisticRegression(n_jobs=4, C=0.1, penalty='l1')  
  75.     xgboost.fit(X_train, y_train)  
  76.   
  77.     xgb_enc.fit(xgboost.apply(X_train)[:, :])  
  78.     xgb_lr.fit(xgb_enc.transform(xgboost.apply(X_train_lr)[:, :]), y_train_lr)  
  79.     y_xgb_lr_test = xgb_lr.predict_proba(xgb_enc.transform(xgboost.apply(X_test)[:,:]))[:, 1]  
  80.     fpr_xgb_lr, tpr_xgb_lr, _ = roc_curve(y_test, y_xgb_lr_test)  
  81.     auc = roc_auc_score(y_test, y_xgb_lr_test)  
  82.     print("Xgboost + LR:", auc)  
  83.     return fpr_xgb_lr, tpr_xgb_lr  
  84.   
  85. if __name__ == '__main__':  
  86.     fpr_rf_lr, tpr_rf_lr = RandomForestLR()  
  87.     fpr_grd_lr, tpr_grd_lr = GdbtLR()  
  88.     fpr_xgboost, tpr_xgboost = Xgboost()  
  89.     fpr_lr, tpr_lr = Lr()  
  90.     fpr_xgb_lr, tpr_xgb_lr = XgboostLr()  
  91.   
  92.     plt.figure(1)  
  93.     plt.plot([01], [01], 'k--')  
  94.     plt.plot(fpr_rf_lr, tpr_rf_lr, label='RF + LR')  
  95.     plt.plot(fpr_grd_lr, tpr_grd_lr, label='GBT + LR')  
  96.     plt.plot(fpr_xgboost, tpr_xgboost, label='XGB')  
  97.     plt.plot(fpr_lr, tpr_lr, label='LR')  
  98.     plt.plot(fpr_xgb_lr, tpr_xgb_lr, label='XGB + LR')  
  99.     plt.xlabel('False positive rate')  
  100.     plt.ylabel('True positive rate')  
  101.     plt.title('ROC curve')  
  102.     plt.legend(loc='best')  
  103.     plt.show()  
  104.   
  105.     plt.figure(2)  
  106.     plt.xlim(00.2)  
  107.     plt.ylim(0.81)  
  108.     plt.plot([01], [01], 'k--')  
  109.     plt.plot(fpr_rf_lr, tpr_rf_lr, label='RF + LR')  
  110.     plt.plot(fpr_grd_lr, tpr_grd_lr, label='GBT + LR')  
  111.     plt.plot(fpr_xgboost, tpr_xgboost, label='XGB')  
  112.     plt.plot(fpr_lr, tpr_lr, label='LR')  
  113.     plt.plot(fpr_xgb_lr, tpr_xgb_lr, label='XGB + LR')  
  114.     plt.xlabel('False positive rate')  
  115.     plt.ylabel('True positive rate')  
  116.     plt.title('ROC curve (zoomed in at top left)')  
  117.     plt.legend(loc='best')  
  118.     plt.show()  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值