• OptBinning 特征分箱包使用介绍


    OptBinning 特征分箱包使用介绍

    OptBinning:支持数值型和分类型的最大IV分箱,并可保证分箱单调性,同事方便处理缺失值。作为一个分箱包还是挺好用,这里简答介绍了这个包中OptimalBinning和fit_transform的参数。

    1、使用案例

    import pandas as pd 
    from sklearn.datasets import load_breast_cancer
    # 数据准备
    data = load_breast_cancer()
    df = pd.DataFrame(data.data, columns=data.feature_names)
    variable = "mean radius"
    x = df[variable].values
    y = data.target
    from optbinning import OptimalBinning
    optb = OptimalBinning(name=variable, dtype="numerical", solver="cp",max_n_bins=5) # 通过设置参数建立分箱器
    w =  optb.fit_transform(x,y,metric='indices')  # 通过设置参数进行分箱
    optb.binning_table.build()  # 展示分箱结果 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    BinCountCount (%)Non-eventEventEvent rateWoEIVJS
    0(-inf, 12.33)1970.34622161910.969543-2.9393641.4894120.139117
    1[12.33, 13.09)680.1195087610.897059-1.6438140.2265990.025513
    2[13.09, 15.05)1320.23198638940.712121-0.3845590.0323260.004016
    3[15.05, 16.93)540.09490344100.1851852.0027540.3595660.038678
    4[16.93, inf)1180.20738111710.0084755.2833232.9009970.183436
    5Special00.000000000.0000000.00.0000000.000000
    6Missing00.000000000.0000000.00.0000000.000000
    Totals5691.0000002123570.6274175.0089000.390760
    optb.splits # 查看分箱切割点 
    
    • 1
    array([12.32999992, 13.09499979, 15.04500008, 16.92500019])
    
    • 1
    optb.binning_table.plot()  # 绘制分箱图 
    
    • 1

    在这里插入图片描述

    optb.binning_table.gini,optb.binning_table.iv,optb.binning_table.ks  # 查看分箱gini、iv和ks计算结果
    
    • 1
    (0.8667089477300354, 5.008900160240071, 0.7286216373341788)
    
    • 1

    2、OptimalBinning 构建分箱器参数介绍

    变量名称、类型和默认值解释可选参数说明
    name : str, optional (default=“”)变量名称不重要
    dtype : str, optional (default=“numerical”)分箱类型numerical;categoricalnumerical:数值型;categorical:分类型;
    prebinning_method : str, optional (default="cart分箱方法cart;mdlp;quantile;uniformcart:决策树;
    mdlp:Minimum Description Length Principle ;
    quantile:分位数,等频率uniform:等宽分箱
    solver : str, optional (default=“cp”)算子cp;mip不重要,一般按默认算子
    divergence : str, optional (default=“iv”)划分依据iv;js;hellinger;triangular求解最大化的指标,其他不了解,一般使用iv
    min_n_bins 、max_n_bins: int or None, optional (default=None)最小、最大分箱数量
    min_bin_size、max_bin_size : float or None, optional (default=None)最小、最大箱数据占比
    min_bin_n_nonevent、max_bin_n_nonevent 、min_bin_n_event 、max_bin_n_event :nt or None, optional (default=None)箱中响应和不响应最小(最大)数量
    monotonic_trend: : str or None, optional (default=“auto”)单调趋势一般使用的有:ascending,descending,auto_asc_desc,peak,valleyascending:单调递增;
    descending:单调递减
    auto_asc_desc:自动增减:
    peak:先增后减
    valley:先减后增
    min_event_rate_diff : float, optional (default=0)相隔分箱之间最小差距only applies when monotonic_trend is “ascending”, “descending”, “peak_heuristic” or “valley_heuristic”.
    user_splits_fixed提前设定的分箱

    3、optb.fit_transform 数据分箱参数介绍

    变量名称、类型和默认值解释可选参数说明
    x : array-like, shape = (n_samples,)x
    y : array-like, shape = (n_samples,)y
    sample_weight : array-like of shape (n_samples,) (default=None)样本权重
    metric : str (default=“woe”)编码方式woe;event_rate;indices;binswoe、event_rate:返回区间woe和rate;
    indices:返回0,1,2,3;
    bins:返回分箱区间;
    metric_missing :loat or str (default=0)缺失值编码缺失值编码的值
  • 相关阅读:
    国外5G行业应用产业政策分析及对我国的启示
    翻译英语的软件-跨境必备翻译软件免费
    生产者消费者模式
    Linux下按键驱动实验
    产业互联网的商业模式并非平台模式这么简单
    js实现短信验证码一键登录
    ARP 地址解析协议——IP地址到MAC地址
    百度Apollo9.0安装中,bash docker-install.sh操作后,一直出现“有10个软件包未被升级 DEPRECATION WARNING 无法定位软件包 ”的情况
    Vue2 Element description组件 列合并
    (五)vuex
  • 原文地址:https://blog.csdn.net/wwqnmdhmp/article/details/127822801