-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw1_perceptron.py
90 lines (77 loc) · 3.4 KB
/
hw1_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
from __future__ import division, print_function
from typing import List, Tuple, Callable
import numpy as np
import scipy
import matplotlib.pyplot as plt
class Perceptron:
def __init__(self, nb_features=2, max_iteration=10, margin=1e-4):
'''
Args :
nb_features : Number of features
max_iteration : maximum iterations. You algorithm should terminate after this
many iterations even if it is not converged
margin is the min value, we use this instead of comparing with 0 in the algorithm
'''
self.nb_features = nb_features
self.w = [margin for i in range(0, nb_features + 1)]
self.margin = margin
self.max_iteration = max_iteration
def train(self, features: List[List[float]], labels: List[int]) -> bool:
'''
Args :
features : List of features. First element of each feature vector is 1
to account for bias
labels : label of each feature [-1,1]
Returns :
True/ False : return True if the algorithm converges else False.
'''
############################################################################
# TODO : complete this function.
# This should take a list of features and labels [-1,1] and should update
# to correct weights w. Note that w[0] is the bias term. and first term is
# expected to be 1 --- accounting for the bias
############################################################################
converge = False
w_vector = np.array(self.w)
for k in range(self.max_iteration):
update = False
for i in range(len(features)):
xn = np.array(features[i]).reshape((1, self.nb_features + 1))
yn = labels[i]
y = self.predict_one_value(w_vector, xn)
if y is not yn:
w_vector = w_vector + xn.dot((yn - y)) / np.linalg.norm(xn)
update = True
if not update:
converge = True
break # converge
self.w = w_vector.flatten().tolist()
return converge
def reset(self):
self.w = [0 for i in range(0, self.nb_features + 1)]
def predict(self, features: List[List[float]]) -> List[int]:
'''
Args :
features : List of features. First element of each feature vector is 1
to account for bias
Returns :
labels : List of integers of [-1,1]
'''
############################################################################
# TODO : complete this function.
# This should take a list of features and labels [-1,1] and use the learned
# weights to predict the label
############################################################################
results = []
weights = np.array(self.w)
for x in features:
results.append(self.predict_one_value(weights, np.array(x).reshape((1, self.nb_features + 1))))
return results
def predict_one_value(self, weights, feature):
result = weights.reshape(1, self.nb_features + 1).dot(feature.transpose())
if result >= self.margin:
return 1
else:
return -1
def get_weights(self) -> List[float]:
return self.w