-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_classifier_average_perceptron.py
183 lines (152 loc) · 6.51 KB
/
binary_classifier_average_perceptron.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#! /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])
count = 1
total_count = 0
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 averaged_perceptron_train(train_vector, y_hat):
global learning_mistakes, learning_successes, count
prediction = dot_product(weight_vector, train_vector)
if (prediction * y_hat) <= 0:
learning_mistakes += 1
for i in xrange(VECTOR_SIZE):
cached_weight_vector[i] += y_hat * train_vector[i]
weight_vector[i] += count * y_hat * train_vector[i]
count = 1
else:
count += 1
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
for fold in range(len(training_files)):
weight_vector = [0] * VECTOR_SIZE
cached_weight_vector = [0] * VECTOR_SIZE
weight_vector_array = []
cached_weight_vector_array = []
learning_mistakes_array = []
learning_successes_array = []
training_mistakes_array = []
training_successes_array = []
training_accuracy_array = []
testing_mistakes_array = []
testing_successes_array = []
testing_accuracy_array = []
for x in range(TRAINING_ITERATIONS):
learning_mistakes = 0
learning_successes = 0
training_mistakes = 0
training_successes = 0
testing_mistakes = 0
testing_successes = 0
count = 1
total_count = 0
with open(training_files[fold], 'r') as f:
for line in f:
if len(line) > 4:
total_count += 1
pixel_vector, y_hat = parse_file_line(line)
averaged_perceptron_train(pixel_vector, y_hat)
weight_vector = [float(x/total_count) for x in weight_vector]
weight_vector_array.append(weight_vector[:])
cached_weight_vector_array.append(cached_weight_vector[:])
learning_mistakes_array.append(learning_mistakes)
learning_successes_array.append(learning_successes)
#Calculate accuracy on training data
final_weight_vector = weight_vector_array[-1]
with open(training_files[fold], 'r') as f:
for line in f:
if len(line) > 4:
pixel_vector, y_hat = parse_file_line(line)
if dot_product(final_weight_vector, pixel_vector) < 0:
training_mistakes += 1
else:
training_successes += 1
training_mistakes_array.append(training_mistakes)
training_successes_array.append(training_successes)
training_accuracy_array.append((training_successes * 100) / float(training_mistakes + training_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_array.append(testing_mistakes)
testing_successes_array.append(testing_successes)
testing_accuracy_array.append((testing_successes * 100) / float(testing_mistakes + testing_successes))
learning_mistakes_per_fold.append(learning_mistakes_array)
learning_successes_per_fold.append(learning_successes_array)
training_mistakes_per_fold.append(training_mistakes_array)
training_successes_per_fold.append(training_successes_array)
training_accuracy_per_fold.append(training_accuracy_array)
testing_mistakes_per_fold.append(testing_mistakes_array)
testing_successes_per_fold.append(testing_successes_array)
testing_accuracy_per_fold.append(testing_accuracy_array)
print average_per_fold(learning_mistakes_per_fold)
print average_per_fold(training_accuracy_per_fold)
print average_per_fold(testing_accuracy_per_fold)
plt.plot(average_per_fold(learning_mistakes_per_fold))
plt.ylabel('Mistakes')
plt.xlabel('Iterations')
plt.title('Learning Curve for Averaged Perceptron')
plt.show()
plt.plot(average_per_fold(training_accuracy_per_fold), label = "Training Accuracy")
plt.plot(average_per_fold(testing_accuracy_per_fold), label = "Testing Accuracy")
plt.ylabel('Accuracy')
plt.xlabel('Iterations')
plt.title('Accuracy Curve for Averaged Perceptron')
plt.legend(loc = 0)
plt.show()