-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_classifier_pa_incr.py
142 lines (114 loc) · 5.1 KB
/
binary_classifier_pa_incr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#! /usr/bin/env python
import sys
import math
import matplotlib.pyplot as plt
training_files = ["Data/ocr_fold0_sm_train.txt", "Data/ocr_fold1_sm_train.txt", "Data/ocr_fold2_sm_train.txt", "Data/ocr_fold3_sm_train.txt", "Data/ocr_fold4_sm_train.txt", "Data/ocr_fold5_sm_train.txt", "Data/ocr_fold6_sm_train.txt", "Data/ocr_fold7_sm_train.txt", "Data/ocr_fold8_sm_train.txt", "Data/ocr_fold9_sm_train.txt"]
test_files = ["Data/ocr_fold0_sm_test.txt", "Data/ocr_fold1_sm_test.txt", "Data/ocr_fold2_sm_test.txt", "Data/ocr_fold3_sm_test.txt", "Data/ocr_fold4_sm_test.txt", "Data/ocr_fold5_sm_test.txt", "Data/ocr_fold6_sm_test.txt", "Data/ocr_fold7_sm_test.txt", "Data/ocr_fold8_sm_test.txt", "Data/ocr_fold9_sm_test.txt"]
VECTOR_SIZE = 128
vowels = ['a', 'e', 'i', 'o' ,'u']
TRAINING_ITERATIONS = 50
if len(sys.argv) > 1:
TRAINING_ITERATIONS = int(sys.argv[1])
learning_mistakes_per_fold = []
learning_successes_per_fold = []
training_mistakes_per_fold = []
training_successes_per_fold = []
training_accuracy_per_fold = []
testing_mistakes_per_fold = []
testing_successes_per_fold = []
testing_accuracy_per_fold = []
def parse_file_line(line):
if len(line) < 4:
return None
line_split = line.strip().split('\t')
pixel_values = line_split[1]
pixel_vector = convert_string_to_int_list(pixel_values)
y_hat = -1
if line_split[2] in vowels:
y_hat = 1
return pixel_vector, y_hat
def dot_product(vector1, vector2):
result = 0
for i in xrange(VECTOR_SIZE):
result += vector1[i] * vector2[i]
return result
def modulus(vector):
sum_of_squares = 0
for x in vector:
sum_of_squares += x*x
return math.sqrt(sum_of_squares)
def passive_aggressive_train(train_vector, y_hat):
global learning_mistakes, learning_successes
prediction = dot_product(weight_vector, train_vector)
learning_rate = (1 - y_hat * (dot_product(weight_vector, train_vector))) / (modulus(train_vector) ** 2)
if (prediction * y_hat) < 1:
learning_mistakes += 1
for i in xrange(VECTOR_SIZE):
weight_vector[i] += learning_rate * (y_hat * train_vector[i])
else:
learning_successes += 1
def test(train_vector, y_hat):
global testing_successes, testing_mistakes
prediction = dot_product(weight_vector, train_vector)
if (prediction * y_hat) <= 0:
testing_mistakes += 1
else:
testing_successes += 1
def convert_string_to_int_list(pixel_values):
pixel_values = pixel_values[2:]
pixel_vector = []
for x in pixel_values:
pixel_vector.append(int(x))
return pixel_vector
def average_per_fold(fold_list):
#accepts a list of lists containing y_hats of different interations per fold, and returns a list of averages
average_array = []
for i in range(len(fold_list[0])):
sum_of_elements = 0
for j in range(len(fold_list)):
sum_of_elements += fold_list[j][i]
average_array.append(sum_of_elements / len(fold_list))
return average_array
testing_mistakes_per_increment = []
testing_successes_per_increment = []
testing_accuracy_per_increment = []
for num_examples in range(1000, 7000, 1000):
for fold in range(len(training_files)):
weight_vector = [0] * VECTOR_SIZE
weight_vector_array = []
learning_mistakes_array = []
learning_successes_array = []
testing_mistakes = 0
testing_successes = 0
for x in range(TRAINING_ITERATIONS):
learning_mistakes = 0
learning_successes = 0
with open(training_files[fold], 'r') as f:
count = 0
for line in f:
if len(line) > 4:
count += 1
pixel_vector, y_hat = parse_file_line(line)
passive_aggressive_train(pixel_vector, y_hat)
if count >= num_examples :
break;
weight_vector_array.append(weight_vector[:])
learning_mistakes_array.append(learning_mistakes)
learning_successes_array.append(learning_successes)
with open(test_files[fold], 'r') as f:
for line in f:
if len(line) > 4:
pixel_vector, y_hat = parse_file_line(line)
test(pixel_vector, y_hat)
testing_mistakes_per_fold.append(testing_mistakes)
testing_successes_per_fold.append(testing_successes)
testing_accuracy_per_fold.append((testing_successes * 100) / float(testing_mistakes + testing_successes))
testing_mistakes_per_increment.append(float(sum(testing_mistakes_per_fold)/len(testing_mistakes_per_fold)))
testing_successes_per_increment.append(float(sum(testing_successes_per_fold)/len(testing_successes_per_fold)))
testing_accuracy_per_increment.append(float(sum(testing_accuracy_per_fold))/len(testing_accuracy_per_fold))
print testing_accuracy_per_increment
plt.plot(range(1000, 7000, 1000), testing_accuracy_per_increment)
plt.ylabel('Accuracy')
plt.xlabel('Iterations')
plt.title('Accuracy per increment for Passive Aggressice algorithm')
plt.show()