85 R 银行信用卡风控评分数据分析
R 银行信用卡风控评分数据分析
- 1 初始环境准备
- 2数据预览与处理
- 3 描述性统计
- 相关包准备
- 盖帽法
- 描述数据分类统计
- 4 属性分箱
- 5 logistic建模
- 6 打分系统
- 7 写入csv文件
1 初始环境准备
读取数据与预览
rm(list=ls())
#setwd("./case")
#install.packages("xlsx")
library(openxlsx)
dat<-read.xlsx("credit.xlsx",1)
View(dat)
2数据预览与处理
数据预览,发现最大值999的异常值,偏离平均值
class(dat)
#describe data
summary(dat)
sum(is.na(dat))
异常值处理,使用na填充
#Outlier filling
dat[,1:6]<-sapply(dat[,1:6],function(x) {x[x==999]<-NA;return(x)} )
nrow(dat)
ncol(dat)
summary(dat[,11])
#Understand data deletion invalid variables
dat<-dat[,-11]
将字符型变量character转化为因子变量factor
#Change string variable type to classification variable
dat1<-dat
sapply(dat1,class)
ch=names(which(sapply(dat1,is.character)))#find the character type variance
dat1[,ch]=as.data.frame(lapply(dat1[,ch], as.factor))
观察数据发现家庭人口数和家庭孩子数密切相关,存在多重共线性,于是产生标识变量代替这二个变量
dat1[,4]<-dat1[,4]-dat1[,3]table(dat1[,4])
dat1[,4]<-factor(dat1[,4],levels=c(1,2),labels=c("其他","已婚"))colnames(dat1)<-c("age","income","child","marital","dur_live","dur_work","housetype","nation","cardtype","loan")
summary(dat1)
3 描述性统计
相关包准备
#install.packages("smbinning")
#install.packages("prettyR")
library(smbinning)
library(prettyR)library(mvtnorm)
library(kernlab)
盖帽法
异常值可以使用盖帽法处理,使用1%和99%分位数替换异常值
##盖帽法函数 去除异常用99%和1%点分别代替异常值
block<-function(x,lower=T,upper=T){if(lower){q1<-quantile(x,0.01)x[x<=q1]<-q1}if(upper){q99<-quantile(x,0.99)x[x>q99]<-q99}return(x)
}
数据集中1是不违约,0是违约,进行反转设定,使1变为违约,0为不违约
#Odds ratio conversion for later IV calculation
dat1$loan<-as.numeric(!as.logical(dat1$loan))
描述数据分类统计
违约和不违约的人群的区别
#data classification ,discretization of continuous variables
##age
boxplot(age~loan,data=dat1,horizontal=T, frame=F, col="lightgray",main="Distribution")
age<-smbinning(dat1,"loan","age")
age$ivtable
违约与否的年龄箱线图
分箱后的IV图
age<-smbinning(dat1,"loan","age")
age$ivtable
对年龄进行分箱后,查看百分比,weight,good,bad rate,后面的描述性统计大致如此,可以秦楚看出不同年龄层次的区别
par(mfrow=c(2,2))
smbinning.plot(age,option="dist",sub="年龄")
smbinning.plot(age,option="WoE",sub="年龄")
smbinning.plot(age,option="goodrate",sub="年龄")
smbinning.plot(age,option="badrate",sub="年龄"
将IV结果添加到一个向量中
par(mfrow=c(1,1))
age$iv
#Add Iv value to vector
cred_iv<-c("年龄"=age$iv)
关于收入,明显存在异常值,使用盖帽法
##incomeboxplot(income~loan,data=dat1,horizontal=T, frame=F, col="lightgray",main="Distribution")
盖帽法填充
dat1$income<-block(dat1$income)
填充后明显变正常了
boxplot(income~loan,data=dat1,horizontal=T, frame=F, col="lightgray",main="Distribution")```
IV值测量,同上age
income<-smbinning(dat1,"loan","income")
income$ivtable
smbinning.plot(income,option="WoE",sub="收入")
income$iv
cred_iv<-c(cred_iv,"收入"=income$iv)
child 统计
##child
boxplot(child~loan,data=dat1,horizontal=T, frame=F, col="lightgray",main="Distribution")child<-smbinning(dat1,"loan","child")
child$ivtable
smbinning.plot(child,option="WoE",sub="孩子数量")
child$iv
cred_iv<-c(cred_iv,"孩子数量"=child$iv)
##marital
xtab(~marital+loan,data=dat1,chisq=T)
marital<-smbinning.factor(dat1,"loan","marital")
marital$ivtable
smbinning.plot(marital,option="WoE",sub="婚姻状态")
marital$iv
cred_iv<-c(cred_iv,"婚姻状态"=marital$iv)
##dur_live
boxplot(dur_live~loan,data=dat1,horizontal=T, frame=F, col="lightgray",main="Distribution")
t.test(dur_live~loan,data=dat1)
dur_live<-smbinning(dat1,"loan","dur_live")
dur_live
观察得到dur_live变量对违约分布区别不大,使用t检验,不能拒绝二者同分布
dur_work变量统计
##dur_work
boxplot(dur_work~loan,data=dat1,horizontal=T, frame=F, col="lightgray",main="Distribution")
t.test(dur_work~loan,data=dat1)
dur_work<-smbinning(dat1,"loan","dur_work")
dur_work$ivtable
smbinning.plot(dur_work,option="WoE",sub="在现工作时间")
dur_work$iv
cred_iv<-c(cred_iv,"在现工作时间"=dur_work$iv)
housetype描述统计
##housetype
xtab(~housetype+loan,data=dat1,chisq=T)
housetype<-smbinning.factor(dat1,"loan","housetype")
housetype$ivtable
smbinning.plot(housetype,option="WoE",sub="住房类型")
housetype$iv
cred_iv<-c(cred_iv,"住房种类"=housetype$iv)
##nation
xtab(~nation+loan,data=dat1,chisq=T)
nation<-smbinning.factor(dat1,"loan","nation")
nation$ivtable
smbinning.plot(nation,option="WoE",sub="国籍")
nation$iv
cred_iv<-c(cred_iv,"国籍"=nation$iv)
cardtype描述统计
##cardtype
xtab(~cardtype+loan,data=dat1,chisq=T)
cardtype<-smbinning.factor(dat1,"loan","cardtype")
cardtype$ivtable
smbinning.plot(cardtype,option="WoE",sub="信用卡类型")
cardtype$iv
cred_iv<-c(cred_iv,"信用卡类型"=cardtype$iv)
总体变量IV值程度
#Drawing shows the amount of information
barplot(cred_iv,main="各变量信息值")
4 属性分箱
#quantity after adding classification
dat2<-dat1
dat2<-smbinning.gen(dat2,age,"glage")
dat2<-smbinning.gen(dat2,income,"glincome")
dat2<-smbinning.gen(dat2,child,"glchild")
dat2<-smbinning.factor.gen(dat2,marital,"glmarital")
dat2<-smbinning.gen(dat2,dur_work,"gldur_work")
dat2<-smbinning.factor.gen(dat2,housetype,"glhousetype")
dat2<-smbinning.factor.gen(dat2,nation,"glnation")
dat2<-smbinning.factor.gen(dat2,cardtype,"glcardtype")
View(dat2)
dat3<-dat2[,c(11:18,10)]View(dat3)
生成分箱后的数据级,将用户属性转化为区间数据
5 logistic建模
具体打分理论参考链接: link.
模型生成
#Creat logistic regression
cred_mod<-glm(loan~. ,data=dat3,family=binomial())
summary(cred_mod)
6 打分系统
依照打分公式发现,信用最高评分和最低评分分别为797,362分
#Scoring card system
cre_scal<-smbinning.scaling(cred_mod,pdo=45,score=800,odds=50)
cre_scal$logitscaled
cre_scal$minmaxscore
违约过与否的箱线图展示
#Score each item
dat4<-smbinning.scoring.gen(smbscaled=cre_scal, dataset=dat3)
boxplot(Score~loan,data=dat4,horizontal=T, frame=F, col="lightgray",main="Distribution")
生成打分指标
#Standardized scoring table
scaledcard<-cre_scal$logitscaled[[1]][-1,c(1,2,6)]
scaledcard[,1]<-c(rep("年龄",5),rep("收入",3),rep("孩子数量",2),rep("婚否",2),rep("在现工作时间",5),rep("住房类型",3),rep("国籍",8),rep("信用卡类型",7))
scaledcard
7 写入csv文件
ncol(dat4)
dat5=dat4[,10:18]#write the results
write.table(scaledcard,"card.csv",row.names = F)
write.table(dat4,"card.csv",row.names = F,append = T)
?write.csv
输出文件给业务人员
85 R 银行信用卡风控评分数据分析相关推荐
- 不容错过|额度管理与应用-银行信用卡行为评分篇(实操见)
在日常生活消费中,我们大多数童鞋可能会采用信用卡来进行支付,毕竟在很多时候显得非常方便,而且随着信用消费和按时还款的良好表现,我们的信用卡额度从最开始的几千逐渐会变成几万,这种变化自然是我们个人较好资 ...
- python金融风控评分卡模型和数据分析
python金融风控评分卡模型和数据分析微专业课(博主录制):http://dwz.date/b9vv 作者Toby:持牌照消费金融模型专家,和中科院,中科大教授保持长期项目合作:和同盾,聚信立等外部 ...
- python金融风控评分卡模型和数据分析(加强版)-收藏
信用评分卡 信用评分是指根据银行客户的各种历史信用资料,利用一定的信用评分模型,得到不同等级的信用分数,根据客户的信用分数,授信者可以通过分析客户按时还款的可能性,据此决定是否给予授信以及授信的额度和 ...
- 金融风控项目实战-银行信用卡流失预测模型_基于ANN神经网络_金融培训_论文科研_毕业设计
业务背景 根据央行公布的数据显示,全国性银行信用卡和借贷合一卡的发卡量增速从2017年同比增速26.35%的高点逐年下降,截至2020年同比增速降至4.26%.银行信用卡发卡增速明显放缓的背景下,预防 ...
- 银行信用卡评分模型(一)| python
文章目录 背景介绍 题目分析 一.申请者评级模型 二.欺诈评级模型 三.行为评级模型 四.催收评级模型 总结 背景介绍 信用风险和评分卡模型的基本概念:信用风险指的是交易对手未能履行约定合同中的义务造 ...
- python金融风控评分卡模型
python金融风控评分卡模型和数据分析微专业课(博主录制): [ http://dwz.date/b9vv ](https://study.163.com/series/1202875601.htm ...
- 无卡支付时代 银行信用卡联手京东金融欲打翻身仗
互联网金融的兴起带动了消费金融的快速发展,很多平台纷纷利用消费分期来提升交易额,同时,那些具有互联网基因的电商平台也大大增加了用户粘性.伴随着整个消费大潮从线上向线下回归,消费金融也开始重新向线下市场 ...
- 银行信用卡流失预测模型_基于ANN神经网络_金融培训_论文科研_毕业设计
业务背景 根据央行公布的数据显示,全国性银行信用卡和借贷合一卡的发卡量增速从2017年同比增速26.35%的高点逐年下降,截至2020年同比增速降至4.26%.银行信用卡发卡增速明显放缓的背景下,预防 ...
- 风控评分模型全流程的开发及应用
风控评分模型全流程的开发及应用 信用评分卡的应用场景有申请评分卡(A卡).行为评分卡(B卡).催收评分卡(C卡)和反欺诈评分卡(F卡). 用户申请信用贷款的流程依次是基本信息核查.强规则校验. ...
最新文章
- [转]100个常用的linux命令
- 在html设置文字位置,html设置怎么文字的位置
- 安卓布局,GridLayout
- Coding the Matrix作业Python Lab及提交方法
- TP收集一些可以用的资源
- webm是什么格式?
- 怎么在pta上搜题_在电脑上搜题,是不用模拟器的那种
- 路径规划算法学习Day4-Astar算法
- 数据结构——线段树学习笔记
- indexOf 用法总结
- 斯蒂夫·乔布斯《你必须要找到你所爱的东西》
- 百度地图语音导航开发
- python暑假培训班
- 什么是质量功能配置(QFD)(转载)
- 模仿 Github设计一个博客网站的 API
- Springboot 热部署----热部署插件的使用
- SQL中数据类型转换
- 1-1:Huawei路由交换技术简单知识
- win10不识别移动硬盘
- 用Python设计抢红包系统