MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / Perceptron

Class Perceptron

machine_learning/perceptron.py:17–77  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15
16
17class Perceptron:
18 def __init__(self, sample, exit, learn_rate=0.01, epoch_number=1000, bias=-1):
19 self.sample = sample
20 self.exit = exit
21 self.learn_rate = learn_rate
22 self.epoch_number = epoch_number
23 self.bias = bias
24 self.number_sample = len(sample)
25 self.col_sample = len(sample[0])
26 self.weight = []
27
28 def trannig(self):
29 for sample in self.sample:
30 sample.insert(0, self.bias)
31
32 for i in range(self.col_sample):
33 self.weight.append(random.random())
34
35 self.weight.insert(0, self.bias)
36
37 epoch_count = 0
38
39 while True:
40 erro = False
41 for i in range(self.number_sample):
42 u = 0
43 for j in range(self.col_sample + 1):
44 u = u + self.weight[j] * self.sample[i][j]
45 y = self.sign(u)
46 if y != self.exit[i]:
47
48 for j in range(self.col_sample + 1):
49
50 self.weight[j] = self.weight[j] + self.learn_rate * (self.exit[i] - y) * self.sample[i][j]
51 erro = True
52 #print('Epoch: \n',epoch_count)
53 epoch_count = epoch_count + 1
54 # if you want controle the epoch or just by erro
55 if erro == False:
56 print(('\nEpoch:\n',epoch_count))
57 print('------------------------\n')
58 #if epoch_count > self.epoch_number or not erro:
59 break
60
61 def sort(self, sample):
62 sample.insert(0, self.bias)
63 u = 0
64 for i in range(self.col_sample + 1):
65 u = u + self.weight[i] * sample[i]
66
67 y = self.sign(u)
68
69 if y == -1:
70 print(('Sample: ', sample))
71 print('classification: P1')
72 else:
73 print(('Sample: ', sample))
74 print('classification: P2')

Callers 1

perceptron.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected