【天池】金融风控贷款违约预测task5

task5学习总结:

1)简单平均和加权平均是常用的两种比赛中模型融合的方式。其优点是快速、简单。
2)stacking在众多比赛中大杀四方,但是跑过代码的小伙伴想必能感受到速度之慢,同时stacking多层提升幅度并不能抵消其带来的时间和内存消耗,所以实际环境中应用还是有一定的难度。
3)当然在比赛中将加权平均、stacking、blending等混用也是一种策略。

# task5 模型融合
# 方法:1)平均:简单、加权;2)投票:简单、加权;3)综合:排序融合、log融合;4)stacking;5)blending
# 5.1. 平均
pre = (pre1 + pre2 + pre3 +...+pren )/n   #简单平均
pre = 0.3pre1 + 0.3pre2 + 0.4pre3     #根据先前模型准确率赋权# 5.2. 投票
from xgboost import XGBClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, VotingClassifier# 简单投票
clf1 = LogisticRegression(random_state=1)
clf2 = RandomForestClassifier(random_state=1)
clf3 = XGBClassifier(learning_rate=0.1, n_estimators=150, max_depth=4, min_child_weight=2, subsample=0.7,objective='binary:logistic')vclf = VotingClassifier(estimators=[('lr', clf1), ('rf', clf2), ('xgb', clf3)])
vclf = vclf .fit(x_train,y_train)
print(vclf .predict(x_test))# 加权投票
# 在VotingClassifier中加入参数 voting='soft', weights=[2, 1, 1],weights用于调节基模型的权重
clf1 = LogisticRegression(random_state=1)
clf2 = RandomForestClassifier(random_state=1)
clf3 = XGBClassifier(learning_rate=0.1, n_estimators=150, max_depth=4, min_child_weight=2, subsample=0.7,objective='binary:logistic')vclf = VotingClassifier(estimators=[('lr', clf1), ('rf', clf2), ('xgb', clf3)], voting='soft', weights=[2, 1, 1])
vclf = vclf .fit(x_train,y_train)
print(vclf .predict(x_test))
# 5.3. stacking
import warnings
warnings.filterwarnings('ignore')
import itertools
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import RandomForestClassifier
from mlxtend.classifier import StackingClassifier
from sklearn.model_selection import cross_val_score, train_test_split
from mlxtend.plotting import plot_learning_curves
from mlxtend.plotting import plot_decision_regions
%matplotlib inline# 以python自带的鸢尾花数据集为例
iris = datasets.load_iris()
X, y = iris.data[:, 1:3], iris.targetclf1 = KNeighborsClassifier(n_neighbors=1)
clf2 = RandomForestClassifier(random_state=1)
clf3 = GaussianNB()
lr = LogisticRegression()
sclf = StackingClassifier(classifiers=[clf1, clf2, clf3], meta_classifier=lr)label = ['KNN', 'Random Forest', 'Naive Bayes', 'Stacking Classifier']
clf_list = [clf1, clf2, clf3, sclf]fig = plt.figure(figsize=(10,8))
gs = gridspec.GridSpec(2, 2)
grid = itertools.product([0,1],repeat=2)clf_cv_mean = []
clf_cv_std = []
for clf, label, grd in zip(clf_list, label, grid):scores = cross_val_score(clf, X, y, cv=5, scoring='accuracy')print("Accuracy: %.2f (+/- %.2f) [%s]" %(scores.mean(), scores.std(), label))clf_cv_mean.append(scores.mean())clf_cv_std.append(scores.std())clf.fit(X, y)ax = plt.subplot(gs[grd[0], grd[1]])fig = plot_decision_regions(X=X, y=y, clf=clf)plt.title(label)plt.show()
Accuracy: 0.91 (+/- 0.07) [KNN]
Accuracy: 0.94 (+/- 0.04) [Random Forest]
Accuracy: 0.91 (+/- 0.04) [Naive Bayes]
Accuracy: 0.94 (+/- 0.04) [Stacking Classifier]

# 5.4. blending
# 以python自带的鸢尾花数据集为例
data_0 = iris.data
data = data_0[:100,:]target_0 = iris.target
target = target_0[:100]#模型融合中基学习器
clfs = [LogisticRegression(),RandomForestClassifier(),ExtraTreesClassifier(),GradientBoostingClassifier()]#切分一部分数据作为测试集
X, X_predict, y, y_predict = train_test_split(data, target, test_size=0.3, random_state=914)#切分训练数据集为d1,d2两部分
X_d1, X_d2, y_d1, y_d2 = train_test_split(X, y, test_size=0.5, random_state=914)
dataset_d1 = np.zeros((X_d2.shape[0], len(clfs)))
dataset_d2 = np.zeros((X_predict.shape[0], len(clfs)))for j, clf in enumerate(clfs):#依次训练各个单模型clf.fit(X_d1, y_d1)y_submission = clf.predict_proba(X_d2)[:, 1]dataset_d1[:, j] = y_submission#对于测试集,直接用这k个模型的预测值作为新的特征。dataset_d2[:, j] = clf.predict_proba(X_predict)[:, 1]print("val auc Score: %f" % roc_auc_score(y_predict, dataset_d2[:, j]))#融合使用的模型
clf = GradientBoostingClassifier()
clf.fit(dataset_d1, y_d2)
y_submission = clf.predict_proba(dataset_d2)[:, 1]
print("Val auc Score of Blending: %f" % (roc_auc_score(y_predict, y_submission)))

【天池】金融风控贷款违约预测task5相关推荐

  1. Datawhale学习笔记【阿里云天池 金融风控-贷款违约预测】Task2 数据分析

    阿里云天池学习赛[金融风控-贷款违约预测] 赛题数据及背景 python库的导入 国内镜像源网址及使用方法 镜像使用方法 文件读取 数据的总体了解 查看数据集中特征缺失值,唯一值等 检查缺失值 缺失值 ...

  2. DataWhale天池-金融风控贷款违约预测-Task01赛题理解

    目录 一.赛题概况 二.数据集介绍 三.预测指标 理解 通过ROC曲线评估分类器 最佳阈值点选择 一.赛题概况 本次新人赛是Datawhale与天池联合发起的0基础入门系列赛事第四场 -- 零基础入门 ...

  3. 1.天池金融风控-贷款违约预测新人赛之预备知识

    比赛链接:金融风控-贷款违约预测 因为这是一个金融风控专题的数据挖掘实战,在开始之前先引入一些预备知识. 1.预备知识 1.1预测指标 本次竞赛用AUC作为评价指标,AUC为ROC曲线下与坐标轴围成的 ...

  4. 【天池】金融风控-贷款违约预测(五)—— 模型融合

    [天池]金融风控-贷款违约预测(五)-- 模型融合 前言 内容介绍 stacking\blending详解 代码示例 总结 前言 [天池]金融风控-贷款违约预测(赛题链接). 上一篇进行数据建模和模型 ...

  5. 阿里天池学习赛-金融风控-贷款违约预测

    阿里天池学习赛-金融风控-贷款违约预测 1 赛题理解 1.1 赛题数据 1.2 评测标准 2 探索性分析(EDA) 2.1 初窥数据 2.2 查看缺失值占比 2.3 数值型变量 2.3.1 数据分布 ...

  6. 阿里天池零基础入门金融风控-贷款违约预测文本处理

    阿里天池零基础入门金融风控-贷款违约预测文本处理 文本处理 日期处理 等级处理 就业年限处理 删除含有空值的行 数据归一化 踩坑 文本处理 日期处理 earliesCreditLine: 可以看到ea ...

  7. 「机器学习」天池比赛:金融风控贷款违约预测

    一.前言 1.1 赛题背景 赛题以金融风控中的个人信贷为背景,要求选手根据贷款申请人的数据信息预测其是否有违约的可能,以此判断是否通过此项贷款,这是一个典型的分类问题. 任务:预测用户贷款是否违约 比 ...

  8. 基于机器学习与深度学习的金融风控贷款违约预测

    基于机器学习与深度学习的金融风控贷款违约预测 目录 一.赛题分析 1. 任务分析 2. 数据属性 3. 评价指标 4. 问题归类 5. 整体思路 二.数据可视化分析 1. 总体数据分析 2. 数值型数 ...

  9. 金融风控-贷款违约预测项目记录

    一.项目介绍 阿里天池数据挖掘学习赛:贷款违约预测 学习赛主要为初学者准备的,论坛里有详细的项目建模过程.这里,仅记录一下自己在做这个项目时的一些思路. 二.数据分析 这里的数据分析过程主要参考有: ...

最新文章

  1. 开源wkhtmltopdf使用心得 (四)
  2. 基于小波变换的图像压缩解压缩仿真
  3. 文巾解题 12. 整数转罗马数字
  4. C. Anton and Making Potions 贪心 + 二分
  5. java堆设置成多少合适_jvm~xmx设置多少合适
  6. oracle xe 乱码_关于Linux操作系统下终端乱码的完美解决方法
  7. Socket模型详解
  8. Linux管理文件和目录
  9. C语言通过网络实现发送文件的一点记录
  10. docker删除mongo数据库库_Docker 搭建MongoDB环境
  11. python opencv 摄像头标定_(五)单目摄像头标定与畸变矫正(C++,opencv)
  12. 让 Cloud Native 飞,Pick 干货,看这里、看这里!
  13. 华为承诺的鸿蒙系统兑现没有,华为鸿蒙系统遭截胡,安卓12系统率先出手
  14. 清华大学王行言教授DELPHI程序设计在线学习
  15. 【跟踪算法】MOSSE论文翻译
  16. QPSK、16QAM、64QAM信号的散点图、正交、同相分量波形图
  17. 网络流-最大流(Ford-Fulkerson算法Dinic算法)
  18. 从阿里巴巴发行价看A股新股投资机会
  19. 我们扒了扒那个“阿里美女高管”,真的不简单(附最新回应)
  20. 【蓝桥真题5】带三百人训练了十天精选蓝桥真题,看看他们都练些什么(三门语言题解)

热门文章

  1. python自动化介绍
  2. Flutter支付宝支付
  3. MacBook Pro电池维修记
  4. 【职场必备知识】毕业留蓉政策与发展前景分析
  5. 计算机网络 自顶向下方法 第二章 应用层
  6. 一款可以排查重复照片,清理多余照片的照片管理软件,使用PowerPhotos的图像浏览器快速查看照片
  7. 李沐精读论文:MAE 《Masked Autoencoders Are Scalable Vision Learners》
  8. HTML+CSS+JS购物网站制作【学生HTML静态网页作业作品】
  9. Unity Pico老版SDK手柄功能编写
  10. 奔驰809android auto,奔驰E Android Auto+AA Mirro技术作业