-
Notifications
You must be signed in to change notification settings - Fork 5
/
MachineLearning_InSample7_AWW.py
392 lines (320 loc) · 16.6 KB
/
MachineLearning_InSample7_AWW.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 25 21:50:48 2017
@author: yzhang17
"""
import numpy as np
from sklearn.model_selection import LeaveOneGroupOut,GridSearchCV
from sklearn.metrics import roc_auc_score
from sklearn import svm
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier, NearestNeighbors
from sklearn.ensemble import RandomForestClassifier,IsolationForest
dataPath = 'AlanWalksWales/'
# Comment out one of the two data names to change portion of data to use
dataName = 'AWW_rest'
#dataName = 'AWW_walk'
nJobs = 12 # Number of cores to use
# Load feature matrices, labels, and groups (denoting which labeled time
# segment each row of the feature matrix comes from)
featuresAll = np.loadtxt(dataPath+dataName+'_all.csv',delimiter=',')
featuresAcc = np.loadtxt(dataPath+dataName+'_acc.csv',delimiter=',')
featuresEda = np.loadtxt(dataPath+dataName+'_eda.csv',delimiter=',')
labels = np.loadtxt(dataPath+dataName+'_label.csv')
groups = np.loadtxt(dataPath+dataName+'_groups.csv')
# Leave-one-group-out cross-validation
cv = LeaveOneGroupOut()
# Svm
# Parameter tuning by grid search
regParamC = 10. ** np.arange(-5, 4)
regParamG = 10. ** np.arange(-9, 1)
parameters = {'gamma': regParamG, 'C': regParamC}
svmgsAll = GridSearchCV(svm.SVC(),
parameters,'roc_auc',n_jobs=nJobs,cv=cv,refit=False,
verbose=1)
svmgsAll.fit(featuresAll,labels,groups)
svmbestgamma_All = svmgsAll.best_params_['gamma']
svmbestC_All = svmgsAll.best_params_['C']
svmgsAcc = GridSearchCV(svm.SVC(),
parameters,'roc_auc',n_jobs=nJobs,cv=cv,refit=False,
verbose=1)
svmgsAcc.fit(featuresAcc,labels,groups)
svmbestgamma_Acc = svmgsAcc.best_params_['gamma']
svmbestC_Acc = svmgsAcc.best_params_['C']
svmgsEda = GridSearchCV(svm.SVC(),
parameters,'roc_auc',n_jobs=nJobs,cv=cv,refit=False,
verbose=1)
svmgsEda.fit(featuresEda,labels,groups)
svmbestgamma_Eda = svmgsEda.best_params_['gamma']
svmbestC_Eda = svmgsEda.best_params_['C']
svmpredAll = np.zeros(np.shape(labels))
svmpredAcc = np.zeros(np.shape(labels))
svmpredEda = np.zeros(np.shape(labels))
for train, test in cv.split(featuresAll,labels,groups):
svmAll = svm.SVC(gamma=svmbestgamma_All, C=svmbestC_All,probability=True)
svmAll.fit(featuresAll[train,:],labels[train])
svmpredAll[test] = svmAll.predict_proba(featuresAll[test,:])[:,1]
svmAcc = svm.SVC(gamma=svmbestgamma_Acc, C=svmbestC_Acc,probability=True)
svmAcc.fit(featuresAcc[train,:],labels[train])
svmpredAcc[test] = svmAcc.predict_proba(featuresAcc[test,:])[:,1]
svmEda = svm.SVC(gamma=svmbestgamma_Eda, C=svmbestC_Eda,probability=True)
svmEda.fit(featuresEda[train,:],labels[train])
svmpredEda[test] = svmEda.predict_proba(featuresEda[test,:])[:,1]
# Save the scores for further analysis
#np.save('svmpredAllScores_walk',svmpredAll)
#np.save('svmpredAccScores_walk',svmpredAcc)
#np.save('svmpredEdaScores_walk',svmpredEda)
print('SVM AUC ALL: %f (%s)' % (roc_auc_score(labels,svmpredAll),svmgsAll.best_params_))
print('SVM AUC ACC: %f (%s)' % (roc_auc_score(labels,svmpredAcc),svmgsAcc.best_params_))
print('SVM AUC EDA: %f (%s)' % (roc_auc_score(labels,svmpredEda),svmgsEda.best_params_))
# Logistic
# Parameter tuning by grid search
regParam = 10.0**-np.arange(-5,5)
parameters = {'C': regParam}
gsAll = GridSearchCV(LogisticRegression(),
parameters,'roc_auc',n_jobs=nJobs,cv=cv,refit=False,
verbose=1)
gsAll.fit(featuresAll,labels,groups)
bestC_All = gsAll.best_params_['C']
gsAcc = GridSearchCV(LogisticRegression(),
parameters,'roc_auc',n_jobs=nJobs,cv=cv,refit=False,
verbose=1)
gsAcc.fit(featuresAcc,labels,groups)
bestC_Acc = gsAcc.best_params_['C']
gsEda = GridSearchCV(LogisticRegression(),
parameters,'roc_auc',n_jobs=nJobs,cv=cv,refit=False,
verbose=1)
gsEda.fit(featuresEda,labels,groups)
bestC_Eda = gsEda.best_params_['C']
LogisticpredAll = np.zeros(np.shape(labels))
LogisticpredAcc = np.zeros(np.shape(labels))
LogisticpredEda = np.zeros(np.shape(labels))
for train, test in cv.split(featuresAll,labels,groups):
lrAll = LogisticRegression(C=bestC_All)
lrAll.fit(featuresAll[train,:],labels[train])
LogisticpredAll[test] = lrAll.predict_proba(featuresAll[test,:])[:,1]
lrAcc = LogisticRegression(C=bestC_Acc)
lrAcc.fit(featuresAcc[train,:],labels[train])
LogisticpredAcc[test] = lrAcc.predict_proba(featuresAcc[test,:])[:,1]
lrEda = LogisticRegression(C=bestC_Eda)
lrEda.fit(featuresEda[train,:],labels[train])
LogisticpredEda[test] = lrEda.predict_proba(featuresEda[test,:])[:,1]
# Save the scores for further analysis
#np.save('LogisticpredAllScores_walk',LogisticpredAll)
#np.save('LogisticpredAccScores_walk',LogisticpredAcc)
#np.save('LogisticpredEdaScores_walk',LogisticpredEda)
print('LR AUC ALL: %f (%s)' % (roc_auc_score(labels,LogisticpredAll),gsAll.best_params_))
print('LR AUC ACC: %f (%s)' % (roc_auc_score(labels,LogisticpredAcc),gsAcc.best_params_))
print('LR AUC EDA: %f (%s)' % (roc_auc_score(labels,LogisticpredEda),gsEda.best_params_))
# kNN classify
# Parameter tuning by grid search
parameters = {'n_neighbors': np.arange(1, 81)}
kNNgsAll = GridSearchCV(KNeighborsClassifier(algorithm='auto', metric='euclidean'),
parameters,'roc_auc',n_jobs=nJobs,cv=cv,refit=False,
verbose=1)
kNNgsAll.fit(featuresAll,labels,groups)
bestneighbors_All = kNNgsAll.best_params_['n_neighbors']
kNNgsAcc = GridSearchCV(KNeighborsClassifier(algorithm='auto', metric='euclidean'),
parameters,'roc_auc',n_jobs=nJobs,cv=cv,refit=False,
verbose=1)
kNNgsAcc.fit(featuresAcc,labels,groups)
bestneighbors_Acc = kNNgsAcc.best_params_['n_neighbors']
kNNgsEda = GridSearchCV(KNeighborsClassifier(algorithm='auto', metric='euclidean'),
parameters,'roc_auc',n_jobs=nJobs,cv=cv,refit=False,
verbose=1)
kNNgsEda.fit(featuresEda,labels,groups)
bestneighbors_Eda = kNNgsEda.best_params_['n_neighbors']
knnCpredAll = np.zeros(np.shape(labels))
knnCpredAcc = np.zeros(np.shape(labels))
knnCpredEda = np.zeros(np.shape(labels))
for train, test in cv.split(featuresAll,labels,groups):
knnCAll = KNeighborsClassifier(n_neighbors=bestneighbors_All, algorithm='auto', metric='euclidean')
knnCAll.fit(featuresAll[train,:],labels[train])
knnCpredAll[test] = knnCAll.predict_proba(featuresAll[test,:])[:,1]
knnCAcc = KNeighborsClassifier(n_neighbors=bestneighbors_Acc, algorithm='auto', metric='euclidean')
knnCAcc.fit(featuresAcc[train,:],labels[train])
knnCpredAcc[test] = knnCAcc.predict_proba(featuresAcc[test,:])[:,1]
knnCEda = KNeighborsClassifier(n_neighbors=bestneighbors_Eda, algorithm='auto', metric='euclidean')
knnCEda.fit(featuresEda[train,:],labels[train])
knnCpredEda[test] = knnCEda.predict_proba(featuresEda[test,:])[:,1]
# Save the scores for further analysis
#np.save('knnCpredAllScores_walk',knnCpredAll)
#np.save('knnCpredAccScores_walk',knnCpredAcc)
#np.save('knnCpredEdaScores_walk',knnCpredEda)
print('kNNclass AUC ALL: %f (%s)' % (roc_auc_score(labels,knnCpredAll),kNNgsAll.best_params_))
print('kNNclass AUC ACC: %f (%s)' % (roc_auc_score(labels,knnCpredAcc),kNNgsAcc.best_params_))
print('kNNclass AUC EDA: %f (%s)' % (roc_auc_score(labels,knnCpredEda),kNNgsEda.best_params_))
# Random Forest
# Parameter tuning by grid search
RFparameters = {'n_estimators': 10*np.arange(1,21)}
RFgsAll = GridSearchCV(RandomForestClassifier(),
RFparameters,'roc_auc',n_jobs=nJobs,cv=cv,refit=False,
verbose=1)
RFgsAll.fit(featuresAll,labels,groups)
RFbestNumTreesAll = RFgsAll.best_params_['n_estimators']
RFgsAcc = GridSearchCV(RandomForestClassifier(),
RFparameters,'roc_auc',n_jobs=nJobs,cv=cv,refit=False,
verbose=1)
RFgsAcc.fit(featuresAcc,labels,groups)
RFbestNumTreesAcc = RFgsAcc.best_params_['n_estimators']
RFgsEda = GridSearchCV(RandomForestClassifier(),
RFparameters,'roc_auc',n_jobs=nJobs,cv=cv,refit=False,
verbose=1)
RFgsEda.fit(featuresEda,labels,groups)
RFbestNumTreesEda = RFgsEda.best_params_['n_estimators']
RFpredAll = np.zeros(np.shape(labels))
RFpredAcc = np.zeros(np.shape(labels))
RFpredEda = np.zeros(np.shape(labels))
for train, test in cv.split(featuresAll,labels,groups):
RFAll = RandomForestClassifier(n_estimators=RFbestNumTreesAll)
RFAll.fit(featuresAll[train,:],labels[train])
RFpredAll[test] = RFAll.predict_proba(featuresAll[test,:])[:,1]
RFAcc = RandomForestClassifier(n_estimators=RFbestNumTreesAcc)
RFAcc.fit(featuresAcc[train,:],labels[train])
RFpredAcc[test] = RFAcc.predict_proba(featuresAcc[test,:])[:,1]
RFEda = RandomForestClassifier(n_estimators=RFbestNumTreesEda)
RFEda.fit(featuresEda[train,:],labels[train])
RFpredEda[test] = RFEda.predict_proba(featuresEda[test,:])[:,1]
# Save the scores for further analysis
#np.save('RandomForestpredAllScores_walk',RFpredAll)
#np.save('RandomForestpredAccScores_walk',RFpredAcc)
#np.save('RandomForestpredEdaScores_walk',RFpredEda)
print('RF AUC ALL: %f (%s)' % (roc_auc_score(labels,RFpredAll),RFgsAll.best_params_))
print('RF AUC ACC: %f (%s)' % (roc_auc_score(labels,RFpredAcc),RFgsAcc.best_params_))
print('RF AUC EDA: %f (%s)' % (roc_auc_score(labels,RFpredEda),RFgsEda.best_params_))
# kNN distance
# Parameter tuning by grid search
maxNb = 30
awwCv = LeaveOneGroupOut()
awwCvAucAll = np.zeros((17,maxNb))
awwCvAucAcc = np.zeros((17,maxNb))
awwCvAucEda = np.zeros((17,maxNb))
fold = 0
for train,test in awwCv.split(featuresAll,labels,groups):
knnDAll = NearestNeighbors(n_neighbors=maxNb, algorithm='auto',
metric='euclidean')
knnDAll.fit(featuresAll[train,:])
distancesAll,indices = knnDAll.kneighbors(featuresAll[test,:])
knnDAcc = NearestNeighbors(n_neighbors=maxNb, algorithm='auto',
metric='euclidean')
knnDAcc.fit(featuresAcc[train,:])
distancesAcc,indices = knnDAcc.kneighbors(featuresAcc[test,:])
knnDEda = NearestNeighbors(n_neighbors=maxNb, algorithm='auto',
metric='euclidean')
knnDEda.fit(featuresEda[train,:])
distancesEda,indices = knnDEda.kneighbors(featuresEda[test,:])
for nNb in range(maxNb):
awwCvAucAll[fold,nNb] = roc_auc_score(
labels[test],distancesAll[:,nNb])
awwCvAucAcc[fold,nNb] = roc_auc_score(
labels[test],distancesAcc[:,nNb])
awwCvAucEda[fold,nNb] = roc_auc_score(
labels[test],distancesEda[:,nNb])
fold += 1
awwBestNbAll = np.argmax(np.mean(awwCvAucAll,axis=0)) + 1
awwBestNbAcc = np.argmax(np.mean(awwCvAucAcc,axis=0)) + 1
awwBestNbEda = np.argmax(np.mean(awwCvAucEda,axis=0)) + 1
distancesAll = np.zeros((np.size(labels),awwBestNbAll))
distancesAcc = np.zeros((np.size(labels),awwBestNbAcc))
distancesEda = np.zeros((np.size(labels),awwBestNbEda))
for train1, test1 in cv.split(featuresAll,labels,groups):
knnDAll = NearestNeighbors(n_neighbors=awwBestNbAll, algorithm='auto', metric='euclidean')
knnDAll.fit(featuresAll[train1,:])
distancesAll[test1],indices = knnDAll.kneighbors(featuresAll[test1,:])
knnDAcc = NearestNeighbors(n_neighbors=awwBestNbAcc, algorithm='auto', metric='euclidean')
knnDAcc.fit(featuresAcc[train1,:])
distancesAcc[test1],indices = knnDAcc.kneighbors(featuresAcc[test1,:])
knnDEda = NearestNeighbors(n_neighbors=awwBestNbEda, algorithm='auto', metric='euclidean')
knnDEda.fit(featuresEda[train1,:])
distancesEda[test1],indices = knnDEda.kneighbors(featuresEda[test1,:])
# Save the scores for further analysis
#np.save('distancespredAllScores_walk',distancesAll[:,awwBestNbAll-1])
#np.save('distancespredAccScores_walk',distancesAcc[:,awwBestNbAcc-1])
#np.save('distancespredEdaScores_walk',distancesEda[:,awwBestNbEda-1])
print('kNNdist AUC ALL: %f (%d neighbors)' % (roc_auc_score(labels,
distancesAll[:,awwBestNbAll-1]),awwBestNbAll))
print('kNNdist AUC ACC: %f (%d neighbors)' % (roc_auc_score(labels,
distancesAcc[:,awwBestNbAcc-1]),awwBestNbAcc))
print('kNNdist AUC EDA: %f (%d neighbors)' % (roc_auc_score(labels,
distancesEda[:,awwBestNbEda-1]),awwBestNbEda))
# 1 class Svm
# Parameter tuning by grid search
regParamG = 10. ** np.arange(-4, 4)
regParamNu = 2. ** np.arange(-12,0)
parameters = {'gamma': regParamG, 'nu': regParamNu}
OnesvmgsAll = GridSearchCV(svm.OneClassSVM(),
parameters,'roc_auc',n_jobs=nJobs,cv=cv,refit=False,
verbose=1)
OnesvmgsAll.fit(featuresAll,1-labels,groups)
Onesvmbestgamma_All = OnesvmgsAll.best_params_['gamma']
Onesvmbestnu_All = OnesvmgsAll.best_params_['nu']
OnesvmgsAcc = GridSearchCV(svm.OneClassSVM(),
parameters,'roc_auc',n_jobs=nJobs,cv=cv,refit=False,
verbose=1)
OnesvmgsAcc.fit(featuresAcc,1-labels,groups)
Onesvmbestgamma_Acc = OnesvmgsAcc.best_params_['gamma']
Onesvmbestnu_Acc = OnesvmgsAcc.best_params_['nu']
OnesvmgsEda = GridSearchCV(svm.OneClassSVM(),
parameters,'roc_auc',n_jobs=nJobs,cv=cv,refit=False,
verbose=1)
OnesvmgsEda.fit(featuresEda,1-labels,groups)
Onesvmbestgamma_Eda = OnesvmgsEda.best_params_['gamma']
Onesvmbestnu_Eda = OnesvmgsEda.best_params_['nu']
OnesvmpredAll = np.zeros(np.shape(labels))
OnesvmpredAcc = np.zeros(np.shape(labels))
OnesvmpredEda = np.zeros(np.shape(labels))
for train, test in cv.split(featuresAll,labels,groups):
OnesvmAll = svm.OneClassSVM(gamma=Onesvmbestgamma_All, nu=Onesvmbestnu_All, kernel='rbf')
OnesvmAll.fit(featuresAll[train,:])
OnesvmpredAll[test] = OnesvmAll.decision_function(featuresAll[test,:]).ravel()
OnesvmAcc = svm.OneClassSVM(gamma=Onesvmbestgamma_Acc, nu=Onesvmbestnu_Acc, kernel='rbf')
OnesvmAcc.fit(featuresAcc[train,:])
OnesvmpredAcc[test] = OnesvmAcc.decision_function(featuresAcc[test,:]).ravel()
OnesvmEda = svm.OneClassSVM(gamma=Onesvmbestgamma_Eda, nu=Onesvmbestnu_Eda, kernel='rbf')
OnesvmEda.fit(featuresEda[train,:])
OnesvmpredEda[test] = OnesvmEda.decision_function(featuresEda[test,:]).ravel()
# Save the scores for further analysis
#np.save('OnesvmpredAllScores_walk',OnesvmpredAll)
#np.save('OnesvmpredAccScores_walk',OnesvmpredAcc)
#np.save('OnesvmpredEdaScores_walk',OnesvmpredEda)
print('OneSVM AUC ALL: %f (%s)' % (roc_auc_score(1-labels,OnesvmpredAll),OnesvmgsAll.best_params_))
print('OneSVM AUC ACC: %f (%s)' % (roc_auc_score(1-labels,OnesvmpredAcc),OnesvmgsAcc.best_params_))
print('OneSVM AUC EDA: %f (%s)' % (roc_auc_score(1-labels,OnesvmpredEda),OnesvmgsEda.best_params_))
# Isolation Forest
# Parameter tuning by grid search
IFparameters = {'n_estimators': 10*np.arange(1,21)}
IFgsAll = GridSearchCV(IsolationForest(),
IFparameters,'roc_auc',n_jobs=nJobs,cv=cv,refit=False,
verbose=1)
IFgsAll.fit(featuresAll,1-labels,groups)
bestNumTreesAll = IFgsAll.best_params_['n_estimators']
IFgsAcc = GridSearchCV(IsolationForest(),
IFparameters,'roc_auc',n_jobs=nJobs,cv=cv,refit=False,
verbose=1)
IFgsAcc.fit(featuresAcc,1-labels,groups)
bestNumTreesAcc = IFgsAcc.best_params_['n_estimators']
IFgsEda = GridSearchCV(IsolationForest(),
IFparameters,'roc_auc',n_jobs=nJobs,cv=cv,refit=False,
verbose=1)
IFgsEda.fit(featuresEda,1-labels,groups)
bestNumTreesEda = IFgsEda.best_params_['n_estimators']
IFpredAll = np.zeros(np.shape(labels))
IFpredAcc = np.zeros(np.shape(labels))
IFpredEda = np.zeros(np.shape(labels))
for train, test in cv.split(featuresAll,labels,groups):
IFAll = IsolationForest(n_estimators=bestNumTreesAll)
IFAll.fit(featuresAll[train,:])
IFpredAll[test] = IFAll.decision_function(featuresAll[test,:])
IFAcc = IsolationForest(n_estimators=bestNumTreesAcc)
IFAcc.fit(featuresAcc[train,:])
IFpredAcc[test] = IFAcc.decision_function(featuresAcc[test,:])
IFEda = IsolationForest(n_estimators=bestNumTreesEda)
IFEda.fit(featuresEda[train,:])
IFpredEda[test] = IFEda.decision_function(featuresEda[test,:])
# Save the scores for further analysis
#np.save('IsolationForestpredAllScores_rest',IFpredAll)
#np.save('IsolationForestpredAccScores_rest',IFpredAcc)
#np.save('IsolationForestpredEdaScores_rest',IFpredEda)
print('IF AUC ALL: %f (%s)' % (roc_auc_score(1-labels,IFpredAll),IFgsAll.best_params_))
print('IF AUC ACC: %f (%s)' % (roc_auc_score(1-labels,IFpredAcc),IFgsAcc.best_params_))
print('IF AUC EDA: %f (%s)' % (roc_auc_score(1-labels,IFpredEda),IFgsEda.best_params_))