-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperceptron.py
More file actions
41 lines (28 loc) · 1.22 KB
/
Copy pathperceptron.py
File metadata and controls
41 lines (28 loc) · 1.22 KB
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
import numpy as np
from neuron import Neuron
class Perceptron(Neuron):
def __init__(self , dim):
super().__init__(dim)
def fit (self , X , Y , eta = 0.1 , iterations = 50 , random_seed = 1):
#validate input values
if X.shape[1] is not self._dim-1:
raise Error("input array dimension not valid")
if X.shape[1] is not Y.shape[1]:
raise Error("input and predictor arrays are not same dimension")
#initalize weights randomly (pick with normal distribution and zero mean)
rgen = np.random.RandomState(random_seed)
self._w = rgen.normal(loc=0.0 , scale = 0.1 , size = self._dim)
#start iterations
for _ in range(iterations):
#for each example provided
for x_i , y_i in zip(X , Y):
self.__updateStep(x_i , y_i , eta)
return self
def predict (self , X):
z = np.dot(X , self._w[1:]) + self._w[0]
return np.where(z >= 0 , 1 , -1 )
def __updateStep(self , x_i , y_i , eta):
#update the weight calculating error with ith example
deltaW = eta*(y_i - self.predict(x_i))
self._w[0] += deltaW
self._w[1:] += deltaW * x_i