def experiment(Xtrn, Ctrn, Xtst, Ctst): classes = np.unique(Ctrn) N = np.size(classes) Kns_vector = [] for i in classes: acc_list = [] labels_list = [] for j in NUM_OF_CLUSTERS_TO_TEST: labels = my_kmeans_classify(get_class_vectors(Xtrn, Ctrn, i), j) new_Ctrn = update_labels(Ctrn, labels, i, N) [Cpreds, _, _] = my_gaussian_classify(Xtrn, new_Ctrn, Xtst) preds = normalize_preds(Cpreds, i, N) [_, acc] = conf(Ctst, preds) acc_list.append(acc) max = np.argmax(acc_list) Kns_vector.append(NUM_OF_CLUSTERS_TO_TEST[max]) return np.array(Kns_vector).transpose()
from data_utils import load_data from my_knn_classify import my_knn_classify as knn from my_confusion import my_confusion as conf import time import numpy as np import scipy.io as sc Ks = [1, 3, 5, 10, 20] [Xtrn, Ctrn, Xtst, Ctst] = load_data() start = time.clock() Cpreds = knn(Xtrn, Ctrn, Xtst, Ks) end = time.clock() print "Time taken: ", (end - start) N = np.size(Ctst) for i in range(np.size(Ks)): [CM, acc] = conf(Ctst, Cpreds[:, i].reshape(N, 1)) print "Number of nearest neighbours: ", Ks[i] print "Number of samples: ", N print "Number of misclassifications: ", int(N - np.trace(CM)) print "Accuracy: ", acc print "--------------------------------" filename = "cm" + str(Ks[i]) sc.savemat(filename, {"Confusion_matrix": CM})
from data_utils import load_data from my_gaussian_classify import my_gaussian_classify as gaussian from my_confusion import my_confusion as conf import numpy as np import time import scipy.io as sc [Xtrn, Ctrn, Xtst, Ctst] = load_data() start = time.clock() [Cpreds, Ms, Covs] = gaussian(Xtrn, Ctrn, Xtst, 0.01) end = time.clock() print "Time taken: ", (end - start) [cm, acc] = conf(Ctst, Cpreds) sc.savemat("cm", {"Confusion_matrix": cm}) sc.savemat("m26", {"Mean_vector": Ms[:, 25]}) sc.savemat("cov26", {"Covariance_matrix": Covs[:, :, 25]}) N = np.size(Ctst) print "Number of samples: ", N print "Number of misclassifications: ", int(N - np.trace(cm)) print "Accuracy: ", acc