-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogisticRegression.py
More file actions
216 lines (174 loc) · 8.7 KB
/
logisticRegression.py
File metadata and controls
216 lines (174 loc) · 8.7 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 15 16:26:33 2021
@author: rapha
"""
# In depth analysis of logistic Regression classification
from textvectorization import loadTfidfdataset
from sklearn.linear_model import LogisticRegression
import sklearn.metrics as metrics
import pickle
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
random_seed = 42
def run_logRegression(savefile, vectorizer, ttsplit,
getFeatImportance = True, clean = True):
"""
Parameters
----------
savefile : string
Folder from which to load and where to save data.
vectorizer : string
only sklearn supported.
ttsplit : number between 0 and 1
choose train test split.
getFeatImportance : Bool, optional
Should feature importance be calculated and plotted. The default is True.
clean : Bool, optional
Use lemmatized and stemmed data. The default is True.
Returns
-------
TYPE
score, feature importance data frame (only if feature importance was selected).
"""
# load data
texts_train, texts_test, labels_train, labels_test = loadTfidfdataset(
savefile, vectorizer = vectorizer, split = ttsplit, clean = clean)
# create classifier and create predictions and calculate score
classifier = LogisticRegression(random_state = random_seed).fit(texts_train, labels_train)
preds = classifier.predict(texts_test)
score = metrics.accuracy_score(labels_test, preds)
report = metrics.classification_report(labels_test, preds)
print(score)
# save data with right labels and create feature importance df
if clean == True:
with open(savefile + "/" + vectorizer + "_tfidf_logReg_report_clean.txt", "w" ) as info:
info.write(report)
if getFeatImportance == True:
with open(savefile + "/feature_names_clean.pickle", 'rb') as handle:
features = pickle.load(handle)
coefficients = classifier.coef_
intercept = classifier.intercept_
intercepts = list(intercept)*len(features)
df = pd.DataFrame(data = (zip(features, list(coefficients[0]), intercepts)),
columns = ["feature", "coefficient", "intercept"])
df.to_csv(savefile + "/LogReg_featImportance_clean.csv")
return score, df
else:
return score
else:
with open(savefile + "/" + vectorizer + "_tfidf_logReg_report.txt", "w" ) as info:
info.write(report)
if getFeatImportance == True:
with open(savefile + "/feature_names.pickle", 'rb') as handle:
features = pickle.load(handle)
coefficients = classifier.coef_
intercept = classifier.intercept_
intercepts = list(intercept)*len(features)
df = pd.DataFrame(data = (zip(features, list(coefficients[0]), intercepts)),
columns = ["feature", "coefficient", "intercept"])
df.to_csv(savefile + "/LogReg_featImportance.csv")
return score, df
else:
return score
def feat_imp_plot_lr(featureImportance, savefile, plot_mostImportant = True, clean = True):
# plot feature importance distribution from dataframe, optionally plot most important features (positive and negative)
if clean:
clean_label = "clean"
else:
clean_label = ""
fig, ax = plt.subplots(figsize= (5,10))
fi = featureImportance["coefficient"]
# count features above 1 and below -1
aboveone = np.sum(fi>1)
belowmone = np.sum(fi<-1)
mean = np.mean(fi)
sd = np.std(fi)
var = np.var(fi)
interc = featureImportance["intercept"][0]
ax.boxplot(fi)
ax.axhline(1, color = "green")
ax.axhline(-1, color = "red")
with open(savefile + "/LogReg_featImp_stats_" + clean_label + ".txt", "w" ) as info:
info.write(savefile + "\nFeatsAboveOne: {} \nFeatsBelowMinusOne: {} \nMean: {} \n\
StdDev {} \nVar: {} \nInterc: {} ".format(aboveone, belowmone,
mean, sd, var, interc))
plt.savefig(savefile + "/LogReg_featImpdistribution_" + clean_label + ".png")
if plot_mostImportant:
# create most important feaures and save most important features as csv
mifeatsplus = featureImportance[featureImportance["coefficient"] >1].sort_values(by = "coefficient", ascending = False)
mifeatsminus = featureImportance[featureImportance["coefficient"] <-1].sort_values(by = "coefficient", ascending = True)
mifeatsplus.to_csv(savefile + "/LogReg_mi_pos_feats_" + clean_label + ".csv")
mifeatsminus.to_csv(savefile + "/LogReg_mi_neg_feats_" + clean_label + ".csv")
fig, axs = plt.subplots(nrows = 1, ncols = 1, figsize= (10,30))
plusfeats = list(mifeatsplus["feature"])
pluscoeff = list(mifeatsplus["coefficient"])
minusfeats = list(mifeatsminus["feature"])
minuscoeff = list(mifeatsminus["coefficient"])
# Horizontal Bar Plot
axs.barh(plusfeats, pluscoeff, color = "green")
# Remove axes splines
for s in ['top', 'bottom', 'left', 'right']:
axs.spines[s].set_visible(False)
# Remove x, y Ticks
axs.xaxis.set_ticks_position('none')
axs.yaxis.set_ticks_position('none')
# Add padding between axes and labels
axs.xaxis.set_tick_params(pad = 5)
axs.yaxis.set_tick_params(pad = 10)
# Add x, y gridlines
axs.grid(b = True, color ='grey',
linestyle ='-.', linewidth = 0.5,
alpha = 0.2)
# Show top values
axs.invert_yaxis()
# Add annotation to bars
for i in axs.patches:
plt.text(i.get_width() + 0.2, i.get_y()+0.5,
str(round((i.get_width()), 2)),
fontsize = 10, fontweight ='bold',
color ='black')
fig.savefig(savefile + "/LogReg_pos_imp_feats_" + clean_label + ".png")
fig, axs = plt.subplots(nrows = 1, ncols = 1, figsize= (10,30))
# Horizontal Bar Plot
axs.barh(minusfeats, minuscoeff, color = "red")
# Remove axes splines
for s in ['top', 'bottom', 'left', 'right']:
axs.spines[s].set_visible(False)
# Remove x, y Ticks
axs.xaxis.set_ticks_position('none')
axs.yaxis.set_ticks_position('none')
# Add padding between axes and labels
axs.xaxis.set_tick_params(pad = 5)
axs.yaxis.set_tick_params(pad = 10)
# Add x, y gridlines
axs.grid(b = True, color ='grey',
linestyle ='-.', linewidth = 0.5,
alpha = 0.2)
# Show top values
axs.invert_yaxis()
# Add annotation to bars
for i in axs.patches:
plt.text(0.2, i.get_y()+0.5,
str(round((i.get_width()), 2)),
fontsize = 10, fontweight ='bold',
color ='black')
fig.savefig(savefile + "/LogReg_neg_imp_feats_" + clean_label + ".png")
return 1
# Run for balanced dataset, cleaned (lemmatized and stemmed) and simply cleaned dataset
score, feat_imp = run_logRegression(savefile = "wikidset_comb_1_basic", vectorizer = "sklearn",
ttsplit = 0.2, clean = True)
feat_imp_plot_lr(feat_imp, savefile = "wikidset_comb_1_basic", plot_mostImportant = True,
clean = True)
score, feat_imp = run_logRegression(savefile = "wikidset_comb_1_basic", vectorizer = "sklearn",
ttsplit = 0.2, clean = False)
feat_imp_plot_lr(feat_imp, savefile = "wikidset_comb_1_basic", plot_mostImportant = True,
clean = False)
# run_logRegression(savefile = "wikidset_comb_2_basic", vectorizer = "sklearn", ttsplit = 0.2)
# run_logRegression(savefile = "wikidset_comb_5_basic", vectorizer = "sklearn", ttsplit = 0.2)
# run_logRegression(savefile = "wikidset_comb_10_basic", vectorizer = "sklearn", ttsplit = 0.2)
# run_randomForest(savefile = "wikidset_comb_1_basic", vectorizer = "sklearn", ttsplit = 0.2)
# run_randomForest(savefile = "wikidset_comb_2_basic", vectorizer = "sklearn", ttsplit = 0.2)
# run_randomForest(savefile = "wikidset_comb_5_basic", vectorizer = "sklearn", ttsplit = 0.2)
# run_randomForest(savefile = "wikidset_comb_10_basic", vectorizer = "sklearn", ttsplit = 0.2)