我有一个计算WOE和IV的函数,如下所示:def calc_iv(df, feature, target, pr=0):
lst = []
for i in range(df[feature].nunique()):
val = list(df[feature].unique())[i]
lst.append([feature, val, df[df[feature] == val].count()[feature], df[(df[feature] == val) & (df[target] == 1)].count()[feature]])
data = pd.DataFrame(lst, columns=['Variable', 'Value', 'All', 'Bad'])
data = data[data['Bad'] > 0]
data['Share'] = data['All'] / data['All'].sum()
data['Bad Rate'] = data['Bad'] / data['All']
data['Distribution Good'] = (data['All'] - data['Bad']) / (data['All'].sum() - data['Bad'].sum())
data['Distribution Bad'] = data['Bad'] / data['Bad'].sum()
data['grp_score'] = round((data['Distribution Good']/(data['Distribution Good'] + data['Distribution Bad']))*10, 2)
data['WoE'] = np.log(data['Distribution Good'] / data['Distribution Bad'])
data['IV'] = (data['WoE'] * (data['Distribution Good'] - data['Distribution Bad'])).sum()
data['Efficiency'] = abs(data['Distribution Good'] - data['Distribution Bad'])/2
data = data.sort_values(by=['Variable', 'Value'], ascending=True)
d = {data['Distribution Good'],data['Distribution Bad'],data['Share'],
data['Bad Rate'],data['grp_score'],data['WoE'],data['IV'],data['Efficiency']}
mydf=pd.DataFrame(data=d)
if pr == 1:
print(data)
#return data['IV'].values[0]
return mydf.values
函数检查数据帧(dat),如下所示
^{pr2}$
然后我调用函数如下calc_iv(dat, 'myvar1', 'target', pr=0)
我希望函数返回myvar1Distribution Good Distribution Bad Share Bad Rate grp_score WoE IV Efficiency
0.1 0.9 1 0.9 20 0.2 0.6 0.8
0.8 0.2 2 0.2 10 0.1 0.2 0.1
0.7 0.3 3 0.3 70 0.7 0.8 0.5
但是我得到了下面的错误TypeError: 'Series' objects are mutable, thus they cannot be hashed