-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRevisedVersion_MEG_Problem_OtherClassifiers_v1.py
More file actions
1194 lines (937 loc) · 45.6 KB
/
RevisedVersion_MEG_Problem_OtherClassifiers_v1.py
File metadata and controls
1194 lines (937 loc) · 45.6 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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# This file is part of EAP.
#
# EAP is free software: you can redistribute it and/or modify
# it under the terms of the GU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# EAP is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with EAP. If not, see <http://www.gnu.org/licenses/>.
def warn(*args, **kwargs):
pass
import warnings
warnings.warn = warn
import argparse
import random
import operator
import csv
import itertools
import numpy as np
#import pylab as pl
import tkinter
import matplotlib
#matplotlib.use('Qt4Agg') # interactive plotting backend
#print(matplotlib.rcParams['backend'])
import matplotlib.pylab as pl
import pylab
#import pygraphviz as pgv
from scipy.io import loadmat
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import RandomForestClassifier
from deap import algorithms
from deap import base
from deap import creator
from deap import tools
from deap import gp
from sklearn import svm, datasets
from sklearn.metrics import roc_curve, auc
from sklearn.cross_validation import StratifiedKFold
from sklearn.cross_validation import KFold
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import RidgeClassifierCV
from sklearn.linear_model import RidgeClassifier
from sklearn.linear_model import Perceptron
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA # sklearn.lda
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis as QDA # sklearn.qda
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn import linear_model
from sklearn import decomposition
from sklearn.cross_validation import train_test_split
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.neighbors import NearestCentroid
from sklearn.multiclass import OneVsRestClassifier
from scipy.io import loadmat
from sklearn.feature_selection import SelectPercentile, f_classif
from sklearn.linear_model import RandomizedLogisticRegression
from sklearn import metrics
#from iw import ImportanceWeightedClassifier
from libtlda.iw import ImportanceWeightedClassifier
SymMean = []
SymSlope = []
MatSymMean = []
MatSymSlope = []
##########################################################################################################
# FUNCTIONS THAT READ THE DATA TO BE CLASSIFIED AND THE LABELS FOR ALL SUBJECTS
##########################################################################################################
#####################################################################################################
# InitData reads a the file TrainingLabels.mat that contain the labels (0 or 1)
# for all the cases of each of the 16 subjects. This information is contained
# in the variable All_ys, where All_ys{i} is the vector containing the labels
# for the cases of subject i. E.g. All_ys{0}(1:10)' = [0,0,1,1,0,0,0,1,0,1], i.e.,
# the labels to the first 10 cases. Each subject has a different number of case.
def InitData():
data = loadmat('matlab_data/TrainingLabels.mat', squeeze_me=True)
All_ys = data['All_ys']
return All_ys
#####################################################################################################
def Read_InputData(subj,tsubj):
global SymMean
global SymSlope
global MatSymMean
global MatSymSlope
class_file = 'data/MEG_classes%d.csv' %(subj-1)
classes = np.loadtxt(class_file, delimiter=' ',unpack=True).astype(int)
ncases = classes.shape[0]
training_indices = range(0,ncases)
classes_train = classes[0:ncases]
class_file = 'data/MEG_classes%d.csv' %(tsubj-1)
classes = np.loadtxt(class_file, delimiter=' ',unpack=True).astype(int)
n_testcases = classes.shape[0]
test_indices = range(0,n_testcases)
classes_test = classes[0:n_testcases]
aux_data = loadmat('matlab_data/IndexMeanSlopeData.mat', squeeze_me=True)
meanSelVars = aux_data['MeanVarsIndex']
slopeSelVars = aux_data['SlopeVarsIndex']
#aux_data = loadmat('matlab_data/SymMeanSlope_gauss.mat', squeeze_me=True)
matlab_file = 'matlab_data/SymMeanSlope_rulif_%d.mat' %(subj)
aux_data = loadmat(matlab_file, squeeze_me=True)
SymMean = aux_data['SymMean']
SymSlope = aux_data['SymSlope']
MatSymMean = aux_data['MatSymMean']
MatSymSlope = aux_data['MatSymSlope']
# print(MatSymMean[1].shape,nsel)
# Reads the file with the features and the classes of the problem
meg_file = 'data/CMEGdataMean_%d.csv' %(subj)
auxMEGReader_mean = np.loadtxt(meg_file, delimiter=' ',usecols=meanSelVars[subj-1,:nsel]-1,unpack=True).astype(float)
meg_file = 'data/CMEGdataSlope_%d.csv' %(subj)
auxMEGReader_slope = np.loadtxt(meg_file, delimiter=' ',usecols=slopeSelVars[subj-1,:nsel]-1,unpack=True).astype(float)
MEGReader = np.hstack((auxMEGReader_mean.transpose(),auxMEGReader_slope.transpose()))
MEG_data_train = list(list(float(elem) for elem in row) for row in MEGReader[:,:])
meg_file = 'data/CMEGdataMean_%d.csv' %(tsubj)
auxMEGReader_mean = np.loadtxt(meg_file, delimiter=' ',usecols=meanSelVars[subj-1,:nsel]-1,unpack=True).astype(float)
meg_file = 'data/CMEGdataSlope_%d.csv' %(tsubj)
auxMEGReader_slope = np.loadtxt(meg_file, delimiter=' ',usecols=slopeSelVars[subj-1,:nsel]-1,unpack=True).astype(float)
MEGReader = np.hstack((auxMEGReader_mean.transpose(),auxMEGReader_slope.transpose()))
MEG_data_test = list(list(float(elem) for elem in row) for row in MEGReader[:,:])
return MEG_data_train,MEG_data_test,classes_train,classes_test
##########################################################################################################
# FUNCTIONS THAT IMPLEMENT TRADITIONAL CLASSIFIERS USING SCI-KIT LEARN
##########################################################################################################
def dist(x,y):
return np.sqrt(np.mean((x-y)**2))
def CrossValAnalysisProb(myclf,X,y):
cv = StratifiedKFold(y, n_folds=5,shuffle=True,random_state=seed) # For Python
res = y*-1.0
for i, (train, test) in enumerate(cv):
#probas_ = myclf.fit(X[train], y[train],sample_weight=Limo_sample_weight[train]).predict(X[test])
probas_ = myclf.fit(X[train], y[train]).predict_proba(X[test])
res[test] = probas_[:,1]
return res
def CrossValAnalysis(myclf,X,y):
cv = StratifiedKFold(y, n_folds=5,shuffle=True,random_state=seed) # For Python
res = y*-1.0
for i, (train, test) in enumerate(cv):
#probas_ = myclf.fit(X[train], y[train],sample_weight=Limo_sample_weight[train]).predict(X[test])
probas_ = myclf.fit(X[train], y[train]).predict(X[test])
res[test] = probas_
return res
def ErrorAnalysis(myclf,X,y):
# Run myclf with crossvalidation
cv = KFold(y.shape[0], n_folds=10)
res = np.zeros([X.shape[0]])
for i, (train, test) in enumerate(cv):
probas_ = myclf.fit(X[train], y[train]).predict(X[test])
#print probas_.shape, res.shape, train.shape, test.shape
res[test] = probas_
return res
def Weighted_InitClassifier(index):
C = 1.0
importance = range(1,10)
parameters1 = {'kernel': ['linear'], 'gamma': [0.1, 0.01, 1e-3, 1e-4],'C': [1, 10, 100, 1000]}
parameters2 = {'kernel': ['poly'], 'gamma': [0.1, 0.01, 1e-4, 1e-5], 'C': [1, 10, 100, 1000]}
parameters3 = {'kernel': ['rbf'], 'gamma': [0.1, 0.01, 1e-4, 1e-5], 'C': [1, 10, 100, 1000]}
if index==1:
clf=LogisticRegression(C=C, penalty='l1')
if index==2:
clf=LogisticRegression(C=C, penalty='l2')
if index==3:
clf=QDA()
if index==4:
clf=LDA()
if index==5:
clf=KNeighborsClassifier()
if index==6:
clf=SVC(kernel='linear', C=C, probability=True, tol=1e-3, verbose=False)
if index==7:
clf = svm.SVC(kernel='poly', degree=3, C=C, probability=True, tol=1e-4, verbose=False)
if index==8:
clf = svm.SVC(kernel='rbf', C=C, probability=True, tol=1e-4, verbose=False)
if index==9:
clf = GaussianNB()
if index==10:
clf = GradientBoostingClassifier(n_estimators=100, max_depth=11, subsample=1.0)
if index==11:
clf = RandomForestClassifier(max_depth=11, n_estimators=100)
if index==12:
clf = DecisionTreeClassifier(max_depth=None, min_samples_split=1.0,random_state=0)
if index==13:
clf = ExtraTreesClassifier(n_estimators=100,random_state=0)
#if index==55:
# clf = RandomizedLogisticRegression() # This
return clf
def GridSearch_Weighted_InitClassifier(index,sw):
C = 1.0
importance = range(1,10)
parameters1 = {'kernel': ['linear'], 'gamma': [0.1, 0.01, 1e-3, 1e-4],'C': [1, 10, 100, 1000]}
parameters2 = {'kernel': ['poly'], 'gamma': [0.1, 0.01, 1e-4, 1e-5], 'C': [1, 10, 100, 1000]}
parameters3 = {'kernel': ['rbf'], 'gamma': [0.1, 0.01, 1e-4, 1e-5], 'C': [1, 10, 100, 1000]}
grid_logistic = {"C":np.logspace(-3,3,16)}
Cs = [0.001, 0.01, 0.1, 1, 10]
gammas = [0.001, 0.01, 0.1, 1]
grid_SVMs = {'C': Cs, 'gamma' : gammas}
grid_GB = {'var_smoothing': [1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 1e-5]}
grid_gradient_boosting = {'learning_rate': [0.1, 0.05, 0.02],
'max_depth': [6, 8, 11],
'min_samples_leaf': [20, 50,100]
}
grid_RF = {
'min_samples_leaf': [3, 4, 5],
'min_samples_split': [2, 5, 10],
'n_estimators': [50, 100, 200]
}
grid_DT = {
'criterion': ['gini','entropy'],
'min_samples_leaf': [3, 4, 5],
'min_samples_split': [2, 5, 10],
}
fit_params = {'sample_weight': sw}
if index==1:
logreg = LogisticRegression(penalty='l1')
clf = GridSearchCV(logreg,grid_logistic,cv=5, fit_params=fit_params)
if index==2:
logreg = LogisticRegression(penalty='l2')
clf = GridSearchCV(logreg,grid_logistic,cv=5, fit_params=fit_params)
if index==3:
clf=QDA()
if index==4:
clf=LDA()
if index==5:
clf=KNeighborsClassifier()
if index==6:
aux_clf = SVC(kernel='linear', probability=True, tol=1e-3, verbose=False)
clf = GridSearchCV(aux_clf,grid_SVMs,cv=5, fit_params=fit_params)
if index==7:
aux_clf = SVC(kernel='poly', degree=3, probability=True, tol=1e-4, verbose=False)
clf = GridSearchCV(aux_clf,grid_SVMs,cv=5, fit_params=fit_params)
if index==8:
aux_clf = SVC(kernel='rbf', probability=True, tol=1e-4, verbose=False)
clf = GridSearchCV(aux_clf,grid_SVMs,cv=5, fit_params=fit_params)
if index==9:
clf = GaussianNB()
if index==10:
aux_clf = GradientBoostingClassifier(n_estimators=100, subsample=1.0)
clf = GridSearchCV(aux_clf,grid_gradient_boosting,cv=5, fit_params=fit_params)
if index==11:
aux_clf = RandomForestClassifier()
clf = GridSearchCV(aux_clf,grid_RF,cv=5, fit_params=fit_params)
if index==12:
aux_clf = DecisionTreeClassifier(max_depth=None,random_state=0)
clf = GridSearchCV(aux_clf,grid_DT,cv=5, fit_params=fit_params)
if index==13:
aux_clf = ExtraTreesClassifier(random_state=0)
clf = GridSearchCV(aux_clf,grid_DT,cv=5, fit_params=fit_params)
#if index==55:
# clf = RandomizedLogisticRegression() # This
return clf
def InitClassifier(index):
C = 1.0
importance = range(1,10)
parameters1 = {'kernel': ['linear'], 'gamma': [0.1, 0.01, 1e-3, 1e-4],'C': [1, 10, 100, 1000]}
parameters2 = {'kernel': ['poly'], 'gamma': [0.1, 0.01, 1e-4, 1e-5], 'C': [1, 10, 100, 1000]}
parameters3 = {'kernel': ['rbf'], 'gamma': [0.1, 0.01, 1e-4, 1e-5], 'C': [1, 10, 100, 1000]}
if index==1:
clf=LogisticRegression(C=C, penalty='l1')
if index==2:
clf=LogisticRegression(C=C, penalty='l2')
if index==3:
clf=QDA()
if index==4:
clf=LDA()
if index==5:
clf=KNeighborsClassifier()
if index==6:
clf=SVC(kernel='linear', C=C, probability=True, tol=1e-3, verbose=False)
if index==7:
clf = svm.SVC(kernel='poly', degree=3, C=C, probability=True, tol=1e-4, verbose=False)
if index==8:
clf = svm.SVC(kernel='rbf', C=C, probability=True, tol=1e-4, verbose=False)
if index==9:
clf = GaussianNB()
if index==10:
clf = GradientBoostingClassifier(n_estimators=100, max_depth=11, subsample=1.0)
if index==11:
clf = RandomForestClassifier(max_depth=11, n_estimators=100)
if index==12:
clf = DecisionTreeClassifier(max_depth=None, min_samples_split=1.0,random_state=0)
if index==13:
clf = ExtraTreesClassifier(n_estimators=100,random_state=0)
if index==14:
clf = DecisionTreeClassifier(max_depth=5)
if index==15:
clf = RandomForestClassifier(max_depth=5, n_estimators=500, max_features=1)
if index==16:
clf = GradientBoostingClassifier(n_estimators=500, max_depth=11)
if index==17:
clf = linear_model.SGDClassifier(loss='log')
if index==18:
clf=KNeighborsClassifier(n_neighbors=5)
if index==19:
clf = GradientBoostingClassifier(n_estimators=500, max_depth=3)
if index==20:
clf = GradientBoostingClassifier(n_estimators=500, max_depth=5)
if index==21:
clf = RandomForestClassifier(max_depth=11, n_estimators=500)
if index==22:
clf = RandomForestClassifier(max_depth=13, n_estimators=700)
if index==23:
clf = GradientBoostingClassifier(n_estimators=100, max_depth=11) # 1000 for 23,24,25,26
if index==24:
clf = GradientBoostingClassifier(n_estimators=100, max_depth=12)
if index==25:
clf = GradientBoostingClassifier(n_estimators=100, max_depth=13)
if index==26:
clf = GradientBoostingClassifier(n_estimators=100, max_depth=14)
if index==27:
clf = linear_model.SGDClassifier(loss='log', penalty='l1')
if index==28:
clf = linear_model.SGDClassifier(loss='log', penalty='l2' )
if index==29:
clf = linear_model.SGDClassifier(loss='modified_huber',penalty='l1')
if index==30:
clf = linear_model.SGDClassifier(loss='modified_huber',penalty='l2')
if index==31:
clf = Perceptron()
if index==32:
clf = GridSearchCV(SVC(kernel='linear',verbose=False),parameters1,verbose=False)
if index==33:
clf = linear_model.SGDClassifier(loss='hinge')
if index==34:
clf = NearestCentroid()
if index==35:
clf = RandomForestClassifier(max_depth=5, n_estimators=50)
if index==36:
clf = GridSearchCV(SVC(kernel='poly',verbose=False),parameters2,verbose=False)
if index==37:
clf = GridSearchCV(SVC(kernel='rbf',verbose=False),parameters3,verbose=False)
if index==38:
clf = NearestCentroid(metric='l1')
if index==39:
clf = NearestCentroid(metric='l2')
if index==40:
clf = NearestCentroid(metric='correlation')
if index==41:
clf = NearestCentroid(metric='cosine')
if index==42:
clf = NearestCentroid(metric='seuclidean')
if index==43:
clf = GradientBoostingClassifier(n_estimators=25, max_depth=5, subsample=1.0) # This
if index==44:
clf = GradientBoostingClassifier(n_estimators=30, max_depth=5, subsample=1.0)
if index==45:
clf = GradientBoostingClassifier(n_estimators=40, max_depth=5, subsample=1.0)
if index==46:
clf = RandomForestClassifier(max_depth=7, n_estimators=80)
if index==47:
clf = RandomForestClassifier(max_depth=7, n_estimators=100)
if index==48:
clf = RandomForestClassifier(max_depth=7, n_estimators=120)
if index==49:
clf = RandomForestClassifier(max_depth=9, n_estimators=150) # This
if index==50:
clf = RandomForestClassifier(max_depth=5, n_estimators=10)
if index==51:
clf = RandomForestClassifier(max_depth=5, n_estimators=15)
if index==52:
clf = RandomForestClassifier(max_depth=9, n_estimators=10)
if index==53:
clf = RandomForestClassifier(max_depth=11, n_estimators=10) # This
if index==54:
clf = RandomForestClassifier(max_depth=11, n_estimators=15) # This
if index==55:
clf = DecisionTreeClassifier(max_depth=7)
if index==56:
clf = DecisionTreeClassifier(max_depth=10)
if index==57:
clf = DecisionTreeClassifier(max_depth=15)
if index==58:
clf = DecisionTreeClassifier(max_depth=20)
#if index==55:
# clf = RandomizedLogisticRegression() # This
return clf
#####################################################################################################
def InitTransferClassifier(index,l,iw):
if index==1:
clf = ImportanceWeightedClassifier(loss=l,iwe=iw)
if index==2:
clf = TransferComponentClassifier()
if index==3:
clf=SubspaceAlignedClassifier()
if index==4:
clf=StructuralCorrespondenceClassifier()
if index==5:
clf=RobustBiasAwareClassifier()
if index==6:
clf=FeatureLevelDomainAdaptiveClassifier()
if index==7:
clf=TargetContrastivePessimisticClassifier()
return clf
#####################################################################################################
def Pop_Effic_evalMEG_All(pop,psize):
all_results = np.zeros((psize,16))
# Evaluate the sum of correctly identified cases in the data set
TrainV = [ [] for j in range(16) ]
aux_data = loadmat('../matlab_data/IndexMeanSlopeData.mat', squeeze_me=True)
meanSelVars = aux_data['MeanVarsIndex']
slopeSelVars = aux_data['SlopeVarsIndex']
for j in range(16):
if (j+1)!=subject:
meg_file = 'CMEGdataMean_%d.csv' %(j+1)
auxMEGReader_mean = np.loadtxt(meg_file, delimiter=' ',usecols=meanSelVars[subject-1,:nsel]-1,unpack=True).astype(float)
meg_file = 'CMEGdataSlope_%d.csv' %(j+1)
auxMEGReader_slope = np.loadtxt(meg_file, delimiter=' ',usecols=slopeSelVars[subject-1,:nsel]-1,unpack=True).astype(float)
X = np.hstack((auxMEGReader_mean.transpose(),auxMEGReader_slope.transpose()))
y = All_ys[j]
else:
X = MEG_data_test
y = classes_test
ncases = len(y)
for l in range(psize):
individual = pop[l]
# Transform the tree expression in a callable function
func = toolbox.compile(expr=individual)
result = 0
for i,cases in enumerate(X):
aux = func(*cases)
tot = (aux==bool(y[i]))
result = result + tot
all_results[l,j] = result/ncases
# print(l,j, result/ncases)
return all_results
#####################################################################################################
# Compute_Frequencies computes the frequencies of each non-terminal in
# the tree program
def Compute_Frequencies(individual):
# Transform the tree expression in a callable function
auxp = str(individual)
prog_list = auxp.split("IN")
Frequencies = np.zeros((2*nsel))
#print(prog_list)
for k in range(0,len(prog_list)):
try:
x = int(prog_list[k][:3])
except ValueError:
try:
x = int(prog_list[k][:2])
except ValueError:
try:
x = int(prog_list[k][:1])
except ValueError:
x = -1
#print(k,x)
if x>-1:
Frequencies[x] = Frequencies[x] + 1
return Frequencies
#####################################################################################################
# is the bi-objective function used by the transfer GP algorithm
# It evaluates the accuracy of the genetic program that serves as a classifier and similarity
# between the terminals used by source and target
def eval_NormalAcc_VarSim(individual):
# Transform the tree expression in a callable function
MatMean = np.asarray(MatSymMean[target_subj-1])
MatSlope = np.asarray(MatSymSlope[target_subj-1])
Frequencies = Compute_Frequencies(individual)
func = toolbox.compile(expr=individual)
# Evaluate the sum of correctly identified cases in the data set
result = 0
ncases = len(classes_train)
# If there is not at least one not-terminal the tree is penalized
posfreq_mean = np.asarray(np.where(Frequencies[:nsel]>0)[0]);
posfreq_slope = np.asarray(np.where(Frequencies[nsel:]>0)[0]);
#print(Frequencies,posfreq_mean.shape[0],nsel)
if posfreq_mean.shape[0]> 0 and posfreq_slope.shape[0]>0:
for i,cases in enumerate(MEG_data_train):
aux = func(*cases)
tot = (aux==bool(classes_train[i]))*1.0
result = result + tot
#print(i,aux, classes_train[i],tot,result)
else:
result = 0
# We measure the si
t_tot = 0;
for i in range(nsel):
if Frequencies[i]>0:
t_tot = t_tot+SymMean[target_subj-1,i]
if Frequencies[i+nsel]>0:
t_tot = t_tot+SymSlope[target_subj-1,i]
return (-1*t_tot,result/ncases)
#####################################################################################################
def eval_NormalAcc_BiasAcc(individual):
# Transform the tree expression in a callable function
MatMean = np.asarray(MatSymMean[target_subj-1])
MatSlope = np.asarray(MatSymSlope[target_subj-1])
Frequencies = Compute_Frequencies(individual)
func = toolbox.compile(expr=individual)
# Evaluate the sum of correctly identified cases in the data set
result = 0
bias_result = 0
ncases = len(classes_train)
posfreq_mean = np.asarray(np.where(Frequencies[:nsel]>0)[0]);
posfreq_slope = np.asarray(np.where(Frequencies[nsel:]>0)[0]);
if posfreq_mean.shape[0]> 0 and posfreq_slope.shape[0]>0:
for i,cases in enumerate(MEG_data_train):
aux = func(*cases)
tot = (aux==bool(classes_train[i]))
importance_mean = np.mean(MatSymMean[target_subj-1][posfreq_mean,i])
importance_slope = np.mean(MatSymSlope[target_subj-1][posfreq_slope,i])
importance = np.mean([importance_mean,importance_slope])
result = result + tot
bias_result = bias_result + tot * importance
else:
result = 0
bias_result = 0
return (bias_result/ncases,result/ncases)
#####################################################################################################
def eval_BiasAcc_VarSim(individual):
# Transform the tree expression in a callable function
MatMean = np.asarray(MatSymMean[target_subj-1])
MatSlope = np.asarray(MatSymSlope[target_subj-1])
Frequencies = Compute_Frequencies(individual)
func = toolbox.compile(expr=individual)
# Evaluate the sum of correctly identified cases in the data set
bias_result = 0
ncases = len(classes_train)
posfreq_mean = np.asarray(np.where(Frequencies[:nsel]>0)[0]);
posfreq_slope = np.asarray(np.where(Frequencies[nsel:]>0)[0]);
if posfreq_mean.shape[0]> 0 and posfreq_slope.shape[0]>0:
for i,cases in enumerate(MEG_data_train):
aux = func(*cases)
tot = (aux==bool(classes_train[i]))
importance_mean = np.mean(MatSymMean[target_subj-1][posfreq_mean,i])
importance_slope = np.mean(MatSymSlope[target_subj-1][posfreq_slope,i])
importance = np.mean([importance_mean,importance_slope])
#print(i,posfreq_mean,posfreq_slope,importance)
bias_result = bias_result + tot * importance
else:
bias_result = 0
t_tot = 0;
n_cas = 1
for i in range(nsel):
if Frequencies[i]>0:
t_tot = t_tot+SymMean[target_subj-1,i]
if Frequencies[i+nsel]>0:
t_tot = t_tot+SymSlope[target_subj-1,i]
return (-1*t_tot,bias_result/ncases)
#####################################################################################################
# eval_Acc_Logistics is a bi-objective function used by the transfer GP algorithm
# One objectives evaluates the accuracy of the genetic program that serves as a classifier
# The second objectives evaluates how good is the set of terminals included in the genetic program
# to discriminate between samples in the train set (source) and the test set (target).
# The discrimination capacity of the features is evaluated using a logistic regression classifier
# to distinguish between source and target
def eval_Acc_LogisticRegression(individual):
# Transform the tree expression in a callable function
MatMean = np.asarray(MatSymMean[target_subj-1])
MatSlope = np.asarray(MatSymSlope[target_subj-1])
Frequencies = Compute_Frequencies(individual)
# Evaluate the sum of correctly identified cases in the data set
result = 0
ncases = len(classes_train)
# If there is not at least one not-terminal the tree is penalized
posfreq_mean = np.asarray(np.where(Frequencies[:nsel]>0)[0]);
posfreq_slope = np.asarray(np.where(Frequencies[nsel:]>0)[0]);
#print(posfreq_mean.shape[0],posfreq_slope.shape[0])
if posfreq_mean.shape[0]> 0 and posfreq_slope.shape[0]>0:
func = toolbox.compile(expr=individual)
for i,cases in enumerate(MEG_data_train):
aux = func(*cases)
tot = (aux==bool(classes_train[i]))*1.0
result = result + tot
#print(i,aux, classes_train[i],tot,result)
else:
result = 0
LR_accuracy = 150
return (-LR_accuracy,result/ncases)
# Here the logistic classifier is used to measure the similarity
if posfreq_mean.shape[0]> 0 and posfreq_slope.shape[0]>0:
joint_features = np.hstack((posfreq_mean,nsel+posfreq_slope))
#print(joint_features)
AuxTrain = np.asarray(MEG_data_train)
AuxTest = np.asarray(MEG_data_test)
LR_training_data = np.vstack((AuxTrain[:,joint_features],AuxTest[:,joint_features[:]]))
LR_data_labels = np.vstack((np.ones((AuxTrain.shape[0],1)),np.zeros((AuxTest.shape[0],1)) ))
if type_class==0:
clf = GaussianNB()
elif type_class==1:
clf = LogisticRegression(C=1.0, penalty='l2', class_weight='balanced')
elif type_class==2:
clf = RandomForestClassifier(max_depth=5, n_estimators=20)
clf.fit(LR_training_data,np.ravel(LR_data_labels)) # LR Learning
aux_ys = clf.predict(LR_training_data) # LR Prediction
#print(aux_ys.shape,LR_data_labels.shape)
#acc = np.random.randint(100)
acc = 0.0
for j in range(LR_data_labels.shape[0]):
#print(j,LR_data_labels.shape[0],aux_ys[j],LR_data_labels[j],np.abs(aux_ys[j]-LR_data_labels[j]),acc)
acc = acc + int(1.0-np.abs(aux_ys[j]-LR_data_labels[j]))
#acc = int(acc)
LR_accuracy = (100.0*acc)/LR_data_labels.shape[0]
#print(acc,LR_data_labels.shape[0],LR_accuracy,result/ncases)
#print(acc,LR_accuracy)
return (-LR_accuracy,result/ncases)
#####################################################################################################
# eval_BiasAcc_Logistics is a bi-objective function used by the transfer GP algorithm
# One objectives evaluates the accuracy of the genetic program that serves as a classifier
# the classifier is biased using information computed a priori
# The second objectives evaluates how good is the set of terminals included in the genetic program
# to discriminate between samples in the train set (source) and the test set (target).
# The discrimination capacity of the features is evaluated using a classifier
# to distinguish between source and target
def eval_BiasAcc_LogisticRegression(individual):
# Transform the tree expression in a callable function
MatMean = np.asarray(MatSymMean[target_subj-1])
MatSlope = np.asarray(MatSymSlope[target_subj-1])
Frequencies = Compute_Frequencies(individual)
# Evaluate the sum of correctly identified cases in the data set
bias_result = 0
ncases = len(classes_train)
posfreq_mean = np.asarray(np.where(Frequencies[:nsel]>0)[0]);
posfreq_slope = np.asarray(np.where(Frequencies[nsel:]>0)[0]);
if posfreq_mean.shape[0]> 0 and posfreq_slope.shape[0]>0:
func = toolbox.compile(expr=individual)
for i,cases in enumerate(MEG_data_train):
aux = func(*cases)
tot = (aux==bool(classes_train[i]))
importance_mean = np.mean(MatSymMean[target_subj-1][posfreq_mean,i])
importance_slope = np.mean(MatSymSlope[target_subj-1][posfreq_slope,i])
importance = np.mean([importance_mean,importance_slope])
bias_result = bias_result + tot * importance
else:
bias_result = 0
LR_accuracy = 150
return (-LR_accuracy,bias_result/ncases)
# Here the logistic classifier is used to measure the similarity
if posfreq_mean.shape[0]> 0 and posfreq_slope.shape[0]>0:
joint_features = np.hstack((posfreq_mean,nsel+posfreq_slope))
#print(joint_features)
AuxTrain = np.asarray(MEG_data_train)
AuxTest = np.asarray(MEG_data_test)
LR_training_data = np.vstack((AuxTrain[:,joint_features],AuxTest[:,joint_features[:]]))
LR_data_labels = np.vstack((np.ones((AuxTrain.shape[0],1)),np.zeros((AuxTest.shape[0],1)) ))
if type_class==0:
clf = GaussianNB()
elif type_class==1:
clf = LogisticRegression(C=1.0, penalty='l2', class_weight='balanced')
elif type_class==2:
clf = RandomForestClassifier(max_depth=5, n_estimators=20)
clf.fit(LR_training_data,np.ravel(LR_data_labels)) # LR Learning
aux_ys = clf.predict(LR_training_data) # LR Prediction
acc = 0.0
for j in range(LR_data_labels.shape[0]):
acc = acc + int(1.0-np.abs(aux_ys[j]-LR_data_labels[j]))
LR_accuracy = (100.0*acc)/LR_data_labels.shape[0]
return (-LR_accuracy,bias_result/ncases)
#####################################################################################################
# eval_Biv_Transfer_Freq is the bi-objective function used by the transfer GP algorithm
# It evaluates the accuracy of the genetic program that serves as a classifier and similarity
# between the terminals used by source and target including the frequency of these terminals
# as a weight. Those terminals that are more frequent gets a higher weight in the function
def eval_Biv_Transfer_Freq(individual):
# Transform the tree expression in a callable function
auxp = str(individual)
prog_list = auxp.split("IN")
Frequencies = np.zeros((120))
for k in range(0,len(prog_list)):
try:
x = int(prog_list[k][:3])
except ValueError:
try:
x = int(prog_list[k][:2])
except ValueError:
try:
x = int(prog_list[k][:1])
except ValueError:
x = -1
#print(k,x)
if x>-1:
Frequencies[x] = Frequencies[x] + 1
func = toolbox.compile(expr=individual)
# Evaluate the sum of correctly identified cases in the data set
result = 0
ncases = len(classes_train)
for i,cases in enumerate(MEG_data_train):
aux = func(*cases)
tot = (aux==bool(classes_train[i]))
result = result + tot
t_tot = 0;
n_cas = 1
for i in range(nsel):
if Frequencies[i]>0:
t_tot = t_tot+SymMean[subject-1][target_subj-1,i]*Frequencies[i]
#n_cas = n_cas + Frequencies[i]
if Frequencies[i+nsel]>0:
t_tot = t_tot+SymSlope[subject-1][target_subj-1,i]*Frequencies[i+nsel]
#n_cas = n_cas + Frequencies[i+nsel]
#print (i,sum(Frequencies),t_tot,t_tot/(sum(Frequencies)+1))
return (t_tot,result/ncases)
#return (t_tot/n_cas,result/ncases)
##################################################################################################
# DEFINITION AND IMPLEMENTATION OF THE GP PROGRAMS
##################################################################################################
def GP_Definitions(nfeatures):
# defined a new primitive set for strongly typed GP
pset = gp.PrimitiveSetTyped("MAIN", itertools.repeat(float, nfeatures), bool, "IN")
# boolean operators
pset.addPrimitive(operator.and_, [bool, bool], bool)
pset.addPrimitive(operator.or_, [bool, bool], bool)
pset.addPrimitive(operator.not_, [bool], bool)
# floating point operators
# Define a safe division function
def safeDiv(left, right):
try: return left / right
except ZeroDivisionError: return 0
pset.addPrimitive(operator.add, [float,float], float)
pset.addPrimitive(operator.sub, [float,float], float)
pset.addPrimitive(operator.mul, [float,float], float)
pset.addPrimitive(safeDiv, [float,float], float)
# logic operators
# Define a new if-then-else function
def if_then_else(input, output1, output2):
if input: return output1
else: return output2
pset.addPrimitive(operator.lt, [float, float], bool)
pset.addPrimitive(operator.eq, [float, float], bool)
pset.addPrimitive(if_then_else, [bool, float, float], float)
# terminals
pset.addEphemeralConstant("rand100", lambda: random.random() * 100, float)
pset.addTerminal(False, bool)
pset.addTerminal(True, bool)
return pset
#####################################################################################################
# Initialization of the Multi-Objective GP
def Init_GP_MOP(subj):
nfeat = number_features
pset = GP_Definitions(nfeat)
maxDepthLimit = 10
creator.create("FitnessMax", base.Fitness, weights=(1.0,1.0))
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register("expr", gp.genHalfAndHalf, pset=pset, type_=pset.ret, min_=1, max_=2) # IT MIGHT BE A BUG WITH THIS
#toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=1, max_=2)
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("compile", gp.compile, pset=pset)
#toolbox.register("evaluate", eval_Biv_Transfer_Freq)
toolbox.register("evaluate",bi_objective_functions[type_function])
#toolbox.register("select", tools.selTournament, tournsize=3)
toolbox.register("select", tools.selNSGA2)
toolbox.register("mate", gp.cxOnePoint)
toolbox.register("expr_mut", gp.genFull, min_=0, max_=2)
toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr_mut, pset=pset)
toolbox.decorate('mutate',gp.staticLimit(key=operator.attrgetter('height'),max_value=maxDepthLimit))
toolbox.decorate('mate',gp.staticLimit(key=operator.attrgetter('height'),max_value=maxDepthLimit))
return toolbox
#####################################################################################################
# Application of the multi-objective GP
def Apply_GP_MOP(toolbox,pop_size,gen_number,therun):
pop = toolbox.population(n=pop_size)
hof = tools.HallOfFame(pop_size)
stats = tools.Statistics(lambda ind: ind.fitness.values)
#stats = tools.Statistics()
stats.register("avg", np.mean, axis=0)
stats.register("std", np.std, axis=0)
stats.register("min", np.min, axis=0)
stats.register("max", np.max, axis=0)
#res, logbook = algorithms.eaSimple(pop, toolbox, 0.5, 0.2, gen_number, stats, halloffame=hof,verbose=1)
res, logbook = algorithms.eaMuPlusLambda(pop, toolbox, mu=pop_size,
lambda_=pop_size,
cxpb=1-0.1,
mutpb=0.1,
stats=stats,
halloffame=hof,
ngen=gen_number,
verbose=0)
return res, logbook, hof
##################################################################################################