文章目录
0.问题描述
这份数据集是金融数据(非原始数据,已经处理过了),我们要做的是预测贷款用户是否会逾期。表格中 “status” 是结果标签:0表示未逾期,1表示逾期。
构建LR、决策树、SVM、随机森林、GBDT、XGBoost和LightGBM这7个模型,评分方式任意。
1.评价指标
记录7个模型(逻辑回归、SVM、决策树、随机森林、GBDT、XGBoost和LightGBM)关于accuracy、precision,recall和F1-score、auc值的评分表格,并画出ROC曲线。
2.模型构建
模型构建的流程如下:
2.1数据导入
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn import svm
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import train_test_split
import xgboost as xgb
import lightgbm as lgb
from sklearn.preprocessing import MinMaxScaler
#核算指标的包
from sklearn.metrics import roc_auc_score
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
from sklearn.metrics import roc_curve,auc
import matplotlib.pyplot as plt
%matplotlib inline
项目 | Value |
---|---|
0 | 3561 |
1 | 1193 |
跟之前猜测差不多,并没有特别均衡,后续再优化,建议可以采用auc-roc指标替换
2.2数据切分
# 切分数据集,引入随机数,以便模型结果复线
x = data.drop(['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)
2.3模型构建
不同于任务一,需要尝试新的4个数模型。
2.3.1模型对象化
# 4个模型对象化,引入随机数,以便模型结果复线
xgb_clf = xgb.XGBClassifier(random_state=2018)
lgb_clf = lgb.LGBMClassifier(random_state=2018)
gbdt_clf = GradientBoostingClassifier(random_state=2018)
rf_clf = RandomForestClassifier(random_state=2018)
#逻辑回归
lr = LogisticRegression(random_state=2018)
# lr.fit(x_train, y_train)
#SVM
svc = svm.SVC(random_state=2018)
# svc.fit(x_train, y_train)
#决策树
dt = DecisionTreeClassifier(random_state=2018)
# dt.fit(x_train, y_train)
2.3.2 构建模型字典,以便后续引用
#建立一个模型的字典,以便后续批量模型
model_dict = {"rf":rf_clf,"gbdt":gbdt_clf,"xgb":xgb_clf,"lgb":lgb_clf,"LR":lr,"SVC":svc,"DT":dt }
**
2.4模型训练及评价
2.4.1封装模型训练及预测评价
#封装评价函数,避免代码冗余
def get_scores(y_true, y_predicet, y_predict_pro):
acc_score = accuracy_score(y_true,y_predicet)
pre_score = precision_score(y_true,y_predicet)
recall = recall_score(y_true,y_predicet)
F1 = f1_score(y_true,y_predicet)
auc_score = roc_auc_score(y_true,y_predict_pro)#直接求AUC值
fpr, tpr, thresholds = roc_curve(y_test,y_predict_pro)#求ROC
plt.plot(fpr,tpr,'b',label='AUC = %0.4f'% auc_score)
plt.plot([0,1],[0,1],'r--',label= 'Random guess')
plt.legend(loc='lower right')
plt.title('ROCcurve')
plt.xlabel('false positive rate')
plt.ylabel('true positive rate')
plt.show()
print('准确率:',acc_score)
print('精确率:',pre_score)
print('召回率:',recall)
print('F1-score:',F1)
print('AUC',auc_score)
#封装一个总的预测画图函数,始得可以一次性运行所有模型,避免代码冗余
def runModel():
for i in model_dict.keys():
print(i,"-training")
clf = model_dict[i].fit(x_train, y_train)
score = clf.score(x_test,y_test)
print(i,"-Score:,",score)
clf_predict = clf.predict(x_test)
clf_predict_proba = clf.predict_proba(x_test)[:,1]
get_scores(y_test,clf_predict,clf_predict_proba)
2.4.1模型运行及评价结果
runModel()
最后结果如下:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20181222220517589.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NoaW5zb253dQ==,size_16,color_FFFFFF,t_70)
在没有任何调参以及特征工程的情况下,xgb预测准确率最高。