示例#1
0
def checkp2():
    feature_matrix = np.array(
        [[
            0.26046531, -0.36308447, -0.01205413, 0.10234256, -0.15834628,
            -0.01892124, -0.3465143, -0.24222911, -0.35764308, -0.16018656
        ],
         [
             -0.17900963, 0.01272608, -0.06946181, 0.35967905, -0.25617767,
             0.25118557, -0.01246244, -0.31880532, 0.11798194, -0.27946061
         ],
         [
             -0.47883835, 0.01509427, 0.16158526, -0.42773632, -0.45076571,
             0.04753269, 0.10344044, -0.08028192, 0.2392796, 0.24580058
         ],
         [
             -0.00515502, -0.1248861, 0.39724835, 0.37006804, -0.20140247,
             -0.48839567, -0.28809722, -0.05218129, -0.07295099, 0.02184959
         ],
         [
             0.23336495, 0.16734786, -0.0663445, -0.34224209, -0.31104588,
             0.00237723, 0.42431789, 0.09560354, -0.19958805, 0.11698492
         ]])
    labels = [-1, 1, -1, 1, -1]
    T = 5
    p1.perceptron(feature_matrix, labels, T)
示例#2
0
文件: main.py 项目: yshen4/pymal
def problem5(T = 10, L = 0.2):
    toy_features, toy_labels = toy_data = utils.load_toy_data('toy_data.tsv')

    thetas_perceptron = p1.perceptron(toy_features, toy_labels, T)
    thetas_avg_perceptron = p1.average_perceptron(toy_features, toy_labels, T)
    thetas_pegasos = p1.pegasos(toy_features, toy_labels, T, L)

    plot_toy_results('Perceptron', thetas_perceptron, toy_features, toy_labels)
    plot_toy_results('Average Perceptron', thetas_avg_perceptron, toy_features, toy_labels)
    plot_toy_results('Pegasos', thetas_pegasos, toy_features, toy_labels)
    def test_algorithm_compare(self):
        # -------------------------------------------------------------------------------
        # # Problem 5
        # #-------------------------------------------------------------------------------

        toy_features, toy_labels = toy_data = utils.load_toy_data('toy_data.tsv')

        T = 100
        L = 0.2

        thetas_perceptron = p1.perceptron(toy_features, toy_labels, T)
        thetas_avg_perceptron = p1.average_perceptron(toy_features, toy_labels, T)
        thetas_pegasos = p1.pegasos(toy_features, toy_labels, T, L)

        def plot_toy_results(algo_name, thetas):
            print('theta for', algo_name, 'is', ', '.join(map(str, list(thetas[0]))))
            print('theta_0 for', algo_name, 'is', str(thetas[1]))
            utils.plot_toy_data(algo_name, toy_features, toy_labels, thetas)

        plot_toy_results('Perceptron', thetas_perceptron)
        plot_toy_results('Average Perceptron', thetas_avg_perceptron)
        plot_toy_results('Pegasos', thetas_pegasos)
        return
示例#4
0
dictionary = p1.bag_of_words(train_texts)

train_bow_features = p1.extract_bow_feature_vectors(train_texts, dictionary)
val_bow_features = p1.extract_bow_feature_vectors(val_texts, dictionary)
test_bow_features = p1.extract_bow_feature_vectors(test_texts, dictionary)
#
#-------------------------------------------------------------------------------
# Section 1.7
#-------------------------------------------------------------------------------
toy_features, toy_labels = toy_data = utils.load_toy_data('toy_data.tsv')

T = 5
L = 10

thetas_perceptron = p1.perceptron(toy_features, toy_labels, T)
thetas_avg_perceptron = p1.average_perceptron(toy_features, toy_labels, T)
thetas_avg_pa = p1.average_passive_aggressive(toy_features, toy_labels, T, L)


def plot_toy_results(algo_name, thetas):
    utils.plot_toy_data(algo_name, toy_features, toy_labels, thetas)


plot_toy_results('Perceptron', thetas_perceptron)
plot_toy_results('Average Perceptron', thetas_avg_perceptron)
plot_toy_results('Average Passive-Aggressive', thetas_avg_pa)
#-------------------------------------------------------------------------------
#
#
#-------------------------------------------------------------------------------
dictionary = p1.bag_of_words(train_texts)

train_bow_features = p1.extract_bow_feature_vectors(train_texts, dictionary)
val_bow_features = p1.extract_bow_feature_vectors(val_texts, dictionary)
test_bow_features = p1.extract_bow_feature_vectors(test_texts, dictionary)

#-------------------------------------------------------------------------------
# Problem 5
#-------------------------------------------------------------------------------

toy_features, toy_labels = toy_data = utils.load_toy_data('toy_data.tsv')

T = 10
L = 0.2

thetas_perceptron = p1.perceptron(toy_features, toy_labels, T)
thetas_avg_perceptron = p1.average_perceptron(toy_features, toy_labels, T)
#thetas_pegasos = p1.pegasos(toy_features, toy_labels, T, L)

def plot_toy_results(algo_name, thetas):
    print('theta for', algo_name, 'is', ', '.join(map(str,list(thetas[0]))))
    print('theta_0 for', algo_name, 'is', str(thetas[1]))
    utils.plot_toy_data(algo_name, toy_features, toy_labels, thetas)

plot_toy_results('Perceptron', thetas_perceptron)
plot_toy_results('Average Perceptron', thetas_avg_perceptron)
#plot_toy_results('Pegasos', thetas_pegasos)

#-------------------------------------------------------------------------------
# Problem 7
#-------------------------------------------------------------------------------
示例#6
0
import project1 as p1
from project1 import perceptron, average_perceptron, pegasos
import utils
import numpy as np
import numpy.testing as npt
import re

toy_features, toy_labels = toy_data = utils.load_toy_data('toy_data.tsv')
T = 10
L = 0.2

thetas = perceptron(toy_features, toy_labels, T)
print(thetas)
utils.plot_toy_data("Perceptron", toy_features, toy_labels, thetas)

thetas = average_perceptron(toy_features, toy_labels, T)
print(thetas)
utils.plot_toy_data("Average Perceptron", toy_features, toy_labels, thetas)

thetas = pegasos(toy_features, toy_labels, T, L)
print(thetas)
utils.plot_toy_data("Pegasos", toy_features, toy_labels, thetas)
示例#7
0
dictionary = p1.bag_of_words(train_texts)

train_bow_features = p1.extract_bow_feature_vectors(train_texts, dictionary)
val_bow_features = p1.extract_bow_feature_vectors(val_texts, dictionary)
test_bow_features = p1.extract_bow_feature_vectors(test_texts, dictionary)
#
#-------------------------------------------------------------------------------
# Section 1.7
#-------------------------------------------------------------------------------
toy_features, toy_labels = toy_data = utils.load_toy_data('toy_data.tsv')

T = 5
L = 10

thetas_perceptron = p1.perceptron(toy_features, toy_labels, T)
thetas_avg_perceptron = p1.average_perceptron(toy_features, toy_labels, T)
thetas_avg_pa = p1.average_passive_aggressive(toy_features, toy_labels, T, L)

def plot_toy_results(algo_name, thetas):
    utils.plot_toy_data(algo_name, toy_features, toy_labels, thetas)

plot_toy_results('Perceptron', thetas_perceptron)
plot_toy_results('Average Perceptron', thetas_avg_perceptron)
plot_toy_results('Average Passive-Aggressive', thetas_avg_pa)
#-------------------------------------------------------------------------------
#
#
#-------------------------------------------------------------------------------
# Section 2.9.b
#-------------------------------------------------------------------------------
示例#8
0
def checkperx():
    feature_matrix = np.array([[1, 2]])
    labels = np.array([1])
    T = 1
    p1.perceptron(feature_matrix, labels, T)