-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject1.py
377 lines (323 loc) · 14.7 KB
/
project1.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
from string import punctuation, digits
import numpy as np
import random
# ==============================================================================
# === PART I =================================================================
# ==============================================================================
def get_order(n_samples):
try:
with open(str(n_samples) + '.txt') as fp:
line = fp.readline()
return list(map(int, line.split(',')))
except FileNotFoundError:
random.seed(1)
indices = list(range(n_samples))
random.shuffle(indices)
return indices
def hinge_loss_single(feature_vector, label, theta, theta_0):
"""
Finds the hinge loss on a single data point given specific classification
parameters.
Args:
`feature_vector` - numpy array describing the given data point.
`label` - float, the correct classification of the data
point.
`theta` - numpy array describing the linear classifier.
`theta_0` - float representing the offset parameter.
Returns:
the hinge loss, as a float, associated with the given data point and
parameters.
"""
# 1 - margin
return max(0, 1 - label * (np.dot(feature_vector, theta) + theta_0))
def hinge_loss_full(feature_matrix, labels, theta, theta_0):
"""
Finds the hinge loss for given classification parameters averaged over a
given dataset
Args:
`feature_matrix` - numpy matrix describing the given data. Each row
represents a single data point.
`labels` - numpy array where the kth element of the array is the
correct classification of the kth row of the feature matrix.
`theta` - numpy array describing the linear classifier.
`theta_0` - real valued number representing the offset parameter.
Returns:
the hinge loss, as a float, associated with the given dataset and
parameters. This number should be the average hinge loss across all of
"""
rows = feature_matrix.shape[0]
loss = 0
for i in range(rows):
loss += hinge_loss_single(feature_matrix[i], labels[i], theta, theta_0)
return loss / rows
def perceptron_single_step_update(
feature_vector,
label,
current_theta,
current_theta_0):
"""
Updates the classification parameters `theta` and `theta_0` via a single
step of the perceptron algorithm. Returns new parameters rather than
modifying in-place.
Args:
feature_vector - A numpy array describing a single data point.
label - The correct classification of the feature vector.
current_theta - The current theta being used by the perceptron
algorithm before this update.
current_theta_0 - The current theta_0 being used by the perceptron
algorithm before this update.
Returns a tuple containing two values:
the updated feature-coefficient parameter `theta` as a numpy array
the updated offset parameter `theta_0` as a floating point number
"""
prediction = np.dot(current_theta, feature_vector) + current_theta_0
# Verifica si la predicción es incorrecta
if label * prediction <= 0:
# Actualiza theta y theta_0
current_theta = current_theta + label * feature_vector
current_theta_0 = current_theta_0 + label
return current_theta, current_theta_0
def perceptron(feature_matrix, labels, T):
"""
Runs the full perceptron algorithm on a given set of data. Runs T
iterations through the data set: we do not stop early.
NOTE: Please use the previously implemented functions when applicable.
Do not copy paste code from previous parts.
Args:
`feature_matrix` - numpy matrix describing the given data. Each row
represents a single data point.
`labels` - numpy array where the kth element of the array is the
correct classification of the kth row of the feature matrix.
`T` - integer indicating how many times the perceptron algorithm
should iterate through the feature matrix.
Returns a tuple containing two values:
the feature-coefficient parameter `theta` as a numpy array
(found after T iterations through the feature matrix)
the offset parameter `theta_0` as a floating point number
(found also after T iterations through the feature matrix).
"""
samples = feature_matrix.shape[0]
current_theta = np.zeros_like(feature_matrix[0])
current_theta_0 = 0
for t in range(T):
for i in get_order(samples):
current_theta, current_theta_0 = perceptron_single_step_update(feature_matrix[i], labels[i], current_theta,
current_theta_0)
return current_theta, current_theta_0
def average_perceptron(feature_matrix, labels, T):
"""
Runs the average perceptron algorithm on a given dataset. Runs `T`
iterations through the dataset (we do not stop early) and therefore
averages over `T` many parameter values.
NOTE: Please use the previously implemented functions when applicable.
Do not copy paste code from previous parts.
NOTE: It is more difficult to keep a running average than to sum and
divide.
Args:
`feature_matrix` - A numpy matrix describing the given data. Each row
represents a single data point.
`labels` - A numpy array where the kth element of the array is the
correct classification of the kth row of the feature matrix.
`T` - An integer indicating how many times the perceptron algorithm
should iterate through the feature matrix.
Returns a tuple containing two values:
the average feature-coefficient parameter `theta` as a numpy array
(averaged over T iterations through the feature matrix)
the average offset parameter `theta_0` as a floating point number
(averaged also over T iterations through the feature matrix).
"""
samples = feature_matrix.shape[0]
current_theta = np.zeros_like(feature_matrix[0])
current_theta_0 = 0
thetas_sum = 0
thetas_0_sum = 0
for t in range(T):
for i in get_order(samples):
current_theta, current_theta_0 = perceptron_single_step_update(feature_matrix[i], labels[i], current_theta,
current_theta_0)
thetas_sum += current_theta
thetas_0_sum += current_theta_0
return thetas_sum / (T * samples), thetas_0_sum / (T * samples)
def pegasos_single_step_update(
feature_vector,
label,
L,
eta,
theta,
theta_0):
"""
Updates the classification parameters `theta` and `theta_0` via a single
step of the Pegasos algorithm. Returns new parameters rather than
modifying in-place.
Args:
`feature_vector` - A numpy array describing a single data point.
`label` - The correct classification of the feature vector.
`L` - The lamba value being used to update the parameters.
`eta` - Learning rate to update parameters.
`theta` - The old theta being used by the Pegasos
algorithm before this update.
`theta_0` - The old theta_0 being used by the
Pegasos algorithm before this update.
Returns:
a tuple where the first element is a numpy array with the value of
theta after the old update has completed and the second element is a
real valued number with the value of theta_0 after the old updated has
completed.
"""
prediction = np.dot(feature_vector, theta) + theta_0
if label * prediction <= 1:
theta = np.dot((1 - eta * L), theta) + eta * label * feature_vector
theta_0 = theta_0 + eta * label
else:
theta = np.dot((1 - eta * L), theta)
return theta, theta_0
def pegasos(feature_matrix, labels, T, L):
"""
Runs the Pegasos algorithm on a given set of data. Runs T iterations
through the data set, there is no need to worry about stopping early. For
each update, set learning rate = 1/sqrt(t), where t is a counter for the
number of updates performed so far (between 1 and nT inclusive).
NOTE: Please use the previously implemented functions when applicable. Do
not copy paste code from previous parts.
Args:
`feature_matrix` - A numpy matrix describing the given data. Each row
represents a single data point.
`labels` - A numpy array where the kth element of the array is the
correct classification of the kth row of the feature matrix.
`T` - An integer indicating how many times the algorithm
should iterate through the feature matrix.
`L` - The lamba value being used to update the Pegasos
algorithm parameters.
Returns:
a tuple where the first element is a numpy array with the value of the
theta, the linear classification parameter, found after T iterations
through the feature matrix and the second element is a real number with
the value of the theta_0, the offset classification parameter, found
after T iterations through the feature matrix.
"""
samples = feature_matrix.shape[0]
theta = np.zeros_like(feature_matrix[0])
theta_0 = 0
t = 1
for j in range(T):
for i in get_order(samples):
eta = 1 / np.sqrt(t)
theta, theta_0 = pegasos_single_step_update(feature_matrix[i], labels[i], L, eta, theta, theta_0)
t += 1
return theta, theta_0
# ==============================================================================
# === PART II ================================================================
# ==============================================================================
## #pragma: coderesponse template
## def decision_function(feature_vector, theta, theta_0):
## return np.dot(theta, feature_vector) + theta_0
## def classify_vector(feature_vector, theta, theta_0):
## return 2*np.heaviside(decision_function(feature_vector, theta, theta_0), 0)-1
## #pragma: coderesponse end
def classify(feature_matrix, theta, theta_0):
"""
A classification function that uses given parameters to classify a set of
data points.
Args:
`feature_matrix` - numpy matrix describing the given data. Each row
represents a single data point.
`theta` - numpy array describing the linear classifier.
`theta_0` - real valued number representing the offset parameter.
Returns:
a numpy array of 1s and -1s where the kth element of the array is the
predicted classification of the kth row of the feature matrix using the
given theta and theta_0. If a prediction is GREATER THAN zero, it
should be considered a positive classification.
"""
return np.where(np.dot(feature_matrix, theta) + theta_0 > 0, 1, -1)
def classifier_accuracy(
classifier,
train_feature_matrix,
val_feature_matrix,
train_labels,
val_labels,
**kwargs):
"""
Trains a linear classifier and computes accuracy. The classifier is
trained on the train data. The classifier's accuracy on the train and
validation data is then returned.
Args:
`classifier` - A learning function that takes arguments
(feature matrix, labels, **kwargs) and returns (theta, theta_0)
`train_feature_matrix` - A numpy matrix describing the training
data. Each row represents a single data point.
`val_feature_matrix` - A numpy matrix describing the validation
data. Each row represents a single data point.
`train_labels` - A numpy array where the kth element of the array
is the correct classification of the kth row of the training
feature matrix.
`val_labels` - A numpy array where the kth element of the array
is the correct classification of the kth row of the validation
feature matrix.
`kwargs` - Additional named arguments to pass to the classifier
(e.g. T or L)
Returns:
a tuple in which the first element is the (scalar) accuracy of the
trained classifier on the training data and the second element is the
accuracy of the trained classifier on the validation data.
"""
theta, theta_0 = classifier(train_feature_matrix, train_labels, **kwargs)
train_predictions = classify(train_feature_matrix, theta, theta_0)
val_predictions = classify(val_feature_matrix, theta, theta_0)
return accuracy(train_predictions, train_labels), accuracy(val_predictions, val_labels)
def accuracy(preds: np.array, targets: np.array) -> float:
"""
Given length-N vectors containing predicted and target labels,
returns the fraction of predictions that are correct.
"""
return (preds == targets).mean()
def extract_words(text):
"""
Helper function for `bag_of_words(...)`.
Args:
a string `text`.
Returns:
a list of lowercased words in the string, where punctuation and digits
count as their own words.
"""
for c in punctuation + digits:
text = text.replace(c, ' ' + c + ' ')
return text.lower().split()
def bag_of_words(texts, remove_stopword=False):
"""
Args:
`texts` - a list of natural language strings.
Returns:
a dictionary that maps each word appearing in `texts` to a unique
integer `index`.
"""
stopwords = open('stopwords.txt', 'r').read().split()
dictionary = {} # maps word to unique index
for text in texts:
word_list = extract_words(text)
for word in word_list:
if word in stopwords and remove_stopword:
continue
if word not in dictionary:
dictionary[word] = len(dictionary)
return dictionary
def extract_bow_feature_vectors(reviews, indices_by_word, binarize=True):
"""
Args:
`reviews` - a list of natural language strings
`indices_by_word` - a dictionary of uniquely-indexed words.
Returns:
a matrix representing each review via bag-of-words features. This
matrix thus has shape (n, m), where n counts reviews and m counts words
in the dictionary.
"""
feature_matrix = np.zeros([len(reviews), len(indices_by_word)], dtype=np.float64)
for i, text in enumerate(reviews):
word_list = extract_words(text)
for word in word_list:
if word in indices_by_word:
if binarize:
feature_matrix[i, indices_by_word[word]] = 1
else:
feature_matrix[i, indices_by_word[word]] += 1
return feature_matrix