-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperceptron.py
71 lines (55 loc) · 2.4 KB
/
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
from supervisedlearner import SupervisedLearner
import numpy as np
class Perceptron(SupervisedLearner):
def __init__(self, feature_funcs, lr, is_c):
"""
:param lr: the rate at which the weights are modified at each iteration.
:param is_c: True if the perceptron is for classification problems,
False if the perceptron is for regression problems.
"""
super().__init__(feature_funcs)
self.weights = None
self.learning_rate = lr
self._trained = False
self.is_classifier = is_c
# TODO: Implement the rest of this class!
def step_function(self, inp):
"""
:param inp: a real number
:return: the predicted label produced by the given input
Assigns a label of 1.0 to the datapoint if <w,x> is a positive quantity
otherwise assigns label 0.0. Should only be called when self.is_classifier
is True.
"""
pass
def train(self, X, Y):
"""
:param X: a 2D numpy array where each row represents a datapoint
:param Y: a 1D numpy array where i'th element is the label of the corresponding datapoint in X
:return:
Does not return anything; only learns and stores as instance variable self.weights a 1D numpy
array whose i'th element is the weight on the i'th feature.
Do not forget to include the bias in your calculation.
"""
pass
def predict(self, x):
"""
:param x: a 1D numpy array representing a single datapoints
:return:
Given a data point x, produces the learner's estimate
of f(x). Use self.weights and make sure to use self.step_function
if self.is_classifier is True
"""
pass
def evaluate(self, datapoints, labels):
"""
:param datapoints: a 2D numpy array where each row represents a datapoint
:param labels: a 1D numpy array where i'th element is the label of the corresponding datapoint in datapoints
:return:
If self.is_classifier is True, returns the fraction (between 0 and 1)
of the given datapoints to which the method predict(.) assigns the correct label
If self.is_classifier is False, returns the Mean Squared Error (MSE)
between the labels and the predictions of their respective inputs (You
do not have to calculate the R2 Score)
"""
pass