87 lines
2.5 KiB
Python
87 lines
2.5 KiB
Python
import numpy as np
|
|
import time as t
|
|
from sklearn import svm, ensemble, naive_bayes, neighbors
|
|
from _image_classifier import ImageClassifier
|
|
|
|
def accuracy(y_true, y_pred):
|
|
"""returns the accuracy"""
|
|
y_pred = np.round(y_pred)
|
|
return (y_true == y_pred).mean()
|
|
|
|
def precision(y_true, y_pred):
|
|
"""returns the precision"""
|
|
y_pred = np.round(y_pred)
|
|
num = np.sum(np.logical_and(y_true, y_pred))
|
|
den = np.sum(y_pred)
|
|
return np.divide(num, den)
|
|
|
|
def recall(y_true, y_pred):
|
|
"""returns the recall"""
|
|
y_pred = np.round(y_pred)
|
|
num = np.sum(np.logical_and(y_true, y_pred))
|
|
den = np.sum(y_true)
|
|
return np.divide(num, den)
|
|
|
|
def f_measure(y_true, y_pred):
|
|
"""returns the F1 measure"""
|
|
p = precision(y_true, y_pred)
|
|
r = recall(y_true, y_pred)
|
|
return 2 * p * r / (p + r)
|
|
|
|
def metric_test(iclf, metric, test_X, test_Y):
|
|
return metric(test_Y, iclf.predict(test_X))
|
|
|
|
## Open data
|
|
im_train = np.load('Waldo_train_data.npy')
|
|
im_test = np.load('Waldo_test_data.npy')
|
|
|
|
lbl_train = np.load('Waldo_train_lbl.npy')
|
|
lbl_test = np.load('Waldo_test_lbl.npy')
|
|
|
|
# lbl_train = to_categorical(lbl_train) # One hot encoding the labels
|
|
# lbl_test = to_categorical(lbl_test)
|
|
|
|
my_metric_test = lambda iclf, f: metric_test(iclf, f, im_test, lbl_test)
|
|
|
|
# ## Define model
|
|
svm_iclf = ImageClassifier(svm.SVC)
|
|
tree_iclf = ImageClassifier(neighbors.KNeighborsClassifier)
|
|
naive_bayes_iclf = ImageClassifier(naive_bayes.GaussianNB)
|
|
forest_iclf = ImageClassifier(ensemble.RandomForestClassifier)
|
|
|
|
classifiers = [
|
|
svm_iclf,
|
|
tree_iclf,
|
|
naive_bayes_iclf,
|
|
forest_iclf,
|
|
]
|
|
|
|
classifier_names = [
|
|
"svm",
|
|
"tree",
|
|
"naive_bayes",
|
|
"forest",
|
|
]
|
|
|
|
# print("name,time,accuracy,precision,recall,f_measure")
|
|
print("name,time,accuracy")
|
|
for clf, name in zip(classifiers, classifier_names):
|
|
start = t.time() # Records time before training
|
|
clf.fit(im_train, lbl_train)
|
|
end = t.time() # Records time after tranining
|
|
y_pred = clf.predict(im_test)
|
|
print(
|
|
name,
|
|
end-start,
|
|
clf.score(im_test, lbl_test),
|
|
# precision(lbl_test, y_pred),
|
|
# recall(lbl_test, y_pred),
|
|
# f_measure(lbl_test, y_pred),
|
|
sep=","
|
|
)
|
|
# print("training time:\t", end-start)
|
|
# print("Accuracy:\t", clf.score(im_test, lbl_test))
|
|
# print("Precision:\t", precision(lbl_test, y_pred))
|
|
# print("Recall:\t\t", recall(lbl_test, y_pred))
|
|
# print("F1-measure:\t", f_measure(lbl_test, y_pred))
|