特征选取之IV(信息值)及python实现
IV表征特征的预测能力:小于0.02,几乎没有预测能力;小于0.1,弱;小于0.3,中等;小于0.5,强;大于0.5,难以置信,需进一步确认
WOE describes the relationship between a predictive variable and a binary target variable.
IV measures the strength of that relationship.
计算公式:暂不写……
代码实现如下:
# 定义字典,记录每个特征的信息值iv iv_dict=dict() def cal_iv(df,feature,target='target'): ''' 用于二分类的信息值计算,返回信息值和具体信息 :df pd.DataFrame :feature 选择的特征 :target 目标特征名 ''' ls=[] for val in df[feature].unique(): al=df[df[feature]==val][feature].count() good=df[(df[feature]==val)&(df[target]==1)][feature].count() bad=df[(df[feature]==val)&(df[target]==0)][feature].count() ls.append([val,al,good,bad]) data=pd.DataFrame(ls,columns=[feature,'all','good','bad']) good_rate=data['good']/data['good'].sum()# good边际概率 bad_rate=data['bad']/data['bad'].sum()# bad边际概率 data['woe']=np.log(good_rate/bad_rate)# woe为证据权重 data = data.replace({'woe': {np.inf: 0, -np.inf: 0}}) data['iv']=data['woe']*(good_rate-bad_rate) iv=data.iv.sum() # 添加到字典 if feature not in iv_dict.keys(): iv_dict[feature]=iv print('iv for %s is %f: '%(feature,iv)) return iv,data