forked from DeepSleepUCDenver/sleep_models
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_test.py
More file actions
191 lines (158 loc) · 5.46 KB
/
model_test.py
File metadata and controls
191 lines (158 loc) · 5.46 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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
from sklearn.metrics import multilabel_confusion_matrix
from sklearn.preprocessing import scale, normalize
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
from sklearn.semi_supervised import label_propagation
from sklearn.semi_supervised import LabelSpreading
from mlxtend.feature_selection import SequentialFeatureSelector as sfs
from mlxtend.plotting import plot_sequential_feature_selection as plot_sfs
from imblearn.over_sampling import SMOTE
from oversample import load_all_data
x_tr, y_tr, x_te, y_te, x_va, y_va = load_all_data()
from sklearn import svm
svm_linear = svm.SVC(kernel='linear')
svm_linear.fit(x_tr, y_tr)
svm_linear.score(x_te, y_te)
svm_linear.score(x_va, y_va)
svm_poly = svm.SVC(kernel='poly')
svm_poly.fit(x_tr, y_tr)
svm_poly.score(x_te, y_te)
svm_poly.score(x_va, y_va)
svm_rbf = svm.SVC(kernel='rbf')
svm_rbf.fit(x_tr, y_tr)
svm_rbf.score(x_te, y_te)
svm_rbf.score(x_va, y_va)
svm_sigmoid = svm.SVC(kernel='sigmoid')
svm_sigmoid.fit(x_tr, y_tr)
svm_sigmoid.score(x_te, y_te)
svm_sigmoid.score(x_va, y_va)
from sklearn.ensemble import RandomForestClassifier
rdf = RandomForestClassifier(max_depth=4, random_state=0)
rdf.fit(x_tr, y_tr)
rdf.score(x_te, y_te)
rdf.score(x_va, y_va)
from sklearn.ensemble import AdaBoostClassifier
adb = AdaBoostClassifier(random_state=0)
adb.fit(x_tr, y_tr)
adb.score(x_te, y_te)
adb.score(x_va, y_va)
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier()
knn.fit(x_tr, y_tr)
knn.score(x_te, y_te)
knn.score(x_va, y_va)
knn.predict(x_va)
from sklearn.naive_bayes import GaussianNB
bay = GaussianNB()
bay.fit(x_tr, y_tr)
bay.score(x_te, y_te)
bay.score(x_va, y_va)
from sklearn.ensemble import RandomForestClassifier
rdf = RandomForestClassifier(max_depth=4, random_state=0)
rdf.fit(x_tr, y_tr)
rdf.score(x_te, y_te)
rdf.score(x_va, y_va)
# example of semi-supervised gan for mnist
from numpy import expand_dims
from numpy import zeros
from numpy import ones
from numpy import asarray
from numpy.random import randn
from numpy.random import randint
from keras.optimizers import Adam
from keras.models import Model
from keras.layers import Input
from keras.layers import Dense
from keras.layers import Reshape
from keras.layers import Flatten
from keras.layers import Conv1D
# from keras.layers import Conv1D
from keras.layers import LeakyReLU
from keras.layers import Dropout
from keras.layers import Lambda
from keras.layers import LSTM
from keras.layers import GRU
from keras.layers import TimeDistributed
from keras.layers import Activation
from matplotlib import pyplot
from keras import backend
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
from sklearn.metrics import multilabel_confusion_matrix
from sklearn.preprocessing import scale, normalize
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
from sklearn import svm
from sklearn.semi_supervised import label_propagation
from sklearn.semi_supervised import LabelSpreading
from mlxtend.feature_selection import SequentialFeatureSelector as sfs
from mlxtend.plotting import plot_sequential_feature_selection as plot_sfs
import matplotlib.pyplot as plt
from sklearn.metrics import plot_confusion_matrix
def edu(x):
r = []
for i in x:
m = 0
for j, v in enumerate(i):
if v > m :
m = v
c = j
r.append(c)
return r
# define the standalone supervised and unsupervised discriminator models
start = Input(shape=(22,))
fe = Dense(21)(start)
fe = Dense(15)(fe)
fe = Dense(10)(fe)
fe = Dense(5)(fe)
c_out_layer = Activation('sigmoid')(fe)
# define and compile supervised discriminator model
c_model = Model(start, c_out_layer)
c_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
#c_model.compile(loss='sparce_binary_crossentropy', optimizer='adam', metrics=['accuracy'])
c_model.summary()
x_tr, y_tr, x_te, y_te, x_va, y_va = load_all_data()
y_tr = pd.get_dummies(y_tr).values
y_te = pd.get_dummies(y_te).values
y_va = pd.get_dummies(y_va).values
c_model.fit(x_tr, y_tr)
hy_te = c_model.predict(x_te)
hy_va = c_model.predict(x_va)
accuracy_score(edu(hy_te), edu(y_te))
accuracy_score(edu(hy_va), edu(y_va))
# define the standalone supervised and unsupervised discriminator models
start = Input(shape=(22,))
fe = Dense(22)(start)
fe = Dense(44)(fe)
fe = Dense(20)(fe)
fe = Dense(15)(fe)
fe = Dense(10)(fe)
fe = Dense(8)(fe)
fe = Dense(5)(fe)
c_out_layer = Activation('LeakyReLU')(fe)
# define and compile supervised discriminator model
c_model = Model(start, c_out_layer)
c_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
#c_model.compile(loss='sparce_binary_crossentropy', optimizer='adam', metrics=['accuracy'])
c_model.summary()
x_tr, y_tr, x_te, y_te, x_va, y_va = load_all_data()
y_tr = pd.get_dummies(y_tr).values
y_te = pd.get_dummies(y_te).values
y_va = pd.get_dummies(y_va).values
c_model.fit(x_tr, y_tr)
hy_te = c_model.predict(x_te)
hy_va = c_model.predict(x_va)
accuracy_score(edu(hy_te), edu(y_te))
accuracy_score(edu(hy_va), edu(y_va))
disp = plot_confusion_matrix(c_model, x_te, y_te,
cmap=plt.cm.Blues
,normalize='true')
disp.ax_.set_title("RBF Kernel with " + str(n_features) + " best features")
cfm = disp.plot()
cfm.figure_.savefig("gru1.png")