-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_breast.py
194 lines (150 loc) · 5.61 KB
/
main_breast.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
import numpy as np
from utils import *
from neural import *
from metrics import *
import math
from sklearn.utils import shuffle
import time
import sys
import csv
max_iterations = 1000
epsilon = 0.0000010000
num_kfolds = 10
def main():
inicio = time.time()
np.set_printoptions(precision=5)
network = []
fnetwork = open(sys.argv[1], "r")
regularization = float(fnetwork.readline())
alpha = float(fnetwork.readline())
for line in fnetwork:
network.append(int(line))
fnetwork.close()
inputs = []
predictions = []
fdataset = open("data/breast-cancer-wisconsin.data", "r")
i = 0
for l in fdataset:
a, b = l.split(";")
inputs.append([])
for v in a.split(","):
inputs[i].append(float(v))
predictions.append([])
for v in b.split(","):
predictions[i].append(float(v))
i = i + 1
for l in range(0, len(inputs)):
inputs[l] = np.array(inputs[l], ndmin=2).T
for l in range(0, len(predictions)):
predictions[l] = np.array(predictions[l], ndmin=2).T
fdataset.close()
class1 = 0
class2 = 0
l_class = []
for v_pred in predictions:
if [[1, 0]] in v_pred.T:
class1 += 1
class2 = len(predictions)-class1
l_class.append(class1)
l_class.append(class2)
for valor in range(0, 2):
l_class[valor] = int(l_class[valor]/num_kfolds)
count_class1 = 0
line_class1 = []
count_class2 = 0
line_class2 = []
for v_pred in predictions:
if [[1, 0]] in v_pred.T:
line_class1.append(count_class1)
count_class1 += 1
if [[0, 1]] in v_pred.T:
line_class2.append(count_class2)
count_class2 += 1
lista_kfold_input =[]
lista_kfold_predition =[]
for i in range(0, num_kfolds-1):
tempx = []
tempy = []
for k in range(0, l_class[0]):
line = np.random.randint(0, len(line_class1))
tempx.append(inputs[line_class1[line]])
tempy.append(predictions[line_class1[line]])
line_class1.remove(line_class1[line])
for k in range(0, l_class[1]):
line = np.random.randint(0, len(line_class2))
tempx.append(inputs[line_class2[line]])
tempy.append(predictions[line_class2[line]])
line_class2.remove(line_class2[line])
lista_kfold_input.append(tempx)
lista_kfold_predition.append(tempy)
tempx = []
tempy = []
for i in range(0, len(line_class1)):
tempx.append(inputs[line_class1[i]])
tempy.append(predictions[line_class1[i]])
for i in range(0, len(line_class2)):
tempx.append(inputs[line_class2[i]])
tempy.append(predictions[line_class2[i]])
lista_kfold_input.append(tempx)
lista_kfold_predition.append(tempy)
k_f = 0
f_mes = []
while k_f != num_kfolds:
weights=build_weights(network)
train_input = []
train_pred = []
test_pred = lista_kfold_predition[k_f]
test_input = lista_kfold_input[k_f]
for w in range(0, num_kfolds):
for j in range(0, len(lista_kfold_input[w])):
if w != k_f:
train_input.append(lista_kfold_input[w][j])
train_pred.append(lista_kfold_predition[w][j])
train_input, train_pred = shuffle(train_input, train_pred)
test_input, test_pred = shuffle(test_input, test_pred)
# here! add normalize data
train_input = normalize(np.asarray(train_input))
test_input = normalize(np.asarray(test_input))
# print(print2D(test_input),print2D(test_pred))
new_weigths = neural_network(network, weights, regularization, train_input, train_pred, max_iterations,alpha)
k_f += 1
results = feedfoward(network, new_weigths, test_input, test_pred)
class1 = np.array(([1],[0]))
class2 = np.array(([0],[1]))
confusion_matrix = np.zeros(shape=(2, 2))
for j in range(0, len(results)):
if np.array_equal(class1,results[j]) and np.array_equal(test_pred[j],results[j]):
confusion_matrix[0][0] += 1
elif np.array_equal(class2,results[j]) and np.array_equal(test_pred[j],results[j]):
confusion_matrix[1][1] += 1
elif np.array_equal(class1, results[j]) and not(np.array_equal(test_pred[j], results[j])):
confusion_matrix[0][1] += 1
elif np.array_equal(class2, results[j]) and not (np.array_equal(test_pred[j], results[j])):
confusion_matrix[1][0] += 1
print(confusion_matrix)
prec_array = []
rec_array = []
# calcula prec e rec pra cada coluna
for col in range(0, len(confusion_matrix) - 1):
conf_matrix_bin = confusion_matrix_bin(confusion_matrix, col)
prec_array.append(precision(conf_matrix_bin))
rec_array.append(recall(conf_matrix_bin))
# media de prec e recal
prec, rec = macro_median(prec_array, rec_array)
beta = 1
# calculo da f_measure para a macro median
f1 = f_measure(prec, rec, beta)
f_mes.append(f1)
media_fmes = sum(f_mes)/len(f_mes)
fim = time.time()
print("lambda:", regularization)
print("alpha:", alpha)
print("media:", media_fmes)
for q in range(0, len(f_mes)):
variancia = math.pow(f_mes[q] - media_fmes, 2) / len(f_mes)
print("variancia:", variancia)
desvio_padrao = math.sqrt(variancia)
print("desvio_padrao:", desvio_padrao)
print("tempo:", fim - inicio)
if __name__ == "__main__":
main()