-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
371 lines (303 loc) · 13.3 KB
/
main.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
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
import numpy as np
from knn import kNN
################ BANANA DATASET ################
# Opening file
print("################ BANANA DATASET ################")
banana_dataset = []
with open("databases/banana.dat", "r") as file:
for line in file:
banana_dataset.append(line.split("\n")[0].split(","))
banana_dataset = np.array(banana_dataset[7:])
# Moving label to first column
banana_dataset = banana_dataset[:,[2,0,1]]
# Dividing dataset
indices = np.random.permutation(banana_dataset.shape[0])
training_idx, test_idx = indices[:int(banana_dataset.shape[0]*0.7)], indices[int(banana_dataset.shape[0]*0.7):]
training_data, test_data = banana_dataset[training_idx,:], banana_dataset[test_idx,:]
# KNN 3
print("KNN, K=3")
banana_knn = kNN(training_data,test_data,3)
print(banana_knn.get_classification())
print("Accuracy:",banana_knn.get_accuracy())
print("Precision:",banana_knn.get_precision())
print("Recall:",banana_knn.get_recall())
# KNN 5
print("KNN, K=5")
banana_knn = kNN(training_data,test_data,5)
print(banana_knn.get_classification())
print("Accuracy:",banana_knn.get_accuracy())
print("Precision:",banana_knn.get_precision())
print("Recall:",banana_knn.get_recall())
################################################
################ AUSTRALIAN DATASET ################
# Opening file
print("################ AUSTRALIAN DATASET ################")
australian_dataset = []
with open("databases/australian.dat", "r") as file:
for line in file:
australian_dataset.append(line.split("\n")[0].split(","))
australian_dataset = np.array(australian_dataset[19:])
# Moving label to first column
labels = [14] + [x for x in range(0,14)]
australian_dataset = australian_dataset[:,labels]
# Dividing dataset
australian_indices = np.random.permutation(australian_dataset.shape[0])
australian_training_idx, australian_test_idx = australian_indices[:int(australian_dataset.shape[0]*0.7)], australian_indices[int(australian_dataset.shape[0]*0.7):]
australian_training_data, australian_test_data = australian_dataset[australian_training_idx,:], australian_dataset[australian_test_idx,:]
# KNN 3
print("KNN, K=3")
australian_knn = kNN(australian_training_data,australian_test_data,3)
print(australian_knn.get_classification())
print("Accuracy:",australian_knn.get_accuracy())
print("Precision:",australian_knn.get_precision())
print("Recall:",australian_knn.get_recall())
# KNN 5
print("KNN, K=5")
australian_knn = kNN(australian_training_data,australian_training_data,5)
print(australian_knn.get_classification())
print("Accuracy:",australian_knn.get_accuracy())
print("Precision:",australian_knn.get_precision())
print("Recall:",australian_knn.get_recall())
################################################
################ IRIS DATASET ################
# Opening file
print("################ IRIS DATASET ################")
iris_dataset = []
with open("databases/iris.dat", "r") as file:
for line in file:
iris_dataset.append(line.split("\n")[0].split(","))
iris_dataset = np.array(iris_dataset[9:])
# Moving label to first column
labels = [4] + [x for x in range(0,4)]
iris_dataset = iris_dataset[:,labels]
print(iris_dataset)
# Dividing dataset
iris_indices = np.random.permutation(iris_dataset.shape[0])
iris_training_idx, iris_test_idx = iris_indices[:int(iris_dataset.shape[0]*0.7)], iris_indices[int(iris_dataset.shape[0]*0.7):]
iris_training_data, iris_test_data = iris_dataset[iris_training_idx,:], iris_dataset[iris_test_idx,:]
# KNN 3
print("KNN, K=3")
iris_knn = kNN(iris_training_data,iris_test_data,3)
print(iris_knn.get_classification())
print("Accuracy:",iris_knn.get_accuracy())
print("Precision:",iris_knn.get_precision())
print("Recall:",iris_knn.get_recall())
# KNN 5
print("KNN, K=5")
iris_knn = kNN(iris_training_data,iris_test_data,5)
print(iris_knn.get_classification())
print("Accuracy:",iris_knn.get_accuracy())
print("Precision:",iris_knn.get_precision())
print("Recall:",iris_knn.get_recall())
################################################
################ BANDS DATASET ################
# Opening file
print("################ BANDS DATASET ################")
bands_dataset = []
with open("databases/bands.dat", "r") as file:
for line in file:
bands_dataset.append(line.split("\n")[0].split(","))
bands_dataset = np.array(bands_dataset[24:])
# Moving label to first column
order = [19] + [x for x in range(0,19)]
bands_dataset = bands_dataset[:,order]
# Dividing dataset
bands_indices = np.random.permutation(bands_dataset.shape[0])
bands_training_idx, bands_test_idx = bands_indices[:int(bands_dataset.shape[0]*0.7)], bands_indices[int(bands_dataset.shape[0]*0.7):]
bands_training_data, bands_test_data = bands_dataset[bands_training_idx,:], bands_dataset[bands_test_idx,:]
# KNN 3
print("KNN, K=3")
bands_knn = kNN(bands_training_data,bands_test_data,3)
print(bands_knn.get_classification())
print("Accuracy:",bands_knn.get_accuracy())
print("Precision:",bands_knn.get_precision())
print("Recall:",bands_knn.get_recall())
# KNN 5
print("KNN, K=5")
bands_knn = kNN(bands_training_data,bands_test_data,5)
print(bands_knn.get_classification())
print("Accuracy:",bands_knn.get_accuracy())
print("Precision:",bands_knn.get_precision())
print("Recall:",bands_knn.get_recall())
################################################
################ HEART DATASET ################
# Opening file
print("################ HEART DATASET ################")
heart_dataset = []
with open("databases/heart.dat", "r") as file:
for line in file:
heart_dataset.append(line.split("\n")[0].split(","))
heart_dataset = np.array(heart_dataset[18:])
# Moving label to first column
labels = [13] + [x for x in range(0,13)]
heart_dataset = heart_dataset[:,labels]
# Dividing dataset
indices = np.random.permutation(heart_dataset.shape[0])
training_idx, test_idx = indices[:int(heart_dataset.shape[0]*0.7)], indices[int(heart_dataset.shape[0]*0.7):]
training_data, test_data = heart_dataset[training_idx,:], heart_dataset[test_idx,:]
# KNN 3
print("KNN, K=3")
heart_knn = kNN(training_data,test_data,3)
print(heart_knn.get_classification())
print("Accuracy:",heart_knn.get_accuracy())
print("Precision:",heart_knn.get_precision())
print("Recall:",heart_knn.get_recall())
# KNN 5
print("KNN, K=5")
heart_knn = kNN(training_data,test_data,5)
print(heart_knn.get_classification())
print("Accuracy:",heart_knn.get_accuracy())
print("Precision:",heart_knn.get_precision())
print("Recall:",heart_knn.get_recall())
################################################
################ HABERMAN DATASET ################
# Opening file
print("################ HABERMAN DATASET ################")
haberman_dataset = []
with open("databases/haberman.dat", "r") as file:
for line in file:
haberman_dataset.append(line.split("\n")[0].split(","))
haberman_dataset = np.array(haberman_dataset[8:])
# Moving label to first column
labels = [3] + [x for x in range(0,3)]
haberman_dataset = haberman_dataset[:,labels]
# Dividing dataset
indices = np.random.permutation(haberman_dataset.shape[0])
training_idx, test_idx = indices[:int(haberman_dataset.shape[0]*0.7)], indices[int(haberman_dataset.shape[0]*0.7):]
training_data, test_data = haberman_dataset[training_idx,:], haberman_dataset[test_idx,:]
print(test_data.size)
# KNN 3
print("KNN, K=3")
haberman_knn = kNN(training_data,test_data,3)
print(haberman_knn.get_classification())
print("Accuracy:",haberman_knn.get_accuracy())
print("Precision:",haberman_knn.get_precision())
print("Recall:",haberman_knn.get_recall())
# KNN 5
print("KNN, K=5")
haberman_knn = kNN(training_data,test_data,5)
print(haberman_knn.get_classification())
print("Accuracy:",haberman_knn.get_accuracy())
print("Precision:",haberman_knn.get_precision())
print("Recall:",haberman_knn.get_recall())
################################################
################ THYROID DATASET ################
# Opening file
print("################ THYROID DATASET ################")
thyroid_dataset = []
with open("databases/thyroid.dat", "r") as file:
for line in file:
thyroid_dataset.append(line.split("\n")[0].split(","))
thyroid_dataset = np.array(thyroid_dataset[27:])
# Moving label to first column
labels = [21] + [x for x in range(0,21)]
thyroid_dataset = thyroid_dataset[:,labels]
# Dividing dataset
indices = np.random.permutation(thyroid_dataset.shape[0])
training_idx, test_idx = indices[:int(thyroid_dataset.shape[0]*0.7)], indices[int(thyroid_dataset.shape[0]*0.7):]
training_data, test_data = thyroid_dataset[training_idx,:], thyroid_dataset[test_idx,:]
# KNN 3
print("KNN, K=3")
thyroid_knn = kNN(training_data,test_data,3)
print(thyroid_knn.get_classification())
print("Accuracy:",thyroid_knn.get_accuracy())
print("Precision:",thyroid_knn.get_precision())
print("Recall:",thyroid_knn.get_recall())
# KNN 5
print("KNN, K=5")
thyroid_knn = kNN(training_data,test_data,5)
print(thyroid_knn.get_classification())
print("Accuracy:",thyroid_knn.get_accuracy())
print("Precision:",thyroid_knn.get_precision())
print("Recall:",thyroid_knn.get_recall())
################################################
################ appendicitis DATASET ################
# Opening file
print("################ appendicitis DATASET ################")
appendicitis_dataset = []
with open("databases/appendicitis.dat", "r") as file:
for line in file:
appendicitis_dataset.append(line.split("\n")[0].split(","))
appendicitis_dataset = np.array(appendicitis_dataset[12:])
# Moving label to first column
labels = [7] + [x for x in range(0,7)]
appendicitis_dataset = appendicitis_dataset[:,labels]
# Dividing dataset
indices = np.random.permutation(appendicitis_dataset.shape[0])
training_idx, test_idx = indices[:int(appendicitis_dataset.shape[0]*0.7)], indices[int(appendicitis_dataset.shape[0]*0.7):]
training_data, test_data = appendicitis_dataset[training_idx,:], appendicitis_dataset[test_idx,:]
# KNN 3
print("KNN, K=3")
appendicitis_knn = kNN(training_data,test_data,3)
print(appendicitis_knn.get_classification())
print("Accuracy:",appendicitis_knn.get_accuracy())
print("Precision:",appendicitis_knn.get_precision())
print("Recall:",appendicitis_knn.get_recall())
# KNN 5
print("KNN, K=5")
appendicitis_knn = kNN(training_data,test_data,5)
print(appendicitis_knn.get_classification())
print("Accuracy:",appendicitis_knn.get_accuracy())
print("Precision:",appendicitis_knn.get_precision())
print("Recall:",appendicitis_knn.get_recall())
################################################
################ titanic DATASET ################
# Opening file
print("################ titanic DATASET ################")
titanic_dataset = []
with open("databases/titanic.dat", "r") as file:
for line in file:
titanic_dataset.append(line.split("\n")[0].split(","))
titanic_dataset = np.array(titanic_dataset[8:])
# Moving label to first column
labels = [3] + [x for x in range(0,3)]
titanic_dataset = titanic_dataset[:,labels]
# Dividing dataset
indices = np.random.permutation(titanic_dataset.shape[0])
training_idx, test_idx = indices[:int(titanic_dataset.shape[0]*0.7)], indices[int(titanic_dataset.shape[0]*0.7):]
training_data, test_data = titanic_dataset[training_idx,:], titanic_dataset[test_idx,:]
# KNN 3
print("KNN, K=3")
titanic_knn = kNN(training_data,test_data,3)
print(titanic_knn.get_classification())
print("Accuracy:",titanic_knn.get_accuracy())
print("Precision:",titanic_knn.get_precision())
print("Recall:",titanic_knn.get_recall())
# KNN 5
print("KNN, K=5")
titanic_knn = kNN(training_data,test_data,5)
print(titanic_knn.get_classification())
print("Accuracy:",titanic_knn.get_accuracy())
print("Precision:",titanic_knn.get_precision())
print("Recall:",titanic_knn.get_recall())
################################################
################ winequality_red DATASET ################
# Opening file
print("################ winequality_red DATASET ################")
winequality_red_dataset = []
with open("databases/winequality-red.dat", "r") as file:
for line in file:
winequality_red_dataset.append(line.split("\n")[0].split(","))
winequality_red_dataset = np.array(winequality_red_dataset[16:])
# Moving label to first column
labels = [11] + [x for x in range(0,11)]
winequality_red_dataset = winequality_red_dataset[:,labels]
# Dividing dataset
indices = np.random.permutation(winequality_red_dataset.shape[0])
training_idx, test_idx = indices[:int(winequality_red_dataset.shape[0]*0.7)], indices[int(winequality_red_dataset.shape[0]*0.7):]
training_data, test_data = winequality_red_dataset[training_idx,:], winequality_red_dataset[test_idx,:]
# KNN 3
print("KNN, K=3")
winequality_red_knn = kNN(training_data,test_data,3)
print(winequality_red_knn.get_classification())
print("Accuracy:",winequality_red_knn.get_accuracy())
print("Precision:",winequality_red_knn.get_precision())
print("Recall:",winequality_red_knn.get_recall())
# KNN 5
print("KNN, K=5")
winequality_red_knn = kNN(training_data,test_data,5)
print(winequality_red_knn.get_classification())
print("Accuracy:",winequality_red_knn.get_accuracy())
print("Precision:",winequality_red_knn.get_precision())
print("Recall:",winequality_red_knn.get_recall())
################################################