I was wondering how to calculate the WOE and IV in python. Are there any dedication function in numpy/scipy/pandas/sklearn?
Here is my example dataframe:
import numpy as np
import pandas as pd
np.random.seed(100)
df = pd.DataFrame({'grade': np.random.choice(list('ABCD'),size=(20)),
'pass': np.random.choice([0,1],size=(20))
})
df
Formulas for woe and iv:
Code to achieve this:
import numpy as np
import pandas as pd
np.random.seed(100)
df = pd.DataFrame({'grade': np.random.choice(list('ABCD'),size=(20)),
'pass': np.random.choice([0,1],size=(20))
})
feature,target = 'grade','pass'
df_woe_iv = (pd.crosstab(df[feature],df[target],
normalize='columns')
.assign(woe=lambda dfx: np.log(dfx[1] / dfx[0]))
.assign(iv=lambda dfx: np.sum(dfx['woe']*
(dfx[1]-dfx[0]))))
df_woe_iv
pass 0 1 woe iv
grade
A 0.3 0.3 0.000000 0.690776
B 0.1 0.1 0.000000 0.690776
C 0.2 0.5 0.916291 0.690776
D 0.4 0.1 -1.386294 0.690776
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With