Ejemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 5
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.º 6
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.º 7
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.º 8
0
from tools import arff
from perceptron import PerceptronClassifier
# arff_file = "data_banknote_authentication.arff"
arff_file = "dataset_1.arff"
# arff_file = "dataset_2.arff"
mat = arff.Arff(arff_file)
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())

import numpy as np
import matplotlib
from matplotlib import pyplot as plt
import scipy
from sklearn import svm

x = data[:, 0]
y = data[:, 1]
labels_flat = np.ndarray.flatten(labels)
weights = P2Class.get_weights()

plt.scatter(x, y, c=labels_flat)

dec_x = [0, (-weights[-1]) / weights[1]]
Ejemplo n.º 9
0
    print(Accuracy)


# Again, the learning rate had almost no impact

# In[11]:


data = nonLin[:,:-1]
labels = nonLin[:,-1:]

PClass = PerceptronClassifier(lr=.1,shuffle=True,deterministic=0)
PClass.fit(data,labels)
Accuracy = PClass.score(data,labels)
print(Accuracy)
w = PClass.get_weights()


# In[12]:


# Non-Linearly Separable
x = np.linspace(-1, 1, 100)
w1,w2,b = w
plt.title("Non-Linearly Seperable")
plt.ylabel("Y: Net Promoter Score")
plt.xlabel("X: pH of Coffee")
plt.scatter(nonLin[:,0],y=nonLin[:,1],c=nonLin[:,2])
plt.plot(x, (-w1/w2)*x + (-b / w2))
plt.ylim([-1,1])
plt.show()
Ejemplo n.º 10
0
    # 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()
    np.set_printoptions(precision=4)
    print("Final Weights = ", weights)
    print("epochs = ", PClass.num_epoch)

    # df = pd.DataFrame(PClass.epoch_error)             # write to excel codes
    # filepath = '1.csv'
    # df.to_csv(filepath, index=True)