lightgbm与贷款违约预测项目
lightgbm
- histogram算法
- 将连续的浮点值离散成k个离散值,构造宽度为k的histogram
- leaf-wise生长策略
- 每次在所有叶子中找到分裂增益最大的一个叶子,一般也是数据量最大的
参数
- num_leaves 叶节点数目,<=2^(max_depth)
- max_depth [1]
调参
- hyperopt:自动获取最佳的超参数。
pip install hyperopt
import hyperopt def hyperopt_objective(params): model=lgb.LGBMRegressor(
num_leaves=31,learning_rate=0.1,n_estimators=int(params["n_estimators"]),max_depth=int(params["max_depth"]),objective="binary",eval_metric="auc")
res=lgb.cv(model.get_params(),train_matrix,nfold=5,early_stopping_rounds=10,metrics="auc")
return -max(res["auc-mean"])
定义一个目标函数hyperopt_objective,由于fmin返回最小值,因此用-auc
params_space={
"n_estimators":hyperopt.hp.randint("n_estimators",300),
"max_depth":hyperopt.hp.randint("max_depth",8) }
定义搜索空间:
hp.uniform(label,low,high)参数在low和high之间均匀分布; hp.quniform(label,low,high,q)参数的取值是round(uniform(low,high)/q)*q,适用于那些离散的取值 hp.randint(label,upper)返回一个在[0,upper)前闭后开的区间内的随机整数。
trials=hyperopt.Trials()
best=hyperopt.fmin(hyperopt_objective,space=params_space,algo=hyperopt.tpe.suggest,
max_evals=10,trials=trials)
在搜索空间内搜索
- hyperopt:自动获取最佳的超参数。
阿里天池大赛:金融风控-贷款违约预测
- 导包
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"#显示全部行输出结果
2. 导入数据
train=pd.read_csv("/风控/train (1).csv")
train.head()#默认显示前五行
train.shape
train.columns#查看字段信息
3. 区分离散型和连续型变量
numerical_columns=['id', 'loanAmnt', 'term', 'interestRate', 'installment', 'employmentTitle', 'homeOwnership',
'annualIncome', 'verificationStatus',
'purpose', 'postCode', 'regionCode', 'dti', 'delinquency_2years',
'ficoRangeLow', 'ficoRangeHigh', 'openAcc', 'pubRec',
'pubRecBankruptcies', 'revolBal', 'revolUtil', 'totalAcc',
'initialListStatus', 'applicationType', 'title',
'policyCode', 'n0', 'n1', 'n2', 'n3', 'n4', 'n5', 'n6', 'n7', 'n8',
'n9', 'n10', 'n11', 'n12', 'n13', 'n14']
def featype(data,feature):
numerical_continus=[]
numerical_discrete=[]
for fea in feature:
count=data[fea].nunique() #返回唯一值的个数
if count>10:
numerical_continus.append(fea)
else:
numerical_discrete.append(fea)
return numerical_continus,numerical_discrete
numerical_continus,numerical_discrete=featype(train,numerical_columns)
numerical_continus
numerical_discrete
4. 处理字符型变量apply
employmentlength_dict={"10+ years":10,"2 years":2,"< 1 year":0,"1 year":1,"5 years":5,"4 years":4,"6 years":6,"8 years":8,"7 years":7,"9 years":9,"3 years":3}
def func4(m):
m["employmentlength_dict"]=m["employmentLength"].apply(lambda x:x if x not in employmentlength_dict else employmentlength_dict[x])
return m
train=func4(train)
def func(x):
month,year=x.split("-")
month_dict={"Aug":8,"Nov":11,"Feb":2,"Jan":1,"Mar":3,"Jul":7,"Oct":10,"Jun":6,"Apr":4,"Sep":9,"May":5,"Dec":12}
month_dict=month_dict[month]
earlistdate=int(year)*12+int(month_dict)
return earlistdate
train["earlistdate"]=train["earliesCreditLine"].apply(lambda x:func(x))
def func2(x):
year,month,day=x.split("-")
final_date=int(year)*12+int(month)
return final_date
train["issueDate_dict"]=train["issueDate"].apply(lambda x:func2(x))
train[["subGrade","interestRate","grade"]].corr()#subgrade grade 为object
def func3(x):
tmp=x[["subGrade"]].sort_values(["subGrade"]).drop_duplicates()
tmp["subgrade_dict"]=range(len(tmp))
x=x.merge(tmp,on="subGrade",how="left")
tmp1=x[["grade"]].sort_values(["grade"]).drop_duplicates()
tmp1["grade_dict"]=range(len(tmp1))
x=x.merge(tmp1,on="grade",how="left")
return x
train=func3(train)
5.填充空值:离散型用众数,连续型用中位数
#查看变量缺失值占比
d=(train.isnull().sum()/train.shape[0]).to_dict()
d
(train.isnull().sum()/train.shape[0]).plot.bar()
train[numerical_continus]=train[numerical_continus].fillna(value=train[numerical_continus].median())
train[numerical_discrete]=train[numerical_discrete].fillna(value=train[numerical_discrete].mode())
category=["employmentlength_dict","subgrade_dict","grade_dict","issueDate_dict",'earlistdate']
train[category]=train[category].fillna(train[category].mode())
6. 参数优化
import lightgbm as lgb
from sklearn.model_selection import train_test_split
x=train.drop(["grade","subGrade","employmentLength","issueDate","earliesCreditLine","isDefault"],axis=1)
y=train["isDefault"]
# 数据集划分
x_train, x_val, y_train, y_val = train_test_split(x, y, test_size=0.2)
train_matrix = lgb.Dataset(x_train, label=y_train)
valid_matrix = lgb.Dataset(x_val, label=y_val)
import hyperopt
def hyperopt_objective(params):
model=lgb.LGBMRegressor(num_leaves=31,learning_rate=0.1,n_estimators=int(params["n_estimators"]),max_depth=int(params["max_depth"]),objective="binary",eval_metric="auc")
res=lgb.cv(model.get_params(),train_matrix,nfold=5,early_stopping_rounds=10,metrics="auc")
return -max(res["auc-mean"])
params_space={
"n_estimators":hyperopt.hp.randint("n_estimators",300),
"max_depth":hyperopt.hp.randint("max_depth",8)
}
trails=hyperopt.Trials()
best=hyperopt.fmin(hyperopt_objective,space=params_space,algo=hyperopt.tpe.suggest,max_evals=10,trials=trails)
print(best)
```{'max_depth': 6, 'n_estimators': 237}
7. 训练模型
params = {
'boosting_type': 'gbdt',
'objective': 'binary',
'learning_rate': 0.1,#较小的学习率,较大的决策树个数
"n_estimators":237,
'metric': 'auc',
'num_leaves': 31,
'max_depth': 6,#树的最大深度,防止过拟合
'feature_fraction': 1, #每次选择所有的特征训练树
'bagging_fraction': 1,
}
"""使用训练集数据进行模型训练"""
model = lgb.train(params, train_set=train_matrix, valid_sets=valid_matrix, num_boost_round=20000, verbose_eval=1000, early_stopping_rounds=200)
8. 数据预测
test=pd.read_csv("C:/Users/廖言/Desktop/新建文件夹/努力学习天天向上/风控/testA.csv")
test.head()
#数据处理
test=func4(test)
test["earlistdate"]=test["earliesCreditLine"].apply(lambda x:func(x))
test["issueDate_dict"]=test["issueDate"].apply(lambda x:func2(x))
test=func3(test)
#空值填充
test[numerical_continus]=test[numerical_continus].fillna(value=test[numerical_continus].median())
test[numerical_discrete]=test[numerical_discrete].fillna(value=test[numerical_discrete].mode())
test[category]=test[category].fillna(test[category].mode())
test2=test.drop(["grade","subGrade","employmentLength","issueDate","earliesCreditLine"],axis=1)
#数据预测
test["isDefault"]=model.predict(test2)
#数据输出
test.to_csv("C:/Users/廖言/Desktop/新建文件夹/努力学习天天向上/风控/output3.csv")
树的深度是从根点到叶子节点的结点数,叶子节点是没有左右孩子的结点。
- n_estimators 迭代次数=决策树个数
- bagging_fraction 每次迭代用的数据比例,小比例加快训练速度,减小过拟合
- feature_fraction 每次迭代用的特征比例
- min_data_in_leaf 每个叶节点的最少样本数量,设置一个较大的数可以处理过拟合 ︎
lightgbm与贷款违约预测项目的更多相关文章
- Lending Club—构建贷款违约预测模型
python信用评分卡(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_camp ...
- Datawhale 人工智能培养方案
版本号:V0.9 阅读须知 每个专业方向对应一个课程表格 课程表格里的课程排列顺序即为本培养方案推荐的学习顺序 诚挚欢迎为本培养方案贡献课程,有意向的同学请联系Datawhale开源项目管理委员会 本 ...
- R语言-来自Prosper的贷款数据探索
案例分析:Prosper是美国的一家P2P在线借贷平台,网站撮合了一些有闲钱的人和一些急用钱的人.用户若有贷款需求,可在网站上列出期望数额和可承受的最大利率.潜在贷方则为数额和利率展开竞价. 本项目拟 ...
- kaggle 欺诈信用卡预测——Smote+LR
from:https://zhuanlan.zhihu.com/p/30461746 本项目需解决的问题 本项目通过利用信用卡的历史交易数据,进行机器学习,构建信用卡反欺诈预测模型,提前发现客户信用卡 ...
- 基于Spark.NET和ML.NET Automated ML (自动学习)进行餐厅等级的检查预测
简介 Apache Spark是一个开源.分布式.通用的分析引擎.多年来,它一直是大数据生态系统中对大型数据集进行批量和实时处理的主要工具.尽管对该平台的本地支持仅限于JVM语言集,但其他通常用于数据 ...
- 数据挖掘项目之---通过对web日志的挖掘来实现内容推荐系统
先说一说问题,不知道大家有没有这样的经验,反正我是经常碰到. 举例1,某些网站每隔几天就发邮件给我,每次发的邮件内容都是一些我根本不感兴趣的东西,我不甚其扰,对其深恶痛绝. 举例2,添 ...
- 由Kaggle竞赛wiki文章流量预测引发的pandas内存优化过程分享
pandas内存优化分享 缘由 最近在做Kaggle上的wiki文章流量预测项目,这里由于个人电脑配置问题,我一直都是用的Kaggle的kernel,但是我们知道kernel的内存限制是16G,如下: ...
- 【SVM】kaggle之澳大利亚天气预测
项目目标 由于大气运动极为复杂,影响天气的因素较多,而人们认识大气本身运动的能力极为有限,因此天气预报水平较低,预报员在预报实践中,每次预报的过程都极为复杂,需要综合分析,并预报各气象要素,比如温度. ...
- Python爱好者社区历史文章列表(每周append更新一次)
2月22日更新: 0.Python从零开始系列连载: Python从零开始系列连载(1)——安装环境 Python从零开始系列连载(2)——jupyter的常用操作 Python从零开始系列连载( ...
- 【干货】Kaggle 数据挖掘比赛经验分享(mark 专业的数据建模过程)
简介 Kaggle 于 2010 年创立,专注数据科学,机器学习竞赛的举办,是全球最大的数据科学社区和数据竞赛平台.笔者从 2013 年开始,陆续参加了多场 Kaggle上面举办的比赛,相继获得了 C ...
随机推荐
- uni-app学习笔记之----getCurrentPages()的使用
1.判断是否是首页 如果得到数组元素只有一个,说明是首页 2.得到页面中的信息 得到数组中的第一个元素代表首页,最后一个元素代表当前页
- es实现规格分组分析
es里面的规格是重复的,页面显示则是不重复
- FileZilla链接centos虚拟机不显示问题
这段时间因为不是很忙,一直想学习一下网络安全方面的技术,准备考个信安中级,网络安全方面经常用到虚拟机,所以及用vm新建虚拟机后,一直发现连不上FileZilla,于是就在网上搜了一下,有一些参考问题, ...
- linux 的防火墙 ufw、firwalld、iptables 、
防火墙综述 linux 防火墙,常用的包括三种:ufw . firewalld 和 iptables.学习难度依次递增. ufw 因为原生的 iptable 配置麻烦,学习成本较高. ufw全称 Un ...
- API对象--Service(chrono《kubernetes入门实战课》笔记整理)
[概念解说] 当pod被实例化出来,如果运行 一段时间会销毁,虽然deployment和ds会注意管理和维护pod的数目,但是pod销毁后再重建,ip会发生变化,这对于服务来说,是很麻烦的.所以需要有 ...
- 日志参数 %n 引起的coredump
今天测试发现一段代码 coredump,居然是一行日志输出.看参数都乱了,以为是内存溢出造成的.查了半天,也没发现问题 最后把前边的函数调用都注释掉,只运行这一段日志输出,依然挂掉 仔细一看: TRA ...
- python中items()和iteritems()的区别
items()函数,将一个字典以dict_items的形式返回,因为字典是无序的,所以返回的列表也是无序的: 1 a ={'a':1,'b':2,'c':3,'d':4} 2 print(a.item ...
- 查询正在执行的SQL的数据库名和表名
创建限制0.5个CPU和0.5G内存的MySQL容器 docker run -itd --name mysql --cpu-quota=50000 --memory 512M --rm -p 3306 ...
- 查询dockerhub中某镜像所有版本
curl https://registry.hub.docker.com/v1/repositories/${imagename}/tags | tr -d '[[]" ]' | tr '} ...
- Flutter 中的普通路由、普通路由传值、 命名路由、命名路由传值
一.Flutter 中的路由 Flutter 中的路由通俗的讲就是页面跳转.在 Flutter 中通过 Navigator 组件管理路由导航.并提供了管理堆栈的方法.如:Navigator.push ...