forked from francisnardi/hackaton-algar-2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogisticRegression.py
187 lines (159 loc) · 6.78 KB
/
logisticRegression.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
177
178
179
180
181
182
183
184
185
186
187
# https://colab.research.google.com/drive/1H9epx3rtmUVdhe0Qb_L5O2rQt8q7CpLZ
# Load All Libraries
from __future__ import print_function
from collections import Counter
import sklearn
import sklearn.datasets
import sklearn.ensemble
import numpy as np
import lime
import lime.lime_tabular
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.base import TransformerMixin
from sklearn.preprocessing import Imputer
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score
from sklearn import cross_validation
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier
import random
import re
import warnings
warnings.filterwarnings("ignore")
train_filename = 'train.csv'
test_filename = 'test.csv'
train_df = pd.read_csv(train_filename, header=0)
test_df = pd.read_csv(test_filename, header=0)
cols=train_df.columns
train_df['source']='train'
test_df['source']='test'
data = pd.concat([train_df, test_df],ignore_index=True)
data = data.fillna(0)
train_df = data.loc[data['source']=="train"]
test_df = data.loc[data['source']=="test"]
train_target = np.ravel(np.array(train_df['Churn'].values))
train_df = train_df.drop(['Churn'],axis=1)
float_columns=[]
cat_columns=[]
int_columns=[]
for i in train_df.columns:
if i not in cols:
continue
if train_df[i].dtype == 'float' :
float_columns.append(i)
elif train_df[i].dtype == 'int64' or train_df[i].dtype == 'int32' or train_df[i].dtype == 'int16' or train_df[i].dtype == 'int8':
int_columns.append(i)
elif train_df[i].dtype == 'object':
cat_columns.append(i)
train_cat_features = train_df[cat_columns]
train_float_features = train_df[float_columns]
train_int_features = train_df[int_columns]
train_cat_features_ver2 = train_cat_features.apply(LabelEncoder().fit_transform)
temp_1 = np.concatenate((train_cat_features_ver2,train_float_features),axis=1)
train_transformed_features = np.concatenate((temp_1,train_int_features),axis=1)
train_transformed_features = pd.DataFrame(data=train_transformed_features)
array = train_transformed_features.values
number_of_features = len(array[0])
X = array[:,0:number_of_features]
Y = train_target
validation_size = 0.2
seed = 7
X_train, X_validation, Y_train, Y_validation = cross_validation.train_test_split(X, Y, test_size=validation_size, random_state=seed)
scoring = 'accuracy'
# Model 1 - Logisitic Regression
model_logreg = LogisticRegression()
model_logreg.fit(X_train, Y_train)
#print("LR: " + str(accuracy_score(Y_validation, model_logreg.predict(X_validation))))
# Model 2 - RandomForest Classifier
#model_rf = RandomForestClassifier()
#model_rf.fit(X_train, Y_train)
#print("RF: " + str(accuracy_score(Y_validation, model_rf.predict(X_validation))))
# Model 3 - XGB Classifier
#model_xgb = XGBClassifier()
#model_xgb.fit(X_train, Y_train)
#print("XGB: " + str(accuracy_score(Y_validation, model_xgb.predict(X_validation))))
#model_logreg = LogisticRegression()
#model_logreg.fit(X, Y)
#model_rf = RandomForestClassifier()
#model_rf.fit(X, Y)
#model_xgb = XGBClassifier()
#model_xgb.fit(X, Y)
# LIME SECTION
predict_fn_logreg = lambda x: model_logreg.predict_proba(x).astype(float)
# Line-up the feature names
feature_names_cat = list(train_cat_features_ver2)
feature_names_float = list(train_float_features)
feature_names_int = list(train_int_features)
feature_names = sum([feature_names_cat, feature_names_float, feature_names_int], [])
#print(feature_names)
# Create the LIME Explainer
explainer = lime.lime_tabular.LimeTabularExplainer(X_train ,feature_names = feature_names,class_names=['Stay','Leave'],
categorical_features=cat_columns,
categorical_names=feature_names_cat, kernel_width=2)
# Pick the observation in the validation set for which explanation is required
# Get the explanation for Logistic Regression
k = 0
procn = 0
procp = 0
positive = []
dp = dict()
dn = dict()
negative = []
examples = 10
while k < len(X_validation) and (procn < examples or procp < examples):
n = random.randint(0,len(X_validation)-1)
k+=1
exp = explainer.explain_instance(X_validation[n], predict_fn_logreg, num_features=6)
#print(exp.as_list())
if model_logreg.predict(X_validation[k].reshape(1,-1)) == 'Yes':
for i in range(2):
s = re.sub("[^a-zA-Z]+","",exp.as_list()[i][0])
if s in feature_names_cat:
if exp.as_list()[i][1] > 0:
final = s+" estado: "+str(data[s][n])
if final not in dp:
dp[final] = exp.as_list()[i][1]
else:
dp[final] = dp.get(final) + exp.as_list()[i][1]
positive.append(s+" estado: "+str(data[s][n]))
else:
if exp.as_list()[i][1] > 0:
final = s+" estado: "+str(exp.as_list()[i][0])
if final not in dp:
dp[final] = exp.as_list()[i][1]
else:
dp[final] = dp.get(final) + exp.as_list()[i][1]
positive.append(final)
procp+=1
else:
for i in range(2):
s = re.sub("[^a-zA-Z]+","",exp.as_list()[i][0])
if s in feature_names_cat:
if exp.as_list()[i][1] < 0:
final = s+" estado: "+str(data[s][n])
if final not in dn:
dn[final] = exp.as_list()[i][1]
else:
dn[final] = dn.get(final) + exp.as_list()[i][1]
negative.append(s+" estado: "+str(data[s][n]))
else:
if exp.as_list()[i][1] < 0:
final = s+" estado: "+str(exp.as_list()[i][0])
if final not in dn:
dn[final] = exp.as_list()[i][1]
else:
dn[final] = dn.get(final) + exp.as_list()[i][1]
negative.append(final)
procn+=1
#print(exp.as_list())
print("Fez o cliente sair: ")
for key,value in Counter(positive).most_common(3):
print(str(key)+", surgiu "+str(value)+" vezes, puxando a probabilidade para essa classe em "+str("{:.2f}".format(abs(dp[key]/value) * 100))+"%")
print("Fez o cliente ficar: ")
for key,value in Counter(negative).most_common(3):
print(str(key)+", surgiu "+str(value)+" vezes"+" vezes, puxando a probabilidade para essa classe em "+str("{:.2f}".format(abs(dn[key]/value) * 100))+"%")