写在前面
1、目的
建模经常要做单变量分析,就把单变量分析要用到的函数全部封装为一个包fctn,其中的函数包含:
1)count_iv(df, refuse, x):每个值为一组
2)qcut_iv(df, refuse,x, n)::n等分进行分组
3)customcut_iv(df,refuse,x,group):自定义区间进行分组
4)autocut_iv(df,refuse, x, percent = 1):自动合并占比x以下的组进行分组
2、用法
将Ipynb_importer.py与fctn.ipynb放入统一文件夹内
在建模文件中:
import Ipynb_importer
import fctn
可以直接直接从fctn中调用函数,例如:
fctn.count_iv(alldata, 'respond', 'ini_lvl')
3、输出结果
fctn.qcut_iv(result, 'target', 'province_br',5)
代码
fctn.ipynb
import pandas as pd
import numpy as np
import seaborn as sns
import math
def count_iv(df, refuse, x):
meany = df[refuse].mean()
df[x] = df[x].fillna('Nan')
t = df.groupby([x])[refuse].agg(['count','mean']).reset_index()
t['percent'] = t['count']/df[x].count()*100
t['是否初步显著'] = t.apply(lambda row: 1 if abs(row['mean']-meany)/meany>=0.2 and row['percent']>=2 else 0, axis=1)
t['是否初步拒绝'] = t.apply(lambda row: 1 if row['mean']>(2*meany) else 0, axis=1)
gc = df[refuse].value_counts()[0]
bc = df[refuse].value_counts()[1]
t['gr'] = t.apply(lambda x: (1-x['mean'])*x['count']/gc, axis=1)
t['br'] = t.apply(lambda x: x['mean']*x['count']/bc, axis=1)
t['WoE'] = t.apply(lambda x: math.log(x['gr']/x['br'])*100 if ((x['br'] > 0) and (x['gr'] > 0)) else 0,axis=1)
t['cIV'] = t.apply(lambda x: (x['gr']-x['br'])*math.log(x['gr']/x['br']) if ((x['br'] > 0) and (x['gr'] > 0)) else 0, axis=1)
# t['WoE'] = t.apply(lambda x: math.log(x['gr']/x['br'])*100, axis=1)
# t['cIV'] = t.apply(lambda x: (x['gr']-x['br'])*math.log(x['gr']/x['br']), axis=1)
print('meany:',meany , 'IV值:',t['cIV'].sum() )
f=sns.barplot(x=x,y='mean',data=t)
return t
def qcut_iv(df, refuse,x, n):
meany = df[refuse].mean()
#df[x] = df[x].fillna('Nan')
x1