-
Notifications
You must be signed in to change notification settings - Fork 4
/
perceptron.py
39 lines (25 loc) · 917 Bytes
/
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
import numpy as np
class perceptron:
def __init__(self, eta, iterations):
self.eta = eta;
self.iterations = iterations;
#función para realizar el entrenamiento del perceptro.
def training(self, data, labels):
self.tetha = np.zeros(1 + data.shape[1])
self.errors_ = []
for _ in range(self.iterations):
errors = 0;
for xi, target in zip(data,labels):
update = self.eta * (target - self.predict(xi))
self.tetha[1:] += update * xi
self.tetha[0] += update
errors += int(update != 0.0)
self.errors_.append(errors)
return self
#función de activacion
def predict(self, data):
phi = np.where(self.calculation_valor_z(data) >= 0.0, 1, -1)
return phi
def calculation_valor_z(self, data):
z = np.dot(data, self.tetha[1:] + self.tetha[0])
return z