MCPcopy Create free account
hub / github.com/AssemblyAI-Community/Machine-Learning-From-Scratch / Perceptron

Class Perceptron

08 Perceptron/perceptron.py:7–41  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5 return np.where(x > 0 , 1, 0)
6
7class Perceptron:
8
9 def __init__(self, learning_rate=0.01, n_iters=1000):
10 self.lr = learning_rate
11 self.n_iters = n_iters
12 self.activation_func = unit_step_func
13 self.weights = None
14 self.bias = None
15
16
17 def fit(self, X, y):
18 n_samples, n_features = X.shape
19
20 # init parameters
21 self.weights = np.zeros(n_features)
22 self.bias = 0
23
24 y_ = np.where(y > 0 , 1, 0)
25
26 # learn weights
27 for _ in range(self.n_iters):
28 for idx, x_i in enumerate(X):
29 linear_output = np.dot(x_i, self.weights) + self.bias
30 y_predicted = self.activation_func(linear_output)
31
32 # Perceptron update rule
33 update = self.lr * (y_[idx] - y_predicted)
34 self.weights += update * x_i
35 self.bias += update
36
37
38 def predict(self, X):
39 linear_output = np.dot(X, self.weights) + self.bias
40 y_predicted = self.activation_func(linear_output)
41 return y_predicted
42
43
44# Testing

Callers 1

perceptron.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected