-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogisticRegression.py
More file actions
277 lines (178 loc) · 8.36 KB
/
logisticRegression.py
File metadata and controls
277 lines (178 loc) · 8.36 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
######################################################
#Imports
######################################################
import pandas as pd
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import recall_score, make_scorer, confusion_matrix, roc_curve, auc
from sklearn.utils.multiclass import unique_labels
from sklearn.preprocessing import StandardScaler
import seaborn as sns
import matplotlib.pyplot as plt
######################################################
#Reading in and formatting data
######################################################
#starting with the first outcome
#load training and validation data. Non-duplicated. Manual Feature extraction.
Xtrain1 = pd.read_csv('./DataSplit/Xtrain1')
ytrain1 = pd.read_csv('./DataSplit/ytrain1')
Xvalidate1 = pd.read_csv('./DataSplit/Xvalidate1')
yvalidate1 = pd.read_csv('./DataSplit/yvalidate1')
#check that the data are correct
print("X train shape", Xtrain1.shape)
print("y train shape", ytrain1.shape)
print("X validate shape", Xvalidate1.shape)
print("y validate shape", yvalidate1.shape)
#change the y's to numpy arrays of type integer
yvalidate1 = yvalidate1['modOneOutcome'].values
# print("type of valid", type(yvalidate1))
yvalidate1 = yvalidate1.astype('int64')
#print("these validation outcomes of type integer", yvalidate1.dtype)
ytrain1 = ytrain1['modOneOutcome'].values
ytrain1 = ytrain1.astype('int64')
######################################################
#Functions to get metrics
######################################################
# Computing metrics function from Lab 7
def compute_performance(yhat, y):
# First, get tp, tn, fp, fn
tp = sum(np.logical_and(yhat == 1, y == 1))
tn = sum(np.logical_and(yhat == 0, y == 0))
fp = sum(np.logical_and(yhat == 1, y == 0))
fn = sum(np.logical_and(yhat == 0, y == 1))
print(f"tp: {tp} tn: {tn} fp: {fp} fn: {fn}")
# Accuracy
trainacc = (tp + tn) / (tp + tn + fp + fn)
# Precision
# "Of the ones I labeled +, how many are actually +?"
precision = tp / (tp + fp)
# Recall
# "Of all the + in the data, how many do I correctly label?"
recall = tp / (tp + fn)
# Sensitivity
# "Of all the + in the data, how many do I correctly label?"
sensitivity = recall
# Specificity
# "Of all the - in the data, how many do I correctly label?"
specificity = tn / (fp + tn)
# Print results
print("Acc:",trainacc,"Sens:",sensitivity,"Spec:",specificity)
######################################################
#Model Building
######################################################
#pipeline for LR model - parameters that give best performance
logisticPipeline = Pipeline([
('logistic regression', LogisticRegression(solver = 'lbfgs',
penalty = 'l2',
C = 10,
max_iter = 10000))
])
###run for different combinations of things
#fit model - manual feature selection; no duplication; obj 1 outcome
lrM1 = logisticPipeline.fit(Xtrain1, ytrain1)
#make predictions on validation set
lrM1_validPred = lrM1.predict(Xvalidate1) #.reshape(-1,1)
lrM1_validPred = lrM1_validPred.astype('int64') #convert to type integer
#call above function to get performance metrics
print("\nPerformance metrics for logistic regression for Objective 1 outcome, non-duplicated data\n")
compute_performance(lrM1_validPred, yvalidate1)
#call sklearn functions to get ROC curve (currently commented out)
yvalM1_pred_prob = lrM1.predict_proba(Xvalidate1)[:,1]
fpr_M1, tpr_M1, _ = roc_curve(yvalidate1, yvalM1_pred_prob)
#incomment roc curve plot
# sns.lineplot(fpr_M1,tpr_M1)
# plt.show()
#AUC metrics
auc_M1 = auc(fpr_M1, tpr_M1)
print("AUC\n", auc_M1)
######################################################
#Reading in and formatting data for objective 1 duplicated manual feature selection
######################################################
#starting with the first outcome
#load training and validation data. Non-duplicated. Manual Feature extraction.
XtrainDuplicate1 = pd.read_csv('./DataSplit/XtrainDuplicate1')
ytrainDuplicate1 = pd.read_csv('./DataSplit/ytrainDuplicate1')
ytrainDuplicate1 = ytrainDuplicate1['modOneOutcome'].values
ytrainDuplicate1 = ytrainDuplicate1.astype('int64')
#fit model - manual feature selection; no duplication; obj 1 outcome
lrMDup1 = logisticPipeline.fit(XtrainDuplicate1, ytrainDuplicate1)
#make predictions on validation set
lrMDup1_validPred = lrMDup1.predict(Xvalidate1) #.reshape(-1,1)
lrMDup1_validPred = lrMDup1_validPred.astype('int64') #convert to type integer
#call above function to get performance metrics
print("\nPerformance metrics for logistic regression for Objective 1 outcome, duplicated data\n")
compute_performance(lrMDup1_validPred, yvalidate1)
#call sklearn functions to get ROC curve (currently commented out)
yvalMDup1_pred_prob = lrMDup1.predict_proba(Xvalidate1)[:,1]
fpr_MDup1, tpr_MDup1, _ = roc_curve(yvalidate1, yvalMDup1_pred_prob)
#incomment roc curve plot
# sns.lineplot(fpr_M1,tpr_M1)
# plt.show()
#AUC metrics
auc_MDup1 = auc(fpr_MDup1, tpr_MDup1)
print("AUC\n", auc_MDup1)
######################################################
#Reading in and formatting data for objective 2
######################################################
#starting with the first outcome
#load training and validation data. Non-duplicated. Manual Feature extraction.
Xtrain2 = pd.read_csv('./DataSplit/Xtrain2')
ytrain2 = pd.read_csv('./DataSplit/ytrain2')
Xvalidate2 = pd.read_csv('./DataSplit/Xvalidate2')
yvalidate2 = pd.read_csv('./DataSplit/yvalidate2')
#check that the data are correct
print("X train shape", Xtrain2.shape)
print("y train shape", ytrain2.shape)
print("X validate shape", Xvalidate2.shape)
print("y validate shape", yvalidate2.shape)
#change the y's to numpy arrays of type integer
yvalidate2 = yvalidate2['modTwoOutcome'].values
# print("type of valid", type(yvalidate1))
yvalidate2 = yvalidate2.astype('int64')
#print("these validation outcomes of type integer", yvalidate1.dtype)
ytrain2 = ytrain2['modTwoOutcome'].values
ytrain2 = ytrain2.astype('int64')
#fit model - manual feature selection; no duplication; obj 1 outcome
lrM2 = logisticPipeline.fit(Xtrain2, ytrain2)
#make predictions on validation set
lrM2_validPred = lrM2.predict(Xvalidate2) #.reshape(-1,1)
lrM2_validPred = lrM2_validPred.astype('int64') #convert to type integer
#call above function to get performance metrics
print("\nPerformance metrics for logistic regression for Objective 2 outcome, non-duplicated data\n")
compute_performance(lrM2_validPred, yvalidate2)
#call sklearn functions to get ROC curve (currently commented out)
yvalM2_pred_prob = lrM2.predict_proba(Xvalidate2)[:,1]
fpr_M2, tpr_M2, _ = roc_curve(yvalidate2, yvalM2_pred_prob)
#incomment roc curve plot
# sns.lineplot(fpr_M1,tpr_M1)
# plt.show()
#AUC metrics
auc_M2 = auc(fpr_M2, tpr_M2)
print("AUC\n", auc_M2)
######################################################
#Reading in and formatting data for objective 2 duplicated manual feature selection
######################################################
#starting with the first outcome
#load training and validation data. Non-duplicated. Manual Feature extraction.
XtrainDuplicate2 = pd.read_csv('./DataSplit/XtrainDuplicate2')
ytrainDuplicate2 = pd.read_csv('./DataSplit/ytrainDuplicate2')
ytrainDuplicate2 = ytrainDuplicate2['modTwoOutcome'].values
ytrainDuplicate2 = ytrainDuplicate2.astype('int64')
#fit model - manual feature selection; no duplication; obj 1 outcome
lrMDup2 = logisticPipeline.fit(XtrainDuplicate2, ytrainDuplicate2)
#make predictions on validation set
lrMDup2_validPred = lrMDup2.predict(Xvalidate2) #.reshape(-1,1)
lrMDup2_validPred = lrMDup2_validPred.astype('int64') #convert to type integer
#call above function to get performance metrics
print("\nPerformance metrics for logistic regression for Objective 2 outcome, duplicated data\n")
compute_performance(lrMDup2_validPred, yvalidate2)
#call sklearn functions to get ROC curve (currently commented out)
yvalMDup2_pred_prob = lrMDup2.predict_proba(Xvalidate2)[:,1]
fpr_MDup2, tpr_MDup2, _ = roc_curve(yvalidate2, yvalMDup2_pred_prob)
#incomment roc curve plot
# sns.lineplot(fpr_M1,tpr_M1)
# plt.show()
#AUC metrics
auc_MDup2 = auc(fpr_MDup2, tpr_MDup2)
print("AUC\n", auc_MDup2)