WOE和IV理论
1.WOE
WOE的全称是“Weight of Evidence”,即证据权重。WOE是对原始自变量的一种编码形式,计算公式如下:
其中,pyi是这个组中响应客户(风险模型中,对应的是违约客户,总之,指的是模型中预测变量取值为“是”或者说1的个体)占所有样本中所有响应客户的比例,pni是这个组中未响应客户占样本中所有未响应客户的比例,#yi是这个组中响应客户的数量,#ni是这个组中未响应客户的数量,#yT是样本中所有响应客户的数量,#nT是样本中所有未响应客户的数量
WOE实际代表的含义:“当前分组中响应客户占所有响应客户的比例”和“当前分组中没有响应的客户占所有没有响应的客户的比例”的差异
对公式做一个简单的变换:
WOE也可以这么理解,他表示的是当前这个组中响应的客户和未响应客户的比值,和所有样本中这个比值的差异。这个差异是用这两个比值的比值,再取对数来表示的。WOE越大,这种差异越大,这个分组里的样本响应的可能性就越大,WOE越小,差异越小,这个分组里的样本响应的可能性就越小。
2.WOE转化的优势:
提升模型的预测效果,提高模型的可理解性
1)WOE与违约概率具有某种线性关系
从而通过这种WOE编码可以发现自变量与目标变量之间的非线性关系(例如U型或者倒U型关系)。提升预测效果
2)WOE变量出现负值情况。
在此基础上,我们可以预料到模型拟合出来的自变量系数应该都是正数,如果结果中出现了负数,应当考虑是否是来自自变量多重共线性的影响。
3)标准化的功能。
WOE编码之后,自变量其实具备了某种标准化的性质,也就是说,自变量内部的各个取值之间都可以直接进行比较(WOE之间的比较),而不同自变量之间的各种取值也可以通过WOE进行直接的比较。
4)WOE能反映自变量的贡献情况。
自变量内部WOE值的变异(波动)情况,结合模型拟合出的系数,构造出各个自变量的贡献率及相对重要性。一般地,系数越大,woe的方差越大,则自变量的贡献率越大(类似于某种方差贡献率),这也能够很直观地理解。
5)异常值处理。
很多极值变量通过WOE可以变为非异常值.
3.IV值
IV的全称是Information Value,中文意思是信息价值,或者信息量。
IV用来衡量自变量的预测能力,类似的指标还有信息增益、基尼系数等等。
计算公式如下:
有了一个变量各分组的IV值,我们就可以计算整个变量的IV值,方法很简单,就是把各分组的IV相加:
4.代码
之前在网上找到一位大牛写的包,亲测可用,代码如下:
import numpy as np
import math
from scipy import stats
from sklearn.utils.multiclass import type_of_target
class WOE:
def __init__(self):
self._WOE_MIN = -20
self._WOE_MAX = 20
def woe(self, X, y, event=1):
'''
Calculate woe of each feature category and information value
:param X: 2-D numpy array explanatory features which should be discreted already
:param y: 1-D numpy array target variable which should be binary
:param event: value of binary stands for the event to predict
:return: numpy array of woe dictionaries, each dictionary contains woe values for categories of each feature
numpy array of information value of each feature
'''
self.check_target_binary(y)
X1 = self.feature_discretion(X)
res_woe = []
res_iv = []
for i in range(0, X1.shape[-1]):
x = X1[:, i]
woe_dict, iv1 = self.woe_single_x(x, y, event)
res_woe.append(woe_dict)
res_iv.append(iv1)
return np.array(res_woe), np.array(res_iv)
def woe_single_x(self, x, y, event=1):
'''
calculate woe and information for a single feature计算单个变量的woe以及IV值
:param x: 1-D numpy starnds for single feature
:param y: 1-D numpy array target variable
:param event: value of binary stands for the event to predict
:return: dictionary contains woe values for categories of this feature
information value of this feature
'''
self.check_target_binary(y)
event_total, non_event_total = self.count_binary(y, event=event)
x_labels = np.unique(x)
woe_dict = {}
iv = 0
for x1 in x_labels:
y1 = y[np.where(x == x1)[0]]
event_count, non_event_count = self.count_binary(y1, event=event)
rate_event = 1.0 * event_count / event_total
rate_non_event = 1.0 * non_event_count / non_event_total
if rate_event == 0:
woe1 = self._WOE_MIN
elif rate_non_event == 0:
woe1 = self._WOE_MAX
else:
woe1 = math.log(rate_event / rate_non_event)
woe_dict[x1] = woe1
iv += (rate_event - rate_non_event) * woe1
return woe_dict, iv
def woe_replace(self, X, woe_arr):
'''
replace the explanatory feature categories with its woe value
:param X: 2-D numpy array explanatory features which should be discreted already
:param woe_arr: numpy array of woe dictionaries, each dictionary contains woe values for categories of each feature
:return: the new numpy array in which woe values filled
'''
if X.shape[-1] != woe_arr.shape[-1]:
raise ValueError('WOE dict array length must be equal with features length')
res = np.copy(X).astype(float)
idx = 0
for woe_dict in woe_arr:
for k in woe_dict.keys():
woe = woe_dict[k]
res[:, idx][np.where(res[:, idx] == k)[0]] = woe * 1.0
idx += 1
return res
def combined_iv(self, X, y, masks, event=1):
'''
calcute the information vlaue of combination features
:param X: 2-D numpy array explanatory features which should be discreted already
:param y: 1-D numpy array target variable
:param masks: 1-D numpy array of masks stands for which features are included in combination,
e.g. np.array([0,0,1,1,1,0,0,0,0,0,1]), the length should be same as features length
:param event: value of binary stands for the event to predict
:return: woe dictionary and information value of combined features
'''
if masks.shape[-1] != X.shape[-1]:
raise ValueError('Masks array length must be equal with features length')
x = X[:, np.where(masks == 1)[0]]
tmp = []
for i in range(x.shape[0]):
tmp.append(self.combine(x[i, :]))
dumy = np.array(tmp)
# dumy_labels = np.unique(dumy)
woe, iv = self.woe_single_x(dumy, y, event)
return woe, iv
def combine(self, list):
res = ''
for item in list:
res += str(item)
return res
def count_binary(self, a, event=1):
event_count = (a == event).sum()
non_event_count = a.shape[-1] - event_count
return event_count, non_event_count
def check_target_binary(self, y):
'''
check if the target variable is binary, raise error if not.
:param y:
:return:
'''
y_type = type_of_target(y)
if y_type not in ['binary']:
raise ValueError('Label type must be binary')
def feature_discretion(self, X):
'''
Discrete the continuous features of input data X, and keep other features unchanged.
:param X : numpy array
:return: the numpy array in which all continuous features are discreted
'''
temp = []
for i in range(0, X.shape[-1]):
x = X[:, i]
x_type = type_of_target(x)
if x_type == 'continuous':
x1 = self.discrete(x)
temp.append(x1)
else:
temp.append(x)
return np.array(temp).T
def discrete(self, x):
'''
Discrete the input 1-D numpy array using 5 equal percentiles
:param x: 1-D numpy array
:return: discreted 1-D numpy array
'''
res = np.array([0] * x.shape[-1], dtype=int)
for i in range(5):
point1 = stats.scoreatpercentile(x, i * 20)
point2 = stats.scoreatpercentile(x, (i + 1) * 20)
x1 = x[np.where((x >= point1) & (x <= point2))]
mask = np.in1d(x, x1)
res[mask] = (i + 1)
return res
@property
def WOE_MIN(self):
return self._WOE_MIN
@WOE_MIN.setter
def WOE_MIN(self, woe_min):
self._WOE_MIN = woe_min
@property
def WOE_MAX(self):
return self._WOE_MAX
@WOE_MAX.setter
def WOE_MAX(self, woe_max):
self._WOE_MAX = woe_max