woe编码报错怎么解决

''' 第四步:WOE编码、计算IV ''' WOE_dict = {} IV_dict = {} # 分箱后的变量进行编码,包括: # 1,初始取值个数小于5,且不需要合并的类别型变量。存放在less_value_features中 # 2,初始取值个数小于5,需要合并的类别型变量。合并后新的变量存放在var_bin_list中 # 3,初始取值个数超过5,需要合并的类别型变量。合并后新的变量存放在var_bin_list中 # 4,连续变量。分箱后新的变量存放在var_bin_list中 all_var = var_bin_list  + less_value_features for var in all_var:     woe_iv = CalcWOE(trainData, var, 'feature56')     WOE_dict[var] = woe_iv['WOE']     IV_dict[var] = woe_iv['IV']  
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-25-119fb2c629b9> in <module>()
     11 all_var = var_bin_list  + less_value_features
     12 for var in all_var:
---> 13     woe_iv = CalcWOE(trainData, var, 'feature56')
     14     WOE_dict[var] = woe_iv['WOE']
     15     IV_dict[var] = woe_iv['IV']

<ipython-input-8-2ad957842800> in CalcWOE(df, col, target)
    298     regroup['bad_pcnt'] = regroup['bad'].map(lambda x: x*1.0/B)
    299     regroup['good_pcnt'] = regroup['good'].map(lambda x: x * 1.0 / G)
--> 300     regroup['WOE'] = regroup.apply(lambda x: np.log(x.good_pcnt*1.0/x.bad_pcnt),axis = 1)
    301     WOE_dict = regroup[[col,'WOE']].set_index(col).to_dict(orient='index')
    302     for k, v in WOE_dict.items():

C:\ProgramData\Anaconda2\lib\site-packages\pandas\core\frame.pyc in apply(self, func, axis, broadcast, raw, reduce, args, **kwds)
   4150                     if reduce is None:
   4151                         reduce = True
-> 4152                     return self._apply_standard(f, axis, reduce=reduce)
   4153             else:
   4154                 return self._apply_broadcast(f, axis)

C:\ProgramData\Anaconda2\lib\site-packages\pandas\core\frame.pyc in _apply_standard(self, func, axis, ignore_failures, reduce)
   4246             try:
   4247                 for i, v in enumerate(series_gen):
-> 4248                     results[i] = func(v)
   4249                     keys.append(v.name)
   4250             except Exception as e:

<ipython-input-8-2ad957842800> in <lambda>(x)
    298     regroup['bad_pcnt'] = regroup['bad'].map(lambda x: x*1.0/B)
    299     regroup['good_pcnt'] = regroup['good'].map(lambda x: x * 1.0 / G)
--> 300     regroup['WOE'] = regroup.apply(lambda x: np.log(x.good_pcnt*1.0/x.bad_pcnt),axis = 1)
    301     WOE_dict = regroup[[col,'WOE']].set_index(col).to_dict(orient='index')
    302     for k, v in WOE_dict.items():

ZeroDivisionError: ('float division by zero', u'occurred at index 3')

田野

赞同来自:

你好,请将数据集传给我,谢谢!

要回复问题请先登录注册