Exemple #1
0
def test_perceptron(sd):
    perc = percc.Perceptron()
    params_perc_sd = perc.train(sd.train_X,sd.train_y)

    y_pred_train = perc.test(sd.train_X,params_perc_sd)
    acc_train = perc.evaluate(sd.train_y, y_pred_train)
    assert allclose(acc_train, 0.960000, tolerance)

    y_pred_test = perc.test(sd.test_X,params_perc_sd)
    acc_test = perc.evaluate(sd.test_y, y_pred_test)
    assert allclose(acc_test, 0.960000, tolerance)
def run_all_classifiers(dataset):
    fig, axis = dataset.plot_data()
    print "Naive Bayes"
    nb = nbc.NaiveBayes()
    params_nb = nb.train(dataset.train_X, dataset.train_y)
    print params_nb.reshape(-1)
    predict = nb.test(dataset.train_X, params_nb)
    evaluation = nb.evaluate(predict, dataset.train_y)
    predict2 = nb.test(dataset.test_X, params_nb)
    evaluation2 = nb.evaluate(predict2, dataset.test_y)
    print "Accuracy train: %f test: %f" % (evaluation, evaluation2)
    fig, axis = dataset.add_line(fig, axis, params_nb, "Naive Bayes", "red")

    print "Perceptron"
    perc = percc.Perceptron()
    params_perc = perc.train(dataset.train_X, dataset.train_y)
    print params_perc.reshape(-1)
    predict = perc.test(dataset.train_X, params_perc)
    evaluation = perc.evaluate(predict, dataset.train_y)
    predict2 = perc.test(dataset.test_X, params_perc)
    evaluation2 = perc.evaluate(predict2, dataset.test_y)
    print "Accuracy train: %f test: %f" % (evaluation, evaluation2)
    fig, axis = dataset.add_line(fig, axis, params_perc, "Perceptron", "blue")

    print "MaxEnt LBFGS"
    me = mec_batch.MaxEnt_batch()
    params_me = me.train(dataset.train_X, dataset.train_y)
    print params_me.reshape(-1)
    predict = me.test(dataset.train_X, params_me)
    evaluation = me.evaluate(predict, dataset.train_y)
    predict2 = me.test(dataset.test_X, params_me)
    evaluation2 = me.evaluate(predict2, dataset.test_y)
    print "Accuracy train: %f test: %f" % (evaluation, evaluation2)
    fig, axis = dataset.add_line(fig, axis, params_me, "ME-LBFGS", "green")

    print "MaxEnt Online"
    me_online = mec_online.MaxEnt_online()
    params_me = me_online.train(dataset.train_X, dataset.train_y)
    print params_me.reshape(-1)
    predict = me_online.test(dataset.train_X, params_me)
    evaluation = me_online.evaluate(predict, dataset.train_y)
    predict2 = me_online.test(dataset.test_X, params_me)
    evaluation2 = me.evaluate(predict2, dataset.test_y)
    print "Accuracy train: %f test: %f" % (evaluation, evaluation2)
    fig, axis = dataset.add_line(fig, axis, params_me, "ME-Online", "pink")

    print "MIRA"
    mira = mirac.Mira()
    params_mira = mira.train(dataset.train_X, dataset.train_y)
    print params_mira.reshape(-1)
    predict = mira.test(dataset.train_X, params_mira)
    evaluation = mira.evaluate(predict, dataset.train_y)
    predict2 = mira.test(dataset.test_X, params_mira)
    evaluation2 = mira.evaluate(predict2, dataset.test_y)
    print "Accuracy train: %f test: %f" % (evaluation, evaluation2)
    fig, axis = dataset.add_line(fig, axis, params_mira, "Mira", "orange")

    print "SVM"
    svm = svmc.SVM()
    params_svm = svm.train(dataset.train_X, dataset.train_y)
    print params_svm.reshape(-1)
    predict = svm.test(dataset.train_X, params_svm)
    evaluation = svm.evaluate(predict, dataset.train_y)
    predict2 = svm.test(dataset.test_X, params_svm)
    evaluation2 = svm.evaluate(predict2, dataset.test_y)
    print "Accuracy train: %f test: %f" % (evaluation, evaluation2)
    fig, axis = dataset.add_line(fig, axis, params_svm, "SVM", "brown")
Exemple #3
0
# Use the learned parameters to predict labels for the test data
y_pred_test = gnb.test(sd.test_X, params_nb_sd)

# Compute accuracy on test data from predicted labels and true labels
acc_test = gnb.evaluate(sd.test_y, y_pred_test)

# Add a line to the plot with the line corresponding to the NB classifier
fig, axis = sd.add_line(fig, axis, params_nb_sd, "Naive Bayes", "red")

# Print these two accuracies to the terminal
print "Naive Bayes Simple Dataset Accuracy train: %f test: %f" % (acc_train,
                                                                  acc_test)
print

# Same as above, but for the perceptron classifier (instead of Naive Bayes)
perc = percc.Perceptron()
params_perc_sd = perc.train(sd.train_X, sd.train_y)
y_pred_train = perc.test(sd.train_X, params_perc_sd)
acc_train = perc.evaluate(sd.train_y, y_pred_train)
y_pred_test = perc.test(sd.test_X, params_perc_sd)
acc_test = perc.evaluate(sd.test_y, y_pred_test)
fig, axis = sd.add_line(fig, axis, params_perc_sd, "Perceptron", "blue")
print "Perceptron Simple Dataset Accuracy train: %f test: %f" % (acc_train,
                                                                 acc_test)
print

# Same as above, but for the MIRA classifier
mira = mirac.Mira()
params_mira_sd = mira.train(sd.train_X, sd.train_y)
y_pred_train = mira.test(sd.train_X, params_mira_sd)
acc_train = mira.evaluate(sd.train_y, y_pred_train)