-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfinal_model_6_AUC_acc_stdev.py
More file actions
232 lines (198 loc) · 8.94 KB
/
final_model_6_AUC_acc_stdev.py
File metadata and controls
232 lines (198 loc) · 8.94 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
# Multi label classification deep learning model voor RNAseq cancer data voor Machine learning project
# Volgens https://machinelearningmastery.com/multi-class-classification-tutorial-keras-deep-learning-library/
# - hoeveel layers: hoeveelheid RNA expressed is input, daarna wordt dit o.a. (deels) omgezet in eiwitten die vervolgens dingen
# doen voor een cel (hier een groep cellen) wat iets zou kunnen zeggen over welk type kanker. Dus ca. 2 layers (3 in die tutorial, want daar is output ook een dense layer).
#Parameter BRCA COAD KIPAN
#Activation function {Rectifier, Tanh, Maxout}
#Number of hidden layers {2, 3, 4}
#Number of units per layer [10, 200]
#L1 regularization [0.001, 0.1]
#L2 regularization [0.001, 0.1]
#Input dropout ratio [0.001, 0.1]
#Hidden dropout ratios [0.001, 0.1]
# import packages
import pandas as pandas
import numpy as np
import imblearn
from numpy import mean
from numpy import std
from numpy import ravel
from matplotlib import pyplot as plt
np.random.seed(123) # for reproducibility
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from tensorflow.keras.utils import to_categorical
from keras.utils import np_utils
from sklearn.model_selection import LeaveOneOut
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import LabelEncoder
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split
from sklearn.utils.class_weight import compute_class_weight
from imblearn.pipeline import Pipeline
from imblearn.over_sampling import SMOTE
from imblearn.under_sampling import RandomUnderSampler
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
from sklearn.decomposition import PCA
from sklearn.feature_selection import RFE
from sklearn.svm import SVR
from keras.optimizers import Adam
from sklearn.model_selection import LeavePOut
from sklearn.metrics import roc_curve
from sklearn.metrics import auc
from sklearn.preprocessing import label_binarize
from numpy import interp
from sklearn.metrics import roc_auc_score
from itertools import cycle
#define the model
def create_model(hidden_layers=1,activation='relu',neurons=1,learning_rate=0.001):
# create model
model = Sequential()
model.add(Dense(neurons, input_dim=200, activation=activation))
for i in range(hidden_layers):
model.add(Dense(neurons,activation=activation))
model.add(Dense(5,activation="softmax"))
# Compile model
optimizer = Adam(learning_rate=learning_rate)
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
return model
def apply_pca(data, nr_of_pc):
if nr_of_pc == 0:
pca = PCA(.95) # minimal number of components to explain 95% of the variance
else: pca = PCA(nr_of_pc)
pca.fit(data)
#nr_of_pc = pca.n_components_
result = pca.transform(data)
return result
def plot_roc_curve(fpr,tpr,roc_auc):
lw = 2
# Plot all ROC curves
plt.figure(1)
plt.plot(fpr["micro"], tpr["micro"],
label='micro-average ROC curve (area = {0:0.2f})'
''.format(roc_auc["micro"]),
color='deeppink', linestyle=':', linewidth=4)
plt.plot(fpr["macro"], tpr["macro"],
label='macro-average ROC curve (area = {0:0.2f})'
''.format(roc_auc["macro"]),
color='navy', linestyle=':', linewidth=4)
colors = cycle(['aqua', 'darkorange', 'cornflowerblue'])
u = ""
for i, color in zip(range(n_classes), colors):
plt.plot(fpr[i], tpr[i], color=color, lw=lw,
label='ROC curve of class {0} (area = {1:0.2f})'
''.format(i, roc_auc[i]))
plt.plot([0, 1], [0, 1], 'k--', lw=lw)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic plot')
plt.legend(loc="lower right")
plt.show()
def RFE_SVM(data, labels, n):
# returns a list of the n most important features
selected_features = []
model = SVR(kernel='linear')
rfe = RFE(model, n_features_to_select=n, step=100, verbose=0)
rfe.fit(data, labels)
for i in range(data.shape[1]):
if rfe.ranking_[i] == True:
selected_features.append(data.columns.values[i])
return selected_features
# load data
data = pandas.read_csv("data.csv", index_col= 0) # default header = True
labels = pandas.read_csv("one_hot-labels.csv", sep = ";", index_col= 0) # default header = True
# Make data compatible for converting to tensors
data_as_array = np.asarray(data).astype('float32')
labels_as_array = np.asarray(labels).astype('float32')
# Perform PCA
nr_of_pc = 200
data_as_array = apply_pca(data_as_array, nr_of_pc)
# Split 5 time the data into a test and training set for outer CV
cv_outer = KFold(n_splits=5, shuffle=True)
outer_results = list()
outer_parameters = list()
results_dict = {}
for train_ix, test_ix in cv_outer.split(data_as_array):
# split data
X_train, X_test = data_as_array[train_ix, :], data_as_array[test_ix, :]
y_train, y_test = labels_as_array[train_ix], labels_as_array[test_ix]
# Balance data set volgens http://glemaitre.github.io/imbalanced-learn/generated/imblearn.over_sampling.RandomOverSampler.html
# oversample samples < average
no_samples = np.count_nonzero(y_train, axis=0)
average_samples = int(mean(no_samples))
weights = []
for i in range(len(no_samples)):
if no_samples[i] < average_samples:
weights.append(average_samples)
else:
weights.append(no_samples[i])
ratio_over = {0: weights[0], 1: weights[1], 2: weights[2], 3: weights[3], 4: weights[4]}
over = SMOTE(sampling_strategy=ratio_over, random_state=314)
X_train, y_train = over.fit_resample(X_train, y_train)
# undersample samples > average
ratio_under = {0: average_samples, 1: average_samples, 2: average_samples, 3: average_samples, 4: average_samples}
under = RandomUnderSampler(sampling_strategy=ratio_under, random_state=314)
X_train, y_train = under.fit_resample(X_train, y_train)
cv_inner = KFold(n_splits=5, shuffle=True)
model = KerasClassifier(build_fn=create_model, batch_size=32, epochs=100, verbose=0)
learning_rate = [0.001, 0.01, 0.1]
batch_size = [8,16,32]
neurons = [50, 100, 150]
hidden_layers = [1,2,3]
epochs = [10, 30, 50]
activation = ['relu','tanh','sigmoid']
param_grid = dict(learning_rate=learning_rate,epochs=epochs,batch_size=batch_size,neurons=neurons,hidden_layers=hidden_layers, activation=activation)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-2, cv=cv_inner, verbose=1)
resultgridsearch = grid.fit(X_train,y_train)
grid_score = resultgridsearch.cv_results_['mean_test_score']
params = resultgridsearch.cv_results_['params']
for score,param in zip(grid_score,params):
results_dict[score] = param
sorted_acc = sorted(results_dict.keys(), reverse = True)
for acc in sorted_acc:
final_model_params = results_dict[acc]
break
final_model = create_model(hidden_layers = final_model_params["hidden_layers"], activation= final_model_params["activation"],
neurons = final_model_params["neurons"], learning_rate = final_model_params["learning_rate"])
final_model.fit(X_train,y_train,epochs=final_model_params["epochs"],batch_size=final_model_params["batch_size"])
n_classes = 5
y_score = final_model.predict_proba(X_test,batch_size=final_model_params["batch_size"])
print('y_score =', y_score)
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(n_classes):
fpr[i], tpr[i], _ = roc_curve(y_test[:, i], y_score[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
# Compute micro-average ROC curve and ROC area
fpr["micro"], tpr["micro"], _ = roc_curve(y_test.ravel(), y_score.ravel())
roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])
# Compute macro-average ROC curve and ROC area
# First aggregate all false positive rates
all_fpr = np.unique(np.concatenate([fpr[i] for i in range(n_classes)]))
# Then interpolate all ROC curves at this points
mean_tpr = np.zeros_like(all_fpr)
for i in range(n_classes):
mean_tpr += interp(all_fpr, fpr[i], tpr[i])
# Finally average it and compute AUC
mean_tpr /= n_classes
fpr["macro"] = all_fpr
tpr["macro"] = mean_tpr
roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])
plot_roc_curve(fpr,tpr,roc_auc)
print('roc_auc =', roc_auc)
# Make y_score compatible with one hot labeling
# Rename to y_hat_new
y_hat_new = np.zeros_like(y_score)
y_hat_new[np.arange(len(y_score)), y_score.argmax(1)] = 1
acc = accuracy_score(y_test,y_hat_new)
outer_results.append(acc)
print('acc =', outer_results)
print('parameters =', results_dict)
print('mean_acc =', mean(outer_results))
print('stdev_acc =', std(outer_results))