def separable(): print("----------------separable-----------------------") mat = Arff("./separableIsSquare.arff", label_count=1) np_mat = mat.data data = mat[:, :-1] labels = mat[:, -1].reshape(-1, 1) print(data[:, 1]) print(labels) ### Make the Classifier ##### P3Class = None for lr in range(10, 0, -1): P3Class = PerceptronClassifier(lr=0.1*lr, shuffle=False) P3Class.fit(data, labels, standard_weight_value=None) Accuracy = P3Class.score(data, labels) print("Learning Rate = ", 0.1*lr) print("Accuracy = [{:.2f}]".format(Accuracy)) print("Epochs = ", P3Class.get_epochs_trained()) # print(P3Class) ## could not get graphing to work in time... # graph(data[:, 0], data[:, 1], labels=mat[:, -1]) w = P3Class.get_weights() y = lambda x: (-w[0]/w[1])*x - (w[2]/w[1]) grapher = Grapher() grapher.graph(data[:, 0], data[:, 1], labels=mat[:, -1], title="Separable") grapher.add_function(y) grapher.show("separable.svg")
def inseparable(): print("----------------Inseparable-----------------------") mat = Arff("./impossible.arff", label_count=1) np_mat = mat.data data = mat[:, :-1] labels = mat[:, -1].reshape(-1, 1) ### Make the Classifier ##### P4Class = None for lr in range(10, 0, -1): P4Class = PerceptronClassifier(lr=0.1*lr, deterministic=10, shuffle=False) P4Class.fit(data, labels, standard_weight_value=None) Accuracy = P4Class.score(data, labels) print("Learning Rate = ", 0.1*lr) print("Accuracy = [{:.2f}]".format(Accuracy)) print("Epochs = ", P4Class.get_epochs_trained()) w = P4Class.get_weights() y = lambda x: (-w[0]/w[1])*x - (w[2]/w[1]) grapher = Grapher() grapher.graph(data[:, 0], data[:, 1], labels=mat[:, -1], title="Inseparable") grapher.add_function(y) grapher.show("Inseparable.svg")
def voting(): print("--------------voting---------------------") mat = Arff("../data/perceptron/vote.arff", label_count=1) np_mat = mat.data avg = [] for iteration in range(5): print("xxxxxxxxxxx " + str(iteration) + " xxxxxxxx") training, testing = _shuffle_split(mat.data, .3) data = training[:, :-1] labels = training[:, -1].reshape(-1, 1) P5Class = PerceptronClassifier(lr=0.1, shuffle=True) P5Class.fit(data, labels) Accuracy = P5Class.score(data, labels) print("Accuracy = [{:.2f}]".format(Accuracy)) print("Epochs = ", P5Class.get_epochs_trained()) tData = testing[:, :-1] tLabels = testing[:, -1].reshape(-1, 1) tAccuracy = P5Class.score(tData, tLabels) print("Test Accuracy = [{:.2f}]".format(tAccuracy)) weights = P5Class.get_weights() print(weights) sort_weights = sorted(zip(weights, list(range(len(weights)))), key=lambda x: abs(x[0]), reverse=True) print("sorted:\r\n", sort_weights) scores = P5Class.getTrace().getColumns("epochScore") print('scores', scores) avg.append((float(scores[-2][0]) - float(scores[0][0])) / len(scores)) print('avg', avg) grapher = Grapher() grapher.graph(list(range(len(avg))), avg, labels=[1]*len(avg), points=False, title="Average Scores", xlabel="Iteration", ylabel="score") grapher.show("AverageScores.svg")