Ejemplo n.º 1
0
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")
Ejemplo n.º 2
0
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")
Ejemplo n.º 3
0
def our_avg_run(avg_num_of_run,filename):
    dataset = load_dataset(filename)



    ptraining_error  = []
    perceptron_error = []

    for i in range(avg_num_of_run):
        (training_set, testing_set) = split_dataset(dataset, PROBABILITY_TRAINING_SET)
        testing_set = dataset
        if IS_VERBOSE:
        	print("training set size: %s testing set size: %s num instances: %s" %
                  (len(training_set), len(testing_set), len(dataset)))

        (train_x, train_y) = split_attribute_and_label(training_set)
        (test_x, test_y) = split_attribute_and_label(testing_set)
        p = PerceptronClassifier(ETA, THRESHOLD, UPPER_BOUND, False)
        p.fit(train_x, train_y)
        t_result_list = p.predict(train_x)
        ptraining_error.append(calculate_error(train_y, t_result_list))
        result_list = p.predict(test_x)
        perceptron_error.append(calculate_error(test_y, result_list))
        print(p.weights)

    return sum(perceptron_error) / len(perceptron_error) , sum(ptraining_error) / len(ptraining_error)
Ejemplo n.º 4
0
def debug():
    print("------------arff-------------------")

    mat = Arff("../data/perceptron/debug/linsep2nonorigin.arff", label_count=1)
    data = mat.data[:, 0:-1]
    labels = mat.data[:, -1].reshape(-1, 1)
    PClass = PerceptronClassifier(
        lr=0.1, shuffle=False, deterministic=10, printIt=False)
    PClass.fit(data, labels)
    Accuracy = PClass.score(data, labels)
    print("Accuray = [{:.2f}]".format(Accuracy))
    print("Final Weights =", PClass.get_weights())
Ejemplo n.º 5
0
def evaluation():
    print("--------------arf2------------------------------")

    mat = Arff("../data/perceptron/evaluation/data_banknote_authentication.arff", label_count=1)
    np_mat = mat.data
    data = mat[:, :-1]
    labels = mat[:, -1].reshape(-1, 1)

    #### Make Classifier ####
    P2Class = PerceptronClassifier(lr=0.1, shuffle=False, deterministic=10)
    P2Class.fit(data, labels)
    Accuracy = P2Class.score(data, labels)
    print("Accuray = [{:.2f}]".format(Accuracy))
    print("Final Weights =", P2Class.get_weights())
Ejemplo n.º 6
0
def runMahCode(arff, shuffle=True, determ=0, training=False, lr=.1, quiet=False):
    mat = Arff(arff,label_count=1)
    data = mat.data[:,0:-1]
    labels = mat.data[:,-1:]
    PClass = PerceptronClassifier(lr=lr,shuffle=shuffle,deterministic=determ)
    Accuracy = 0.0
    if (training):
        X_train, y_train, X_test, y_test = PerceptronClassifier.split_training(data,labels)
        PClass.fit(X_train,y_train)
        Accuracy = PClass.score(X_test,y_test)
    else:
        PClass.fit(data,labels)
        Accuracy = PClass.score(data,labels)
    if not quiet:
        print("Accuracy = [{:.5f}]".format(Accuracy))
        print("Final Weights =",PClass.get_weights())
    else:
        return Accuracy
Ejemplo n.º 7
0
def basic():
    print("-------------basic---------------")

    x = np.array([[0,0,1],[1,1,1],[1,0,1],[0,1,1]])
    y = np.array([[0],[1],[1],[0]])

    # print(x)
    # print(y)

    pc = PerceptronClassifier(lr=1)
    # print(pc)
    print(pc.fit(x, y).score(np.array([[0,0,0],[0,1,0],[1,0,0],[1,1,0]]), np.array([[0],[0],[1],[1]])))
    print(pc)
Ejemplo n.º 8
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")
Ejemplo n.º 9
0
Archivo: eval.py Proyecto: pts314/CS472
from perceptron import PerceptronClassifier
from arff import Arff
import numpy as np

mat = Arff("../data/perceptron/evaluation/data_banknote_authentication.arff",
           label_count=1)
data = mat.data[:, 0:-1]
labels = mat.data[:, -1:]
PClass = PerceptronClassifier(lr=0.1, shuffle=False, deterministic=10)
PClass.fit(data, labels)
Accuracy = PClass.score(data, labels)
print("Accuray = [{:.5f}]".format(Accuracy))
print("Final Weights =", PClass.get_weights())
Ejemplo n.º 10
0
plt.show();
# add the plot later


# # 3. Train on both sets

# Linearly Separable:

# In[9]:


data = linSep[:,:-1]
labels = linSep[:,-1:]
for lr in np.linspace(.01, 1, 10):
    PClass = PerceptronClassifier(lr=lr,shuffle=True,deterministic=0)
    PClass.fit(data,labels)
    Accuracy = PClass.score(data,labels)
    print(Accuracy)


# Because everything is centered very close to zero, it doesn't take very long for the learning rate to differentiate the two sets. 
# 
# We also see that the learning rate doesn't change much.

# In[10]:


data = nonLin[:,:-1]
labels = nonLin[:,-1:]
for lr in np.linspace(.01, 1, 10):
    PClass = PerceptronClassifier(lr=lr,shuffle=True,deterministic=0)
Ejemplo n.º 11
0
import matplotlib.pyplot as plt
from utils import DatasetGenerator
from perceptron import PerceptronClassifier
import numpy as np

discr_func = 'ellipse'  # choose one of ['linear', 'ellipse', 'quadratic']
data_generator = DatasetGenerator()
X, y = data_generator.generate_separable_data(num_points=57,
                                              discr_func=discr_func)

clf = PerceptronClassifier(discr_func=discr_func)
clf.fit(X, y)

plt.xlim(-14, 14)
plt.ylim(-14, 14)
# plot data points
plt.scatter(X[:, 0], X[:, 1], s=25, c=y)
# plot separating curve
xmin, xmax = plt.xlim()
ymin, ymax = plt.ylim()
xlist = np.linspace(xmin, xmax, 100)
ylist = np.linspace(ymin, ymax, 100)
XX, YY = np.meshgrid(xlist, ylist)
xy = np.vstack([XX.ravel(), YY.ravel()]).T
Z = clf.score(xy).reshape(XX.shape)
plt.contour(XX, YY, Z, levels=[0], colors=['r'])
plt.show()
Ejemplo n.º 12
0
    mat = Arff("votingMissingValuesReplaced.arff", label_count=1)
    # mat = Arff("test2.arff", label_count=1)

    data = mat.data[:, 0:-1]
    labels = mat.data[:, -1].reshape(-1, 1)

    X_train, y_train, X_eval, y_eval = split(data,
                                             labels)  # split data in 70/30

    # PClass = PerceptronClassifier(lr=0.1, shuffle=False, deterministic=10)    #initialize perceptron with settings
    # PClass = PerceptronClassifier(lr=0.1, shuffle=False)
    PClass = PerceptronClassifier(lr=0.1, shuffle=True)

    row, col = data.shape  # using all zeros initial weight, if not provided, it will do random
    initial_weight = np.zeros((col + 1, 1))
    PClass.fit(X_train, y_train, initial_weight)  # train the perceptron
    # PClass.fit(data, labels, initial_weight)

    # graph(data[:,0].reshape(-1,1),data[:,1].reshape(-1,1),labels, "linearly separable")    # plotting scatter plot
    # index = list(range(1, PClass.num_epoch + 1))      # plotting scatter plot with each epoch's error during training
    # plt.scatter(index, PClass.epoch_error)
    # plt.plot(index, PClass.epoch_error)
    # plt.show()
    # y = lambda x: -1.3333* x              # plotting the line
    # y = lambda x: 0.8181 * x
    # graph_function(y)

    Accuracy = PClass.score(X_eval, y_eval)
    # Accuracy = PClass.score(data, labels)
    print("Accuracy = [{:.2f}]".format(Accuracy))
    weights = PClass.get_weights()
Ejemplo n.º 13
0
import sys

sys.path.append('../')
from tools import arff
from perceptron import PerceptronClassifier
# arff_file = "data_banknote_authentication.arff"
# arff_file = "dataset_1.arff"
# arff_file = "dataset_2.arff"
arff_file = "voting_data.arff"
mat = arff.Arff(arff_file)
np_mat = mat.data
data = mat[:, :-1]
labels = mat[:, -1].reshape(-1, 1)

#### Make Classifier ####
P2Class = PerceptronClassifier(None, lr=0.1, shuffle=True)
X_train, y_train, X_test, y_test = P2Class._train_test_split(data, labels, 70)
P2Class.fit(X_train, y_train)

misclassifications = P2Class.misclassifications
print('Train split accuracy: {}'.format(P2Class.score(X_train, y_train)))
print('Test split accuracy: {}'.format(P2Class.score(X_test, y_test)))
print("Final Weights =", P2Class.get_weights())
for i in range(len(misclassifications)):
    print('epoch: {}, {}'.format(i + 1, misclassifications[i]))

# Accuracy = P2Class.score(data,labels)
# print("Accuray = [{:.2f}]".format(Accuracy))
Ejemplo n.º 14
0
                    i, 0] > net2[i, 0]:
                correct += 1
    return correct / row


if __name__ == "__main__":
    mat = Arff("iris.arff", label_count=1)
    data = mat.data[:, 0:-1]
    labels = mat.data[:, -1].reshape(-1, 1)
    X_train, y_train, X_eval, y_eval = split(data, labels)

    P1 = PerceptronClassifier(lr=0.1, shuffle=True)  # train each perceptron
    y = process_label(y_train, 0)
    row, col = X_train.shape
    initial_weight = np.zeros((col + 1, 1))
    P1.fit(X_train, y, initial_weight)
    print("P1 training accuracy = ", P1.score(X_train, y))

    P2 = PerceptronClassifier(lr=0.1, shuffle=True)
    y = process_label(y_train, 1)
    row, col = X_train.shape
    initial_weight = np.zeros((col + 1, 1))
    P2.fit(X_train, y, initial_weight)
    print("P2 training accuracy = ", P2.score(X_train, y))

    P3 = PerceptronClassifier(lr=0.1, shuffle=True)
    y = process_label(y_train, 2)
    row, col = X_train.shape
    initial_weight = np.zeros((col + 1, 1))
    P3.fit(X_train, y, initial_weight)
    print("P3 training accuracy = ", P3.score(X_train, y))