美文网首页机器学习与数据挖掘白话评分卡
免费高效良心包:R开发传统评分卡步骤

免费高效良心包:R开发传统评分卡步骤

作者: _KevinZhang_ | 来源:发表于2019-08-19 14:42 被阅读8次

    口号:深扒术语不装逼,实践真知变牛逼。
    为什么有这个口号?文末有对口号的解释。

    好了,直接上主角,在Gihub上有这么一个项目:

    ShichenXie/scorecard

    * 安装部分请参考项目描述,比较简单,这里就不赘述了。
    * 在进行以下步骤前,强烈建议使用 RStudio (放心,这也是免费的)。

    看这个包的名字就知道,是专门做评分卡的。

    The goal of scorecard package is to make the development of the traditional credit risk scorecard model easier and efficient by providing functions for some common tasks that summarized in below. This package can also used in the development of machine learning models on binomial classification.

    一句话就是:本包可以简单快速的实现传统评分卡的步骤。

    经过测试,这个包涵盖了完整开发传统评分卡所需要的全部功能:

    • 数据准备 - data preparation (split_df, one_hot)
    • 变量筛选 - variable selection (var_filter, iv, vif)
    • 分箱以及WOE转换 - weight of evidence (woe) binning (woebin, woebin_plot, woebin_adj, woebin_ply)
    • 表现评估 - performance evaluation (perf_eva, perf_psi)
    • 评分转换 - scorecard scaling (scorecard, scorecard_ply)
    • 评分卡报告 - scorecard report (gains_table, report)

    下面就按照示例看看这个包好用的地方吧。

    • 引入scorecard程序包
    # Traditional Credit Scoring Using Logistic Regression
    library(scorecard)
    
    • 数据准备
      注意目标变量(Y)要设置成0|1,1表示坏样本。
      以下例子的程序包中有兼容,即'bad'为坏样本
    # data preparing ------
    # 加载German Credit数据
    data("germancredit")
    # filter variable via missing rate, iv, identical value rate
    # 默认的是筛选IV值大于0.02,缺失率小于95%,identical value rate 小于0.95
    dt_f = var_filter(germancredit, y="creditability")
    # breaking dt into train and test
    # 把数据分成训练(60%)、测试(40%),随机数种子随便设,这里是30
    dt_list = split_df(dt_f, y="creditability", ratio = 0.6, seed = 30)
    label_list = lapply(dt_list, function(x) x$creditability)
    
    # woe binning ------
    # 分箱
    bins = woebin(dt_f, y="creditability")
    # 分箱情况图表
    woebin_plot(bins)
    
    举例
    # binning adjustment
    ## adjust breaks interactively
    ## 手动调整分箱
    # breaks_adj = woebin_adj(dt_f, "creditability", bins) 
    ## or specify breaks manually
    breaks_adj = list(
      age.in.years=c(26, 35, 40),
      other.debtors.or.guarantors=c("none", "co-applicant%,%guarantor"))
    bins_adj = woebin(dt_f, y="creditability", breaks_list=breaks_adj)
    

    调整前:



    调整后:


    这只是示范哈,实战中如何调整在另外的篇幅里说。

    • 把原始变量转换成woe值
    # converting train and test into woe values
    dt_woe_list = lapply(dt_list, function(x) woebin_ply(x, bins_adj))
    
    • 用训练数据开始建模
    # glm ------
    m1 = glm( creditability ~ ., family = binomial(), data = dt_woe_list$train)
    # vif(m1, merge_coef = TRUE) # summary(m1)
    # Select a formula-based model by AIC (or by LASSO for large dataset)
    m_step = step(m1, direction="both", trace = FALSE)
    m2 = eval(m_step$call)
    # 以下语句是检验膨胀系数
    vif(m2, merge_coef = TRUE) # summary(m2)
    
    • 检查模型效果
    # performance ks & roc ------
    ## predicted proability
    pred_list = lapply(dt_woe_list, function(x) predict(m2, x, type='response'))
    ## performance 以下语句把模型所有都效果曲线都显示出来
    perf = perf_eva(pred = pred_list, label = label_list, show_plot = c('ks', 'lift', 'gain', 'roc', 'lz', 'pr', 'f1', 'density'))
    

    以上就是这个程序包的主要功能步骤,是不是很高效?

    但是肯定有同学会疑惑,大概看明白了,但是自己实际做是另外一回事。

    对滴,因为上面步骤里包含了很多知识,这些知识的表象体现就是很多术语...

    • 什么是术语?

    术语就是每个字应该都懂,但是合起来完全不懂的词语...

    后续(如果有)会用以上的风格对相关术语、知识进行大白话讲述。

    如果你对统计模型、评分卡、机器学习有兴趣。那么请点赞吧。

    如果点赞超过10个,笔者有动力继续写下去,完成本文第一句口号说的事情。

    没有10个?那就再说吧...

    谢谢啦。

    相关文章

      网友评论

        本文标题:免费高效良心包:R开发传统评分卡步骤

        本文链接:https://www.haomeiwen.com/subject/vdlssctx.html