首页 > 其他分享 >特征筛选-WOE和IV

特征筛选-WOE和IV

时间:2023-10-07 23:56:37浏览次数:26  
标签:df label iv woe WOE 筛选 IV

背景

在评分卡建模流程中,WOE(Weight of Evidence)常用于特征变换,IV(Information Value)则用来衡量特征的预测能力。
文章取自:风控模型—WOE与IV指标的深入理解应用
代码取自:特征值筛选依据:IV值和WOE的python计算

WOE和IV的应用价值

WOE(Weight of Evidence)叫做证据权重,大家可以思考下为什么会取这个名字?

那么WOE在业务中常有哪些应用呢?

处理缺失值:当数据源没有100%覆盖时,那就会存在缺失值,此时可以把null单独作为一个分箱。这点在分数据源建模时非常有用,可以有效将覆盖率哪怕只有20%的数据源利用起来。
处理异常值:当数据中存在离群点时,可以把其通过分箱离散化处理,从而提高变量的鲁棒性(抗干扰能力)。例如,age若出现200这种异常值,可分入“age > 60”这个分箱里,排除影响。
业务解释性:我们习惯于线性判断变量的作用,当x越来越大,y就越来越大。但实际x与y之间经常存在着非线性关系,此时可经过WOE变换。
IV(Information Value)是与WOE密切相关的一个指标,常用来评估变量的预测能力。因而可用来快速筛选变量。在应用实践中,其评价标准如下:

在此引用一段话来说明两者的区别和联系:

  1. WOE describes the relationship between a predictive variable and a binary target variable.
  2. IV measures the strength of that relationship.

WOE和IV的计算步骤

单个特征的IV值计算

import pandas as pd
import numpy as np

# 生成数据集
data = {'age':[6,7,8,11,12,13,23,24,25,37,38,39,44,45,46,51,61,71],
        'label':[0,1,0,0,0,1,0,1,1,0,1,1,0,0,1,0,0,1]}
df = pd.DataFrame(data)

# 拆分好坏样本标签
df['good'] = df['label'].map(lambda x: 1 if x == 0 else 0)  # 好样本
df['bad'] = df['label'].map(lambda x: 1 if x == 1 else 0)  # 坏样本



# 年龄分箱
bins = [0, 10, 20, 30, 40, 50, 100]
df['age_cate'] = pd.cut(df.age, bins, labels=["婴儿","少年","青年","中年","壮年","老年"])

# 统计各箱中好坏比率
woe_iv_df_good = df.groupby('age_cate').agg({'good':'sum'}).reset_index() # 好样本个数
woe_iv_df_bad = df.groupby('age_cate').agg({'bad':'sum'}).reset_index() # 坏样本个数
woe_iv_df = pd.merge(woe_iv_df_good, woe_iv_df_bad, how = 'left', on = 'age_cate') 
woe_iv_df['cnt'] = woe_iv_df['good'] + woe_iv_df['bad']

# 计算各分箱的woe值
woe_iv_df['good'] = woe_iv_df['good']/woe_iv_df['good'].sum()
woe_iv_df['bad'] = woe_iv_df['bad']/woe_iv_df['bad'].sum()
woe_iv_df['woe'] = np.log(woe_iv_df['good']/woe_iv_df['bad'])
woe_iv_df['iv'] = (woe_iv_df['good'] - woe_iv_df['bad']) * woe_iv_df['woe']
woe_iv_df


# 计算iv值
iv = woe_iv_df['iv'].sum()
iv

多个特征的IV值计算

data = {'age':[6,7,8,11,12,13,23,24,25,37,38,39,44,45,46,51,61,71],
        'gender':['男','女','男','女','男','女','女','女','男','男','女','男','女','男','女','女','女','男'],
        'label':[0,1,0,0,0,1,0,1,1,0,1,1,0,0,1,0,0,1]}
df = pd.DataFrame(data)

# 分箱
bins = [0, 10, 20, 30, 40, 50, 100]
df['age_cate'] = pd.cut(df.age, bins, labels=["婴儿","少年","青年","中年","壮年","老年"])

# 正负标签处理
df['label_0'] = df['label'].map(lambda x: 1 if x == 0 else 0)
df['label_1'] = df['label'].map(lambda x: 1 if x == 1 else 0)



# 选择需要计算的指标
dim = ['age_cate','gender']

# 创建空的表格
iv_list = pd.DataFrame([],columns = ['指标','IV值'])

# 写循环计算IV值
for index in dim:
    woe_iv_df = df.groupby(index).agg({'label_0':'sum'
                                     ,'label_1':'sum'
                                     },).reset_index()
    woe_iv_df['ratio_0'] = woe_iv_df['label_0']/woe_iv_df['label_0'].sum()
    woe_iv_df['ratio_1'] = woe_iv_df['label_1']/woe_iv_df['label_1'].sum()
    woe_iv_df['woe'] = np.log(woe_iv_df['ratio_0']/woe_iv_df['ratio_1'])
    woe_iv_df['iv'] = (woe_iv_df['ratio_0'] - woe_iv_df['ratio_1']) * woe_iv_df['woe']
    
    iv = round(woe_iv_df['iv'].sum(),4)
    a = pd.DataFrame({'指标':[index],'IV值':[iv]})
    iv_list = iv_list.append(a)
# 输出结果
iv_list

标签:df,label,iv,woe,WOE,筛选,IV
From: https://www.cnblogs.com/tian1022/p/17747797.html

相关文章

  • Effective C++——Item33: 避免隐藏继承的名字
    EffectiveC++——Item33:避免隐藏继承的名字一、从原理理解隐藏从变量作用域看隐藏全局变量x和局部变量x的类型是不同的,但C++的隐藏规则:只隐藏名字(hidingnames)。intx;//globalvariablevoidsomeFunc(){doublex;/......
  • WebKit Insie: Active 样式表
    WebKitInside:CSS样式表的匹配时机介绍了当HTML页面有不同CSS样式表引入时,CSS样式表开始匹配的时机。后续文章继续介绍CSS样式表的匹配过程,但是在匹配之前,首先需要收集页面里面的Active样式表。1Active样式表在一个HTML文件里面,可能会使用<style>标签与<link>标......
  • Serverless平台knative第十章如何应用pod频繁抖动
    负载变动频繁时,Knative可能会因为响应负载变动而导致频繁创建或销毁Pod实例为避免服务规模“抖动”,AutoScaler支持两种扩缩容模式Stable稳定模式在稳定模式中,KPA会在默认的稳定窗口期(默认为60秒)内计算Pod的平均并发数。根据这个平均并发数,KPA会调整Pod的数量,以保持稳定的负载水......
  • Codeforces Round 900 (Div. 3) E. Iva & Pav (位运算)
    CodeforcesRound900(Div.3)E.Iva&Pav//思路:10^9转换为2^32上的位,进行位运算,a[x][i]为到x为止第i位的1个数前缀和//对于与运算,如果当前i的前缀和不为r-l+1,则这一位的与运算结果为0//当找到从左往右第一个位置i为1使得k在这位为0,则与运算前缀大于k//二分查找最后一......
  • Codeforces Round 901 (Div. 2) C. Jellyfish and Green Apple (位运算)
    CodeforcesRound901(Div.2)C.JellyfishandGreenApple//思路:浮点数转二进制,a/b的结果为gcd(a,b)*最简分式(n/m)的结果//苹果能分的前提是人数得是一个2的次幂数,通过切割只能分为形同0.001的二进制小数//a/b的二进制如果在从左到右的sp位为1,则需要切割到这个情况//一个......
  • Serverless平台knative第九章配置文件介绍
    knative配置文件[root@ip-172-17-11-227~]#kubectlgetcm-nknative-servingNAMEDATAAGEconfig-autoscaler12d2hconfig-defaults12d2hconfig-deployment22d2hconfig-domain22d2h......
  • Selenium - 自动下载 webdriver
    背景很多浏览器会自动更新,但是driver不会自动更新。为了确保driver版本和浏览器匹配,可以使用第三方库webdriver_manager代码这个文件里封装了几个函数driver_seek:根据给定的目录,和文件名称,查找该目录下是否有这个文件driver_download:下载webdriver到指定目录,如果path......
  • difference between a Client-Server and Sender-Receiver interface in Autosar
    thedifferencebetweenaClient-ServerandSender-ReceiverinterfaceinAutosarInaClient-Serverinterface,theclientrequestsaservicefromtheserverandtheserverrespondswitharesult.InaSender-Receiverinterface,thesendersendsdatatoone......
  • knative serving 域名映射
    创建应用hello-world.yamlapiVersion:serving.knative.dev/v1kind:Servicemetadata:name:helloworld-gonamespace:knative-demospec:template:spec:containers:-image:ghcr.dockerproxy.com/knative/helloworld-go:latestenv......
  • 创建vue3项目、setup函数、ref函数、reactive函数、计算监听属性、生命周期、torefs、
    创建vue3项目#两种方式-vue-cli:vue脚手架---》创建vue项目---》构建vue项目--》工具链跟之前一样-vite:https://cn.vitejs.dev/-npmcreatevue@latest一路选择即可#运行vue3项目-vue-cli跟之前一样-vi......