-
Notifications
You must be signed in to change notification settings - Fork 30
/
AttributeRelevance.py
176 lines (150 loc) · 6.15 KB
/
AttributeRelevance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import pandas as pd
import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
import seaborn as sns
pd.set_option('mode.chained_assignment', None)
class AttributeRelevance():
def seq_palette(self, n_colors):
return sns.cubehelix_palette(n_colors, start=.5, rot=-.75, reverse=True)
def bulk_iv(self, feats, iv, woe_extremes=False):
iv_dict = {}
for f in feats:
iv_df, iv_value = iv.calculate_iv(f)
if woe_extremes:
iv_dict[f.feature] = [iv_value, iv_df['woe'].min(), iv_df['woe'].max()]
cols = ['iv', 'woe_min', 'woe_max']
else:
iv_dict[f.feature] = iv_value
cols = ['iv']
df = pd.DataFrame.from_dict(iv_dict, orient='index', columns=cols)
return df
def bulk_stats(self, feats, s):
stats_dict = {}
for f in feats:
p_value, effect_size = s.calculate_chi(f)
stats_dict[f.feature] = [p_value, effect_size]
df = pd.DataFrame.from_dict(stats_dict, orient='index', columns=['p-value', 'effect_size'])
return df
def analyze(self, feats, iv, s=None, interpretation=False):
df_iv = self.bulk_iv(feats, iv).sort_values(by='iv', ascending=False)
if s is not None:
df_stats = self.bulk_stats(feats, s)
df_iv = df_iv.merge(df_stats, left_index=True, right_index=True)
if interpretation:
df_iv['iv_interpretation'] = df_iv['iv'].apply(iv.interpretation)
if s is not None:
df_iv['es_interpretation'] = df_iv['effect_size'].apply(s.interpretation)
return df_iv
def draw_iv(self, feats, iv):
df = self.analyze(feats, iv)
fig, ax = plt.subplots(figsize=(10, 6))
sns.barplot(x=df.index, y='iv', data=df, palette=self.seq_palette(len(feats)))
ax.set_title('IV values')
plt.xticks(rotation=90)
plt.show()
def draw_woe_extremes(self, feats, iv):
df = self.bulk_iv(feats, iv, woe_extremes=True).sort_values(by='iv', ascending=False)
fig, ax = plt.subplots(figsize=(10, 6))
sns.barplot(x=df.index, y='woe_min', data=df, palette=self.seq_palette(len(feats)))
sns.barplot(x=df.index, y='woe_max', data=df, palette=self.seq_palette(len(feats)))
ax.axhline(y=0, color='black', linewidth=1)
ax.set_title('Range of WOE values')
ax.set_ylabel('WOE')
plt.xticks(rotation=90)
plt.show()
def draw_woe_multiplot(self, feats, iv):
n = len(feats)
nrows = int(np.ceil(n/3))
fig, ax = plt.subplots(nrows=nrows, ncols=3, figsize=(15, nrows*4))
for i in range(n):
iv_df, iv_value = iv.calculate_iv(feats[i])
sns.barplot(x=feats[i].feature, y='woe', data=iv_df, color='#455872', ax=fig.axes[i])
for ax in fig.axes:
plt.sca(ax)
plt.xticks(rotation=50)
plt.tight_layout()
plt.show()
class Analysis():
def seq_palette(self, n_colors):
return sns.cubehelix_palette(n_colors, start=.5, rot=-.75, reverse=True)
def group_by_feature(self, feat):
df = feat.df_lite \
.groupby('bin') \
.agg({'label': ['count', 'sum']}) \
.reset_index()
df.columns = [feat.feature, 'count', 'good']
df['bad'] = df['count'] - df['good']
return df
class StatsSignificance(Analysis):
def calculate_chi(self, feat):
df = self.group_by_feature(feat)
df_chi = np.array(df[['good', 'bad']])
n = df['count'].sum()
chi = stats.chi2_contingency(df_chi)
cramers_v = np.sqrt(chi[0] / n) # assume that k=2 (good, bad)
return chi[1], cramers_v
@staticmethod
def interpretation(cramers_v):
if cramers_v < 0.1:
return 'useless'
elif cramers_v < 0.2:
return 'weak'
elif cramers_v < 0.4:
return 'medium'
elif cramers_v < 0.6:
return 'strong'
else:
return 'very strong'
def interpret_chi(self, feat):
_, cramers_v = self.calculate_chi(feat)
return self.interpretation(cramers_v)
def print_chi(self, feat):
p_value, cramers_v = self.calculate_chi(feat)
print('P-value: %0.2f\nEffect size: %0.2f' % (p_value, cramers_v))
print('%s is a %s predictor' % (feat.feature.capitalize(), self.interpretation(cramers_v)))
class IV(Analysis):
@staticmethod
def __perc_share(df, group_name):
return df[group_name] / df[group_name].sum()
def __calculate_perc_share(self, feat):
df = self.group_by_feature(feat)
df['perc_good'] = self.__perc_share(df, 'good')
df['perc_bad'] = self.__perc_share(df, 'bad')
df['perc_diff'] = df['perc_good'] - df['perc_bad']
return df
def __calculate_woe(self, feat):
df = self.__calculate_perc_share(feat)
df['woe'] = np.log(df['perc_good']/df['perc_bad'])
df['woe'] = df['woe'].replace([np.inf, -np.inf], np.nan).fillna(0)
return df
def calculate_iv(self, feat):
df = self.__calculate_woe(feat)
df['iv'] = df['perc_diff'] * df['woe']
return df, df['iv'].sum()
def draw_woe(self, feat):
iv_df, iv_value = self.calculate_iv(feat)
fig, ax = plt.subplots(figsize=(10,6))
sns.barplot(x=feat.feature, y='woe', data=iv_df, palette=self.seq_palette(len(iv_df.index)))
ax.set_title('WOE visualization for: ' + feat.feature)
plt.show()
plt.show()
@staticmethod
def interpretation(iv):
if iv < 0.02:
return 'useless'
elif iv < 0.1:
return 'weak'
elif iv < 0.3:
return 'medium'
elif iv < 0.5:
return 'strong'
else:
return 'suspicious'
def interpret_iv(self, feat):
_, iv = self.calculate_iv(feat)
return self.interpretation(iv)
def print_iv(self, feat):
_, iv = self.calculate_iv(feat)
print('Information value: %0.2f' % iv)
print('%s is a %s predictor' % (feat.feature.capitalize(), self.interpretation(iv)))