def run_svm(inputs, targets, nRuns=5): total_time, total_percent = 0, 0 for i in range(nRuns): x, y = shuffle_data(inputs, targets, True, i) x_in, x_out, y_in, y_out = separate_data(x, y, False) for i in range(x_out.shape[0]): for j in range(x_out.shape[1]): if x_out[i, j] == 0.0: x_out[i, j] = -1.0 start = time() predict = np.zeros((y_out.shape[0], 3)) classifier0 = svm(x_in, x_out[:, 0], kernel="rbf") predict[:, 0] = classifier0(y_in, soft=True).T classifier1 = svm(x_in, x_out[:, 1], kernel="rbf") predict[:, 1] = classifier1(y_in, soft=True).T classifier2 = svm(x_in, x_out[:, 2], kernel="rbf") predict[:, 2] = classifier2(y_in, soft=True).T total_time += time() - start cm = conf_mat(predict, y_out) total_percent += np.trace(cm) / np.sum(cm) * 100 return total_time / nRuns, total_percent / nRuns
def run_knn(inputs, targets, nRuns=5): total_time, total_percent = 0, 0 for i in range(nRuns): x, y = shuffle_data(inputs, targets, True, i) x_in, x_out, y_in, y_out = separate_data(x, y, False) start = time() predict = knn(3, x_in, x_out, y_in) total_time += time() - start cm = conf_mat(predict, y_out) total_percent += np.trace(cm) / np.sum(cm) * 100 return total_time / nRuns, total_percent / nRuns
def run_boost(inputs, targets, features, nRuns=5): total_time, total_percent = 0, 0 #classes = [None] * len(targets) classes = np.empty((len(targets), 1), dtype=int) for i in range(len(targets)): if targets[i][0] == 1: classes[i] = 0 #"Setosa" elif targets[i][1] == 1: classes[i] = 1 #"Versicolour" else: classes[i] = 2 #"Virginica" #classes = np.array(classes).reshape(len(targets), 1) for i in range(nRuns): x, y = shuffle_data(inputs, classes, True, i) x_in, x_out, y_in, y_out = separate_data(x, y, False) x_out, y_out = x_out[:, 0], y_out[:, 0] start = time() classify = boosting(x_in, x_out, features) predict = np.empty((len(y_out), 1), dtype=int) for idx, val in enumerate(y_in): predict[idx] = classify(val) total_time += time() - start oneHotPredict = np.empty((len(y_out), 3), dtype=int) oneHotYout = np.empty((len(y_out), 3), dtype=int) for i in range(len(predict)): if predict[i] == 0: oneHotPredict[i] = [1, 0, 0] elif predict[i] == 1: oneHotPredict[i] = [0, 1, 0] else: oneHotPredict[i] = [0, 0, 1] if y_out[i] == 0: oneHotYout[i] = [1, 0, 0] elif y_out[i] == 1: oneHotYout[i] = [0, 1, 0] else: oneHotYout[i] = [0, 0, 1] cm = conf_mat(oneHotPredict, oneHotYout) total_percent += np.trace(cm) / np.sum(cm) * 100 return total_time / nRuns, total_percent / nRuns
def run_rbf_wtm(inputs, targets, nRuns=5): total_time, total_percent = 0, 0 for i in range(nRuns): x, y = shuffle_data(inputs, targets, True, i) x_in, x_out, v_in, v_out, y_in, y_out = separate_data(x, y, True) start = time() forward = rbf(x_in, x_out, outtype="weight_matrix", nRbf=5, normalize=True) predict = forward(y_in) total_time += time() - start cm = conf_mat(predict, y_out) total_percent += np.trace(cm) / np.sum(cm) * 100 return total_time / nRuns, total_percent / nRuns
def run_mlp(inputs, targets, nRuns=5): total_time, total_percent = 0, 0 for i in range(nRuns): x, y = shuffle_data(inputs, targets, True, i) x_in, x_out, v_in, v_out, y_in, y_out = separate_data(x, y, True) start = time() forward = mlp(x_in, x_out, valid_inputs=v_in, valid_targets=v_out, validate=True, outtype="softmax", eta=0.1, nHidden=5, valid_iterations=100) predict = forward(y_in) total_time += time() - start cm = conf_mat(predict, y_out) total_percent += np.trace(cm) / np.sum(cm) * 100 return total_time / nRuns, total_percent / nRuns
import os import numpy as np import pandas as pd from Learning.ensemble_learning import randomForest from Learning.scoring import conf_mat from Preprocessing.reformat import one_hot_encode if __name__ == "__main__": df = pd.read_csv(os.path.join(os.getcwd(), "Data/car.data")) car = df.to_numpy() x_in = car[::2, :-1] x_out = car[::2, -1] y_in = car[1::2, :-1] y_out = car[1::2, -1] features = ["buying", "maint", "doors", "persons", "lug_boot", "safety"] classify = randomForest(x_in, x_out, features, 5) predict = classify(y_in) options = ["unacc", "acc", "good", "vgood"] predict_check, y_out_check = one_hot_encode(predict, options), one_hot_encode( y_out, options) breakpoint() conf_mat(predict_check, y_out_check)