-
Notifications
You must be signed in to change notification settings - Fork 70
/
knn.py
104 lines (90 loc) · 3.42 KB
/
knn.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
import numpy as np
from keras.utils import np_utils
import time
from datetime import timedelta
from sklearn import neighbors
from sklearn.externals import joblib
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
import argparse
import math
from utils import dataset_traditional as dataset
def build_dataset(data_directory, img_width):
X, y, tags = dataset.dataset(data_directory, int(img_width))
nb_classes = len(tags)
feature = X
label = np_utils.to_categorical(y, nb_classes)
return feature, label, nb_classes
def knn_classifier(features, target):
clf = neighbors.KNeighborsClassifier(algorithm='kd_tree')
clf.fit(features, target)
return clf
def main():
start_time = time.monotonic()
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-i', '--input',
help='an input directory of dataset', required=True)
parser.add_argument('-d', '--dimension',
help='a image dimension', type=int, default=50)
parser.add_argument('-o', '--output',
help='a result file', type=str, default="hasilnya.txt")
args = parser.parse_args()
data_directory = args.input
print("loading dataset")
X_train, Y_train, nb_classes = build_dataset(
"{}/train".format(data_directory), args.dimension)
X_test, Y_test, nb_classes = build_dataset(
"{}/test".format(data_directory), args.dimension)
print("number of classes : {}".format(nb_classes))
trained_model = knn_classifier(X_train, Y_train)
predicted = trained_model.predict(X_test)
y_pred = np.argmax(predicted, axis=1)
Y_test = np.argmax(Y_test, axis=1)
cm = confusion_matrix(Y_test, y_pred)
report = classification_report(Y_test, y_pred)
tn = cm[0][0]
fn = cm[1][0]
tp = cm[1][1]
fp = cm[0][1]
if tp == 0:
tp = 1
if tn == 0:
tn = 1
if fp == 0:
fp = 1
if fn == 0:
fn = 1
TPR = float(tp)/(float(tp)+float(fn))
FPR = float(fp)/(float(fp)+float(tn))
accuracy = round((float(tp) + float(tn))/(float(tp) +
float(fp) + float(fn) + float(tn)), 3)
specitivity = round(float(tn)/(float(tn) + float(fp)), 3)
sensitivity = round(float(tp)/(float(tp) + float(fn)), 3)
mcc = round((float(tp)*float(tn) - float(fp)*float(fn))/math.sqrt(
(float(tp)+float(fp))
* (float(tp)+float(fn))
* (float(tn)+float(fp))
* (float(tn)+float(fn))
), 3)
f_output = open(args.output, 'a')
f_output.write('=======\n')
f_output.write('knn_model_{}\n'.format(
data_directory.replace("/", "_")))
f_output.write('TN: {}\n'.format(tn))
f_output.write('FN: {}\n'.format(fn))
f_output.write('TP: {}\n'.format(tp))
f_output.write('FP: {}\n'.format(fp))
f_output.write('TPR: {}\n'.format(TPR))
f_output.write('FPR: {}\n'.format(FPR))
f_output.write('accuracy: {}\n'.format(accuracy))
f_output.write('specitivity: {}\n'.format(specitivity))
f_output.write("sensitivity : {}\n".format(sensitivity))
f_output.write("mcc : {}\n".format(mcc))
f_output.write("{}".format(report))
f_output.write('=======\n')
f_output.close()
end_time = time.monotonic()
print("Duration : {}".format(timedelta(seconds=end_time - start_time)))
if __name__ == "__main__":
main()