金融贷款逾期的模型构建2——集成模型
生活随笔
收集整理的這篇文章主要介紹了
金融贷款逾期的模型构建2——集成模型
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
任務(wù)——模型構(gòu)建
構(gòu)建隨機(jī)森林、GBDT、XGBoost和LightGBM這4個(gè)模型,并對每一個(gè)模型進(jìn)行評分,評分方式任意,例如準(zhǔn)確度和auc值。
1、相關(guān)安裝資源
- 隨機(jī)森林、GBDT均在sklearn包中;
- LightGBM:https://github.com/Microsoft/LightGBM
- 目前已經(jīng)是pypi中的資源 ==》pip方式安裝
- XGBoost:https://www.lfd.uci.edu/~gohlke/pythonlibs/#xgboost、https://github.com/dmlc/xgboost
Tips:若 pip 安裝過程中,網(wǎng)速、超時(shí)等 ==》換源
sudo pip install -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com/simple lightgbm2、數(shù)據(jù)讀取 + 標(biāo)準(zhǔn)化
import pandas as pd from sklearn.model_selection import train_test_split import xgboost as xgb import lightgbm as lgb from sklearn.ensemble import RandomForestClassifier from sklearn.ensemble import GradientBoostingRegressor import warnings from sklearn.preprocessing import StandardScalerwarnings.filterwarnings(action ='ignore', category = DeprecationWarning)## 讀取數(shù)據(jù) data = pd.read_csv("data_all.csv") x = data.drop(labels='status', axis=1) y = data['status'] x_train, x_test, y_train, y_test = train_test_split(x, y,test_size=0.3,random_state=2018) print(len(x)) # 4754## 數(shù)據(jù)標(biāo)準(zhǔn)化 scaler = StandardScaler() scaler.fit(x_train) x_train_stand = scaler.transform(x_train) x_test_stand = scaler.transform(x_test)3、 隨機(jī)森林模型
思想:通過 Bagging 的思想將多棵樹集成的一種算法,它的基本單元是決策樹。
rfc = RandomForestClassifier() rfc.fit(x_train, y_train) rfc_score = rfc.score(x_test, y_test) print("The score of RF:",rfc_score)rfc1 = RandomForestClassifier() rfc1.fit(x_train_stand, y_train) rfc1_score = rfc1.score(x_test_stand, y_test) print("The score of RF(with preprocessing):",rfc1_score)輸出結(jié)果
The score of RF: 0.7638402242466713 The score of RF(with preprocessing): 0.76524176594253684、GBDT模型
GBDT 的全稱是 Gradient Boosting Decision Tree,梯度下降樹。
思想:通過損失函數(shù)的負(fù)梯度來擬合
輸出結(jié)果:
The score of GBDT: 0.181180754059806715、XGBoost模型
xgb = xgb.XGBClassifier() xgb.fit(x_train, y_train) xgb_score = xgb.score(x_test, y_test) print("The score of XGBoost:", xgb_score)輸出結(jié)果
The score of XGBoost: 0.7855641205325858遇到的問題
DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.if diff:==》經(jīng)過在網(wǎng)上查找問題發(fā)現(xiàn):這是一個(gè)numpy問題,在空數(shù)組上棄用了真值檢查。該問題numpy已經(jīng)修復(fù)。
==》解決方案1:忽略警告2
6、lightGBM
思想:LightGBM 是一個(gè)梯度 boosting 框架,使用基于學(xué)習(xí)算法的決策樹。它可以說是分布式的,高效的,有以下優(yōu)勢:
更快的訓(xùn)練效率 低內(nèi)存使用 更高的準(zhǔn)確率 支持并行化學(xué)習(xí) 可處理大規(guī)模數(shù)據(jù)
輸出結(jié)果
The score of LightGBM: 0.18118075405980671總結(jié)
以上是生活随笔為你收集整理的金融贷款逾期的模型构建2——集成模型的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Python】编程笔记4
- 下一篇: 【Python】编程笔记5