Example #1
0
def build_bt_tree(labels , multiclass_dataset , k):
    if len(labels) == 1:
        return Binary_Tournament_TreeNode(None , labels)

    break_idx = len(labels) // 2
    l = labels[0 : break_idx]
    r = labels[break_idx : len(labels)]

    D_l = []
    D_r = []
    for labeledSample in multiclass_dataset.training_set:
        if labeledSample.label in l:
            s = data.LabeledSample(+1 , labeledSample.features)
            D_l.append(s)

        elif labeledSample.label in r:
            s = data.LabeledSample(-1 , labeledSample.features)
            D_r.append(s)

    D = D_l + D_r
    D_dataset = data.NumericalDataset( D , multiclass_dataset.validation_set  , multiclass_dataset.testing_set , multiclass_dataset.metadata)

    binary_model = perceptron.train(D_dataset, k)

    node = Binary_Tournament_TreeNode(binary_model , labels)
    node.left  = build_bt_tree(l , multiclass_dataset , k)
    node.right = build_bt_tree(r , multiclass_dataset , k)

    return node
Example #2
0
def main(options, args):
    logging.info("processing started ...")
    training_document = Prepositions(options.filename)
    testing_document = Prepositions(options.testfile)
    baseline1_document = Baseline1(options.filename)
    baseline2_document = Baseline2(options.filename)

    training_set = training_document.get_labeled_featureset()
    classifier = perceptron.train(training_set)

    testing_set = testing_document.get_labeled_featureset()
    logging.info("Accuracy of the Perceptron Classifier: %f", nltk.classify.accuracy(classifier, testing_set))

    errors = open("errors", "a")
    for fs, label in testing_set:
        if classifier.classify(fs) != label:
            errors.write(str((fs, label)) + "\n")

    logging.info("Training NaiveBayes classifier")
    bayes_classifier = nltk.NaiveBayesClassifier.train(training_set)
    logging.info("Accuracy of the NaiveBayes Classifier: %f", nltk.classify.accuracy(bayes_classifier, testing_set))

    logging.info("Accuracy of the Baseline1: %f", nltk.classify.accuracy(baseline1_document, testing_set))

    logging.info("Accuracy of the Baseline2: %f", nltk.classify.accuracy(baseline2_document, testing_set))
Example #3
0
def simulate_skin(steps=5, max_iter=100, learning_rate=0.1):
    """Simulate learning skin data set."""
    data = read_data('Skin_NonSkin.txt')
    train_data, test_data = split_list(data, 0.75)

    start = len(train_data)/steps  # First step training set size.
    end = len(train_data)  # Final step training set size.

    sizes = []  # Training data set sizes.
    success = []  # Success rates according to training data set sizes.
    for i in xrange(steps):
        # Increase training data size according to iteration.
        size = start + i*end/steps
        current_train_data = train_data[:size]

        w = train(current_train_data, max_iter=max_iter, r=learning_rate)
        error = test(test_data, w)

        status(current_train_data, test_data, error)
        print

        # Record size-success statistics.
        sizes.append(size)
        success.append(100 - error)

    plot_success_per_size(sizes, success)
    show()
Example #4
0
def main(options, args):
    logging.info("processing started ...")
    training_document = Prepositions(options.filename)
    testing_document = Prepositions(options.testfile)
    baseline1_document = Baseline1(options.filename)
    baseline2_document = Baseline2(options.filename)

    training_set = training_document.get_labeled_featureset()
    classifier = perceptron.train(training_set)

    testing_set = testing_document.get_labeled_featureset()
    logging.info("Accuracy of the Perceptron Classifier: %f",
                 nltk.classify.accuracy(classifier, testing_set))

    errors = open('errors', 'a')
    for fs, label in testing_set:
        if classifier.classify(fs) != label:
            errors.write(str((fs, label)) + '\n')

    logging.info("Training NaiveBayes classifier")
    bayes_classifier = nltk.NaiveBayesClassifier.train(training_set)
    logging.info("Accuracy of the NaiveBayes Classifier: %f",
                 nltk.classify.accuracy(bayes_classifier, testing_set))

    logging.info("Accuracy of the Baseline1: %f",
                 nltk.classify.accuracy(baseline1_document, testing_set))

    logging.info("Accuracy of the Baseline2: %f",
                 nltk.classify.accuracy(baseline2_document, testing_set))
def run(weights, bias, training_list, training_results, test_data, test_res):
    training_range = len(training_list) - 1
    for itr in range(0, training_range):
        inputs = training_list[itr]
        weights, bias = train(training_list[itr], training_results[itr],
                              weights, sign, bias)

    return (weights, bias)
Example #6
0
def simulate_seperable(data_size):
    """Simulate learning a completely seperable data set."""
    data = generate_sphere_data(10000, margin=0)
    train_data, test_data = split_list(data, 0.75)
    w = train(train_data, max_iter=500, r=0.01)
    error = test(test_data, w)
    status(train_data, test_data, error)

    plot_data(data)
    plot_w(data, w)
    show()
def perceptronModel():
    try:
        x_train, y_train, isBiased, learningRate, epochNum, x_test, y_test = modelOperations(
            'perceptron')

        W = perceptron.train(x_train, y_train, isBiased, learningRate,
                             epochNum)

        labels = [firstClassCB.get(), secondClassCB.get()]
        perceptron.test(x_test, y_test, W, labels)
        # show confusion matrix and accuracy
    except:
        pass
Example #8
0
def simulate_increasing(data_size, margin=0.3, max_iter=100, learning_rate=0.1,
                        steps=5, start=None, end=None):
    """Simulate learning an increasing training data set.

    Generates an unseperable data set, and trains on an increasing training
    set, then tests and plots.

    start: Initial (first step) training data set size.
    end: Final (last step) training data set size.
    """
    data = generate_sphere_data(data_size, margin=margin)
    train_data, test_data = split_list(data, 0.75)

    # Initialize start/end sizes if not given.
    start = len(train_data)/steps if start is None else start
    end = len(train_data) if end is None else end

    w_colors = ['b', 'c', 'm', 'y', 'k']  # w vector (line) graph color.
    w_gs = []  # w plot graphs.
    sizes = []  # Training data set sizes.
    success = []  # Success rates according to training data set sizes.
    for i in xrange(steps):
        # Increase training data size according to iteration.
        size = start + i*end/steps
        current_train_data = train_data[:size]

        w = train(current_train_data, max_iter=max_iter, r=learning_rate)
        error = test(test_data, w)

        status(current_train_data, test_data, error)
        print

        # Record size-success statistics.
        sizes.append(size)
        success.append(100 - error)

        # Plot decision boundary.
        w_color = w_colors[i] if i < len(w_colors) else w_colors[-1]
        figure(0)
        g, = plot_w(current_train_data, w, color=w_color)
        w_gs.append(g)

    figure(0).suptitle('Test data size: %d\nMaximum iterations: %d' % (len(test_data), max_iter))
    plot_w_legend(w_gs, sizes)
    plot_data(data)

    figure(1).suptitle('Success rate according to training set size.')
    plot_success_per_size(sizes, success)

    show()
def main():
    training = read_data(argv[1])
    test = read_data(argv[2])

    filter_modes = ["unfiltered", "filtered"]
    iteration_limits = [10, 25, 50, 100]
    training_rates = [0.01, 0.05, 0.1, 0.5, 0.8]
    for mode in filter_modes:
        for limit in iteration_limits:
            for rate in training_rates:
                weights = perceptron.train(training[mode], limit, rate)
                accuracy = perceptron.test(test[mode], weights) * 100
                print("{0:.6f}".format(accuracy) + "% accurate:", mode,
                      "stop words,", limit, "iterations,", rate,
                      "training rate")
Example #10
0
def main():
    # notice that, for your convenience, this script hard-codes
    # the loading of "mystery-dataset.pickle", since you'll need
    # to hardcode the transformation in MysteryTransform above
    # anyway.
    if len(sys.argv) < 2:
        print("Usage: %s number_of_passes" % sys.argv[0])
        sys.exit(1)
    dataset = (pickle.load(open("mystery-dataset.pickle",
                                "rb")).convert_labels_to_numerical(
                                    "y").convert_features_to_numerical())
    # this is implemented in `transform.py`
    dataset = MysteryTransform().transform_dataset(dataset)
    k = int(sys.argv[1])
    model = train(dataset, k)
    evaluate_model(model, dataset)
Example #11
0
def ava_train(multiclass_dataset , k):
    # Write this code! It should return a model with the same API as your
    # previous models, but should use models obtained from `perceptron.train`

    # your general strategy will be to create transformations that
    # will convert your multiclass dataset to a number of different
    # two-class datasets, train these, and then at test time, you
    # will need to run the binary classifiers, combine their results
    # appropriately, and produce a final preduction.
	
    model = AVA()

    labels = []
    for labeledSample in multiclass_dataset.training_set:
        labels.append(labeledSample.label)
    model.labels = list(set(labels))

    for i in range(len(model.labels)-1):

        D_pos = []
        for labeledSample in multiclass_dataset.training_set:
            if labeledSample.label == model.labels[i]:
                l = data.LabeledSample(+1 , labeledSample.features)
                D_pos.append(l)

        for j in range(i+1 , len(model.labels)):
            
            D_neg = []
            for labeledSample in multiclass_dataset.training_set:
                if labeledSample.label == model.labels[j]:
                    l = data.LabeledSample(-1 , labeledSample.features)
                    D_neg.append(l)

            D_bin = D_pos + D_neg
            D_bin_dataset = data.NumericalDataset( D_bin , multiclass_dataset.validation_set  , multiclass_dataset.testing_set , multiclass_dataset.metadata)
            #D_bin_dataset.is_numerical = multiclass_dataset.is_numerical

            binary_model = perceptron.train(D_bin_dataset, k)
            model.f[(i,j)] = binary_model

    return model
Example #12
0
def ova_train(multiclass_dataset, k):
    # Write this code! It should return a model with the same API as your
    # previous models, but should use models obtained from `perceptron.train`

    # your general strategy will be to create transformations that
    # will convert your multiclass dataset to a number of different
    # two-class datasets, train these, and then at test time, you
    # will need to run the binary classifiers, combine their results
    # appropriately, and produce a final preduction.

    model = OVA()

    labels = []
    for labeledSample in multiclass_dataset.training_set:
        labels.append(labeledSample.label)
    model.labels = list(set(labels))

    for i in range(len(model.labels)):
        dataset = multiclass_dataset.convert_labels_to_numerical(
            model.labels[i])
        binary_model = perceptron.train(dataset, k)
        model.binary_models.append(binary_model)

    return model
Example #13
0
from perceptron import train

train([[1, 4], [-1, 2], [1, 7], [-1, 9]], [[1], [-1], [1], [-1]])
Example #14
0
    for i in range(n_labels):
        result[i][Y[i]] = 1
    return result


# Uncomment one of the next three lines to decide which dataset to load
x1, x2, y = np.loadtxt('linearly_separable.txt', skiprows=1, unpack=True)
# x1, x2, y = np.loadtxt('non_linearly_separable.txt', skiprows=1, unpack=True)
# x1, x2, y = np.loadtxt('circles.txt', skiprows=1, unpack=True)

X_train = X_test = prepend_bias(np.column_stack((x1, x2)))
Y_train_unencoded = Y_test = y.astype(int).reshape(-1, 1)
Y_train = one_hot_encode(Y_train_unencoded)
w = perceptron.train(X_train,
                     Y_train,
                     X_test,
                     Y_test,
                     iterations=10000,
                     lr=0.1)


# Generate a mesh over one-dimensional data
# (The mesh() and plot_boundary() functionality were inspired by the
# documentation of the BSD-licensed scikit-learn library.)
def mesh(values):
    range = values.max() - values.min()
    padding_percent = 5
    padding = range * padding_percent * 0.01
    resolution = 1000
    interval = (range + 2 * range * padding) / resolution
    return np.arange(values.min() - padding, values.max() + padding, interval)
Example #15
0
            #	iris_data.append([data[:4], 3])
    return iris_data


# instance perceptron
perceptron = perceptron.Perceptron()
perceptron.input_length = 4  # set input length

# prepare dataset
iris_dataset = generate_iris_dataset()
ts_input_iris = np.array([specs[0] for specs in iris_dataset])  # input data
ts_output_iris = np.array([specs[1]
                           for specs in iris_dataset])  # expected output data

# train
perceptron.train(ts_input_iris, ts_output_iris)

# test al dataset


def test_all():
    for ts_input, expected in zip(ts_input_iris, ts_output_iris):
        output = perceptron.predict(ts_input)
        expected = 'OK' if expected == output else 'FAIL'
        iris_type_name = "Iris-setosa" if output == 1 else "Iris-versicolor"
        print(f'Input:{ts_input} Output: {output} = {iris_type_name}')


# you can test a iris measurements
p_input = [5.9, 3.0, 4.2, 1.0]
if (perceptron.predict(p_input) == 1):
Example #16
0
import perceptron
import csvconv

#inputs
lr = float(input("Input Learning Rate:"))
Epochs = int(input("Input Number Of Epochs:"))

#training
floatdata = csvconv.tofloat('train.csv')
weights = perceptron.train(floatdata, lr, Epochs)
print("Weights\n ", weights)

#testing
floattest = csvconv.tofloat('test.csv')
score = perceptron.test(floattest, weights)
print('Score=', score, '/', len(floattest))
acc = (score / len(floattest)) * 100
print("Accuracy=", acc)
Example #17
0
data = np.c_[xx.ravel(), yy.ravel()]
# Dans le format de votre classifier

TP_perceptron = []
FP_perceptron = []
TP_KNNWeight = []
FP_KNNWeight = []
TP_KNN = []
FP_KNN = []
TP_NN = []
FP_NN = []
Xtr, ytr, Xte, yte = split(X, y)

for k in range(len(np.unique(yte, return_counts=False))):
    labels_k = perceptron.two_classes(ytr, k)
    weights, errors = perceptron.train(Xtr, labels_k, with_errors=True)
    pred = np.array([perceptron.predict(weights, x) for x in Xte])
    TP_perceptron.append(true_positive_perceptron(pred, yte, k))
    FP_perceptron.append(false_negative_perceptron(pred, yte, k))
    # For each class we have a TP and FP

S = [0.1, 0.2, 0.5, 1, 2, 5]
K = [2, 3, 4, 5, 10, 15]
z1 = predict(Xte, Xtr, ytr)

TP_NN.append(true_positive(z1, yte))
FP_NN.append(false_negative(z1, yte))
s = 0.1
k = 10

Z1 = []
    D = 2
    w1 = [[-1, -1]]  #
    w2 = [[0, 0]]  #
    w3 = [[1, 1]]
    y2 = [-1] * len(w2)
    y3 = [-1] * len(w3)
    #choose w1 as positive samples
    y1 = [1] * len(w1)
    X = np.concatenate((w1, w2, w3), axis=0)
    b = np.ones(len(X))
    b.shape = (len(b), 1)
    X = np.concatenate((X, b), axis=1)
    Y = np.concatenate((y1, y2, y3), axis=0)

    theta1 = np.random.normal(0, 0.01, 1 + D)  # parameters
    train(X, Y, theta1)
    print 'theta1:', theta1
    #choose w2 as positive samples
    y1 = [-1] * len(w1)
    y2 = [1] * len(w2)
    Y = np.concatenate((y1, y2, y3), axis=0)
    theta2 = np.random.normal(0, 0.01, 1 + D)
    train(X, Y, theta2)
    print 'theta2:', theta2
    #choose w3 as positive samples
    y2 = [-1] * len(w2)
    y3 = [1] * len(w3)
    Y = np.concatenate((y1, y2, y3), axis=0)
    theta3 = np.random.normal(0, 0.01, 1 + D)
    train(X, Y, theta3)
    print 'theta3:', theta3
def runTrials(trial, trainingDigits, testingDigits, testingLabelsData , flattenedTestDigits, percent):
    

    testIndices = []
    guesses = []
    #Start timing bayes
    bayesStart = time.process_time()
    learningDigits = classifier.pickData(trainingDigits, percent)

    formattedDigits = classifier.getFormattedTraining(learningDigits)
            
    condProbCounters = classifier.getCondProbs(learningDigits)
    for i in range(0, len(testingDigits)):
        

        testIndices.append(i)
        #lp = LineProfiler()
        #lp_wrapper = lp(classifier.naiveBayes)
        #guesses.append(lp_wrapper(i, testingDigits, formattedDigits, percent, condProbCounters))
        #lp.print_stats()
        guesses.append(classifier.naiveBayes(i, testingDigits, formattedDigits, percent, condProbCounters))
    
    #End timing bayes
    bayesEnd = time.process_time()
    bayesTrainingTime = bayesEnd - bayesStart


    numCorrect = 0
    for tup in guesses:
        digitIndex = tup[0]
        guess = tup[1]
        if( int(guess) == int(testingLabelsData[digitIndex])):
            numCorrect+=1
    accBayes = (numCorrect/len(testingDigits)) * 100
    print("Accuracy for Naive Bayes classifier trial: " + str(trial) + " is: " + str(accBayes))

    #Begin perceptron training and prediction:
    numPixels = classifier.digitRowLen * classifier.digitColLen
    WVectors = perceptron.initializeWeightVecs(numPixels)

    
    #Start training time for perceptron
    startTime = time.process_time()
    for iteration in range(0, maxIter):
        #pool.starmap(perceptron.train, zip(itertools.repeat(WVectors), itertools.repeat(trainingDigits), itertools.repeat(percent)))
        perceptron.train(WVectors, trainingDigits, percent, iteration)

    #End training time
    endTime = time.process_time()
    perceptronTrainingTime = endTime - startTime 

    numCorrect = 0
    for k in range(0, len(flattenedTestDigits)):
	    testDigit = flattenedTestDigits[k]
	    guess = perceptron.predict(WVectors, testDigit)
	    if(guess == int(testingLabelsData[k])):
		    numCorrect+=1
    acc = (numCorrect/len(testingDigits)) * 100
    print("Accuracy for Perceptron trial: " + str(trial) + " is: " + str(acc))

        
    percentFolderPath = "{0:d} percent/".format(int(percent*100))


    with open("trainingData/" + percentFolderPath + "output{0:d}.txt".format(trial), "w+") as f:
            with redirect_stdout(f):
                print("%s %s %s %s" % ("Bayes Acc: ", str(accBayes) , "Bayes training time: ", str(bayesTrainingTime)))
                print("%s %s %s %s" % ("Percep Acc: ", str(acc), "Percep training time: ", str(perceptronTrainingTime)))
Example #20
0
from perceptron import train, predict

# We can use any set of Xs and Ys to fit the perceptron
# A perceptron training will only converge if the relationship between X&Y is linear
# (i.e., linearly separable data)

# Data given for the assignment
X = [[0.25, 0.353], [0.25, 0.471], [0.5, 0.353], [0.5, 0.647], [0.75, 0.705],
     [0.75, 0.882], [1, 0.705], [1, 1]]
Y = [0, 1, 0, 1, 0, 1, 0, 1]

W, b = train(X, Y, learning_rate=0.1)

# Checking if the converged values are correct
print('Prediction is correct?', predict(X, W, b) == Y)
Example #21
0
    dataset_C = pd.read_csv("../../dataset/processed/C.tsv",
                            header=0,
                            delimiter="\t",
                            quoting=3)
    # dataset_D = pd.read_csv( "../../dataset/processed/D.tsv", header=0, delimiter="\t", quoting=3 )
    # dataset_E = pd.read_csv( "../../dataset/processed/E.tsv", header=0, delimiter="\t", quoting=3 )
    print("Loaded data.")

    training_set = list(dataset_A.to_numpy())
    dev_set = list(dataset_B.to_numpy())

    X_train, Y_train = reviews_to_features(training_set)
    print("Featurized training data.")

    weights, losses = perceptron.train(X_train,
                                       Y_train,
                                       iterations=10000,
                                       eta=0.1)
    print("Done training.")

    X_test, Y_test = reviews_to_features(dev_set)
    print("Featurized test data.")

    test_scores = perceptron.score(X_test.T, weights)
    test_sentiments = perceptron.predict(test_scores)
    (accuracy, recall, precision, f1, false_positive_rate,
     false_negative_rate) = perceptron.test(Y_test, test_sentiments)
    print("Predicted scores w/ threshold = 0.5:")
    print(f"  - accuracy      : {accuracy}")
    print(f"  - recall        : {recall}")
    print(f"  - precision     : {precision}")
    print(f"  - f1            : {f1}")
Example #22
0
from matplotlib import pyplot

import perceptron

n = 10
d = 2

data0 = np.random.randn(n, d) + 3
data1 = np.random.randn(n, d) - 3
data = np.concatenate([data0, data1])

labels = np.concatenate([np.zeros(n),
                         np.ones(n)])  # Deux classes etiquettees 0 et 1
labels = perceptron.two_classes(labels, 0)  # Deux classes etiquettees -1 et 1

weights, errors = perceptron.train(data, labels, with_errors=True)
print(weights)

for i in range(data.shape[0]):
    print(i, labels[i], perceptron.predict(weights, data[i]), data[i])

pyplot.scatter(data0[:, 0], data0[:, 1], marker="x", color="r", s=100)
pyplot.scatter(data1[:, 0], data1[:, 1], marker="*", color="b", s=100)
x0 = 0
y0 = -weights[2] / weights[1]
x1 = -weights[2] / weights[0]
y1 = 0
a = (y1 - y0) / (x1 - x0)
b = y0
pyplot.plot([-10, +10], [-10 * a + b, +10 * a + b], color="g")
pyplot.xlim(-6, 6)
Example #23
0
"""

from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import numpy as np
import perceptron
import utils

if __name__ == "__main__":
    iris = load_iris()
    data = iris.data[:, [0, 3]]
    data = data[0:100]
    target = iris.target[0:100]
    number_of_epochs = 100000
    learning_rate = 0.01
    w = perceptron.train(data, target, number_of_epochs, learning_rate)
    line_xs, line_ys = utils.getLineFromWeights(w, np.min(data, axis=0), np.max(data, axis=0))
    # class 0
    data_0 = data[:50, :]
    # class 1
    data_1 = data[50:, :]
    fig, x = plt.subplots(1)
    x.plot(data_0[:, 0], data_0[:, 1], 'r+')
    x.plot(data_1[:, 0], data_1[:, 1], 'bo')
    # drawin hyperplane
    l = mlines.Line2D(line_xs, line_ys)
    x.add_line(l)
    x.plot()
    plt.show()
Example #24
0
lines = read_instances("spam_train.txt")

print "Counting words..."
vocabulary = build_vocabulary(lines)

print "Building labels..."
desired_outputs = build_labels(lines)

print "Filtering instances..."
vocabulary = Counter({w: c for w, c in vocabulary.iteritems() if c >= 30})

print "Buiding feature vectors..."
feature_vectors = build_features(lines, vocabulary)

print "Calling perceptron train"
(w, k, it) = perceptron.train(feature_vectors, desired_outputs,
                              PERCEPTRON_MAX_ITER)

print "Number of iterations (perceptron):", it

print "Number of mistakes (perceptron):", k

print "15 most positive words (perceptron):", w.most_common(15)

print "15 most negative words: (perceptron)", w.most_common()[:-15 - 1:-1]

print "Calling pegasos train"

# Test with array of lambdas or hardcoded one

l = range(-9, 9)
ws = [pegasos.train(feature_vectors, desired_outputs, 2**x) for x in l]
Example #25
0
from perceptron import train, plot
from ImagenPuntos import AbrirImagen

root_path = "D:\\Facultad\\Catedras\\UBA\\Teorías\\02 - Perceptrón\\Python\\"

file_path = root_path + 'Imagen 1.bmp'

X = AbrirImagen(file_path)
column_count = 3

T = X[:, column_count - 1]
P = X[:, 0:(column_count - 1)]

alfa = 0.1
max_ite = 300

(W, b, ite) = train(P, T, alfa, max_ite, False)
print(ite)

plot(P, T, W, b)
Example #26
0
# Save data to temporary file (in case of unexpected close)
with open('tmpfile', 'wb+') as f:
    pickle.dump(data, f)

debug_print("Training")

w = None
try:
    with open(args.output, 'rb') as f:
        weights = pickle.load(f)
        debug_print("Continuing training on weights (remove the file '%s' to start from stratch)" % str(args.output))
except:
    w = None

if args.max_iterations == None:
    weights = perceptron.train(data, args.learning_rate, args.error, -1, args.debug, w)
else:
    weights = perceptron.train(data, args.learning_rate, args.error, args.max_iterations, args.debug, w)

if args.output != None:
    with open(args.output, 'wb') as f:
        pickle.dump(weights, f)
    norm_squared = perceptron.dot(weights, weights)
    debug_print("Weights vector norm squared: %d (if this is 0 then something is wrong)" % norm_squared)
else:
    import pprint

    pp = pprint.PrettyPrinter()

    pp.pprint(weights)
Example #27
0
import pandas as pd
import numpy as np
from perceptron import train, perceive

columns = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'class']

train_data = pd.read_csv("data/iris.data", header=None)
train_data.columns = columns

class_map = {'Iris-setosa': 0, 'Iris-versicolor': 1}
p = train(train_data.ix[:,0:4].values, train_data.ix[:,4].map(class_map))

test_data = pd.read_csv("data/test_iris.data", header=None)
test_data.columns = columns

for vec, target in zip(test_data.ix[:,0:4].values, test_data.ix[:,4].map(class_map)):
    print "Perceived:", perceive(p, vec), "Actual:", target
Example #28
0
from perceptron import train, plot
from ImagenPuntos import AbrirImagen


file_path = "Imagen.bmp"

X = AbrirImagen(file_path)
column_count = 3;

T = X[:, column_count - 1]
P = X[:, 0:(column_count - 1)]

alfa = 0.1
max_ite = 300

(W, b, ite) = train(P, T, alfa, max_ite, True)
print(ite)

plot(P, T, W, b)

Example #29
0
import random, numpy, csv
import perceptron


def load_csv(filename):
    file = open(filename, "rb")
    lines = csv.reader(file)
    dataset = list(lines)
    random.shuffle(dataset)
    return dataset


inputs = load_csv("iris.csv")
weights = [numpy.random.random_sample() for i in range(len(inputs))]
bias = 0
learning_rate = 0.01

weights = perceptron.train(inputs, weights, bias, learning_rate)
c = perceptron.test(inputs, weights, bias)
print c
Example #30
0
                            delimiter="\t",
                            quoting=3)
    # dataset_D = pd.read_csv( "../../dataset/processed/D.tsv", header=0, delimiter="\t", quoting=3 )
    # dataset_E = pd.read_csv( "../../dataset/processed/E.tsv", header=0, delimiter="\t", quoting=3 )

    training_set = list(dataset_A.to_numpy())
    dev_set = list(dataset_B.to_numpy())
    test_set = list(dataset_C.to_numpy())

    print("Loaded data.")

    X_train, Y_train = reviews_to_features(training_set)
    print("Featurized training data.")

    weights, losses = perceptron.train(X_train,
                                       Y_train,
                                       iterations=ITERATIONS,
                                       eta=ETA)
    print("Done training.")

    X_test, Y_test = reviews_to_features(dev_set)
    print("Featurized test data.")

    test_scores = perceptron.score(X_test.T, weights)
    test_sentiments = perceptron.predict(test_scores)
    (accuracy, recall, precision, f1, false_positive_rate,
     false_negative_rate) = perceptron.test(Y_test, test_sentiments)
    print(
        f"Predicted scores w/ threshold = 0.5, iterations = {ITERATIONS}, eta = {ETA}:"
    )
    print(f"  - accuracy      : {accuracy}")
    print(f"  - recall        : {recall}")
Example #31
0
__author__ = 'juan pablo isaza'
from util import *
import constants as cts
import MyData
import perceptron
from pandas import *

# read: http://svivek.com/teaching/structured-prediction/fall2014/lectures.html

train_data = MyData.generate_toy(cts.num_x)

w_learned = perceptron.train(train_data, learning_rate=.8)

prediction = DataFrame(get_labels(x_data=train_data.as_matrix(columns=get_feature_names(cts.x, cts.num_x))
                                  , w=w_learned))

train_error = get_error(labels=train_data.loc[:, get_feature_names(cts.c, cts.num_c)],
                        prediction=prediction)


print("Train error is = " + str(train_error))
MyData.plot_all(w_learned, train_data)