-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleNeuronalNetwork.py
More file actions
207 lines (129 loc) · 5.19 KB
/
Copy pathSimpleNeuronalNetwork.py
File metadata and controls
207 lines (129 loc) · 5.19 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
#!/bin/python3
# -*-coding:utf-8 -*-
"""Importing librairies"""
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn import linear_model
from numpy import *
import math
import matplotlib.pyplot as plt
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
from keras.utils import to_categorical
from keras.models import Model
from keras.layers import Dense, Input
from keras.layers import Dropout
"""Creating filename of the file"""
dataFile = "../output/399GenesDataframe/newDataFrame.csv"
""" Creating a counter Line"""
counterLine = 0
"""Reading the file"""
openingDataFile = open(dataFile, 'r')
"""Counting the lines"""
while(openingDataFile.readline()):
counterLine += 1
"""Closing the file"""
openingDataFile.close()
"""ReOpening the file"""
openingDataFile = open(dataFile, 'r')
"""Creating a dictionnary of Labels"""
dictionnaryLabels = {}
""" Reading header line and creating items of dictionnary"""
line = openingDataFile.readline().replace('\n', '')
lineSplitted = line.split(',')
lineSplitted[0] = "X"
listOfHeaders = lineSplitted
listOfData = []
"""Reading file and assigning data to item"""
for i in range(0, counterLine - 1):
line = openingDataFile.readline().replace('\n', '')
lineSplitted = line.split(',')
listOfData.append(lineSplitted)
openingDataFile.close()
"""Creating DataFrame of Data"""
dataFrameOfData = pd.DataFrame(listOfData, columns= listOfHeaders)
"""Deleting unused variables"""
del(listOfData)
del(listOfHeaders)
"""Creating a list of labels"""
columnLabels = dataFrameOfData["Labels"]
uniqColumnLabels = list(columnLabels.drop_duplicates())
"""Taking only data from dataframe"""
dataFrameOfData = dataFrameOfData.iloc[:, 1:len(dataFrameOfData.columns) - 1]
"""Creating numbers to categorize labels"""
for i,j in zip(uniqColumnLabels, range(0, len(uniqColumnLabels))):
dictionnaryLabels[i] = j
"""Categorisating Labels"""
listOfLabels = []
for i in columnLabels:
listOfLabels.append(dictionnaryLabels[i])
listOfLabels = to_categorical(listOfLabels)
"""Splitting data in training and testing datasets"""
X_train, X_test, Y_train, Y_test = train_test_split(dataFrameOfData, listOfLabels, test_size=0.33, random_state=42, stratify = columnLabels)
"""Deleting unused variables"""
del(dataFrameOfData)
del(columnLabels)
del(dictionnaryLabels)
del(listOfLabels)
"""Importing the Ridge Regularization term"""
from keras.regularizers import l2
"""Creating Neural Network"""
def initModel():
init = 'random_uniform'
input_layer = Input(shape=(397,))
mid_layer = Dense(6, activation = 'relu', kernel_initializer = init, kernel_regularizer=l2(0.001))(input_layer)
mid_layer2 = Dropout(0.01, input_shape = (6,))(mid_layer)
output_layer = Dense(5, activation = 'softmax', kernel_initializer = init)(mid_layer2)
model = Model(input = input_layer, output = output_layer)
model.compile(optimizer='sgd',loss='binary_crossentropy',metrics=['accuracy'])
return(model)
""" Saving values for Accuracy Curve"""
training_accuracy = []
testing_accuracy = []
index = []
"""Creating a function to draw the Accuracy curves"""
def tracingPlots(figureName = "False"):
for i in range(50, X_train.shape[0], 20):
# Model initialization
model = initModel()
# Model fit
model.fit(X_train.iloc[1:i,:],Y_train[1:i,:], batch_size=6, epochs=100, verbose=2, validation_split=0.1)
# Prediction with training dataset
Z_train = model.predict(X_train.iloc[i:X_train.shape[0],:])
# Prediction with testing dataset
Z_test = model.predict(X_test)
prediction_train = np.argmax(Z_train, axis = 1)
prediction_test = np.argmax(Z_test, axis = 1)
# Accuracy
training_accuracy.append(accuracy_score(np.argmax(Y_train[i:X_train.shape[0],:], axis = 1), prediction_train))
testing_accuracy.append(accuracy_score(np.argmax(Y_test, axis = 1), prediction_test))
index.append(i)
"""Drawing Accuracy curves"""
figure = plt.figure()
plt.plot(index, training_accuracy, label = 'Training')
plt.plot(index, testing_accuracy, label = 'Testing')
plt.legend()
if (figureName != "False"):
plt.savefig("../figures/{}.eps".format(figureName), dpi = 600, format = "eps", bbox_inches='tight')
plt.close()
"""Creating a function to draw loss curves"""
def tracingLearningCurve(figureName = "False"):
model = initModel()
history = model.fit(X_train,Y_train, batch_size=6, epochs=100, verbose=2, validation_split=0.1, validation_data=(X_test, Y_test))
"""Drawing loss curves"""
figure = plt.figure()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
if (figureName != "False"):
plt.savefig("../figures/{}.eps".format(figureName), dpi = 600, format = "eps", bbox_inches='tight')
else:
plt.show()
plt.close()
"""Drawing learning curves"""
tracingPlots(figureName="BestNeuralNetworkAccuracyPlot")
tracingLearningCurve(figureName="BestNeuralNetworkLossCurve")