Ejemplo n.º 1
0
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")