[-2,3], [2,4], [4,2], [-3,-2], [0,-2], [3,-3]]) loose_labels = np.array([1,1,1,1,-1,-1,-1]) average_theta, average_theta_0 = p1.averager(loose_points, loose_labels) p1.plot_2d_examples(loose_points, loose_labels, average_theta_0, average_theta, 'Averager - loose points') perceptron_theta, perceptron_theta_0 = p1.train_perceptron(loose_points, loose_labels) p1.plot_2d_examples(loose_points, loose_labels, perceptron_theta_0, perceptron_theta, 'Perceptron - loose points') pa_theta, pa_theta_0 = p1.train_passive_agressive(loose_points, loose_labels, 1000) p1.plot_2d_examples(loose_points, loose_labels, pa_theta_0, pa_theta, 'Passive Agressive - loose points') close_points = np.array([ [-1,-1.25], [-1.5, -1], [1,4], [1.5,1.5], [4,10], [-1,-1]]) close_labels = np.array([-1, 1, 1, -1, 1, -1]) average_theta, average_theta_0 = p1.averager(close_points, close_labels) p1.plot_2d_examples(close_points, close_labels, average_theta_0, average_theta, 'Averager - close points')
loose_points = np.array([[-3, 4], [-2, 3], [2, 4], [4, 2], [-3, -2], [0, -2], [3, -3]]) loose_labels = np.array([1, 1, 1, 1, -1, -1, -1]) average_theta, average_theta_0 = p1.averager(loose_points, loose_labels) p1.plot_2d_examples(loose_points, loose_labels, average_theta_0, average_theta, 'Averager - loose points') perceptron_theta, perceptron_theta_0 = p1.train_perceptron( loose_points, loose_labels) p1.plot_2d_examples(loose_points, loose_labels, perceptron_theta_0, perceptron_theta, 'Perceptron - loose points') pa_theta, pa_theta_0 = p1.train_passive_agressive(loose_points, loose_labels, 1000) p1.plot_2d_examples(loose_points, loose_labels, pa_theta_0, pa_theta, 'Passive Agressive - loose points') close_points = np.array([[-1, -1.25], [-1.5, -1], [1, 4], [1.5, 1.5], [4, 10], [-1, -1]]) close_labels = np.array([-1, 1, 1, -1, 1, -1]) average_theta, average_theta_0 = p1.averager(close_points, close_labels) p1.plot_2d_examples(close_points, close_labels, average_theta_0, average_theta, 'Averager - close points') perceptron_theta, perceptron_theta_0 = p1.train_perceptron( close_points, close_labels) p1.plot_2d_examples(close_points, close_labels, perceptron_theta_0,
print 'Size', len(dictionary) print 'T_0', perceptron_theta_0 correct = 0 for i in xrange(0, len(label_output)): if(label_output[i] == labels[i]): correct = correct + 1 percentage_correct = 100.0 * correct / len(label_output) print("Perceptron gets " + str(percentage_correct) + "% correct (" + str(correct) + " out of " + str(len(label_output)) + ").") ###################### # PASSIVE-AGRESSIVE ###################### pa_theta, pa_theta_0 = p1.train_passive_agressive(feature_matrix, labels, 50) label_output = p1.perceptron_classify(feature_matrix, pa_theta_0, pa_theta) correct = 0 for i in xrange(0, len(label_output)): if(label_output[i] == labels[i]): correct = correct + 1 percentage_correct = 100.0 * correct / len(label_output) print("Passive-agressive gets " + str(percentage_correct) + "% correct (" + str(correct) + " out of " + str(len(label_output)) + ").") # ###################### # # CROSS VALIDATION # ###################### K = 40 print 'K =', K
import numpy as np import project1_code as p1 ###################### # INITIALIZE ###################### adjectives = p1.extract_set('adjectives.txt') dictionary = p1.extract_dictionary('train-tweet.txt') train_labels = p1.read_vector_file('train-answer.txt') train_feature_matrix = p1.extract_feature_vectors_with_keywords( 'train-tweet.txt', dictionary, adjectives) test_feature_matrix = p1.extract_feature_vectors_with_keywords( 'test-tweet.txt', dictionary, adjectives) ###################### # TRAIN ###################### pa_theta, pa_theta_0 = p1.train_passive_agressive(train_feature_matrix, train_labels, 1000) ###################### # CLASSIFY ###################### label_output = p1.perceptron_classify(test_feature_matrix, pa_theta_0, pa_theta) print train_feature_matrix.shape print test_feature_matrix.shape p1.write_label_answer(label_output, 'tweet_labels.txt')
import numpy as np import project1_code as p1 ###################### # INITIALIZE ###################### adjectives = p1.extract_set('adjectives.txt') dictionary = p1.extract_dictionary('train-tweet.txt') train_labels = p1.read_vector_file('train-answer.txt') train_feature_matrix = p1.extract_feature_vectors_with_keywords('train-tweet.txt', dictionary, adjectives) test_feature_matrix = p1.extract_feature_vectors_with_keywords('test-tweet.txt', dictionary, adjectives) ###################### # TRAIN ###################### pa_theta, pa_theta_0 = p1.train_passive_agressive(train_feature_matrix, train_labels, 1000) ###################### # CLASSIFY ###################### label_output = p1.perceptron_classify(test_feature_matrix, pa_theta_0, pa_theta) print train_feature_matrix.shape print test_feature_matrix.shape p1.write_label_answer(label_output, 'tweet_labels.txt')