Skip to content

Commit 017b50f

Browse files
committedAug 27, 2017
Perceptron - Neural Network
1 parent 494e7f5 commit 017b50f

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
 

‎Neural_Network/perceptron.py

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
'''
2+
3+
Perceptron
4+
w = w + N * (d(k) - y) * x(k)
5+
6+
Using perceptron network for oil analysis,
7+
with Measuring of 3 parameters that represent chemical characteristics we can classify the oil, in p1 or p2
8+
p1 = -1
9+
p2 = 1
10+
11+
'''
12+
13+
import random
14+
15+
16+
class Perceptron:
17+
def __init__(self, sample, exit, learn_rate=0.01, epoch_number=1000, bias=-1):
18+
self.sample = sample
19+
self.exit = exit
20+
self.learn_rate = learn_rate
21+
self.epoch_number = epoch_number
22+
self.bias = bias
23+
self.number_sample = len(sample)
24+
self.col_sample = len(sample[0])
25+
self.weight = []
26+
27+
def trannig(self):
28+
for sample in self.sample:
29+
sample.insert(0, self.bias)
30+
31+
for i in range(self.col_sample):
32+
self.weight.append(random.random())
33+
34+
self.weight.insert(0, self.bias)
35+
36+
epoch_count = 0
37+
38+
while True:
39+
erro = False
40+
for i in range(self.number_sample):
41+
u = 0
42+
for j in range(self.col_sample + 1):
43+
u = u + self.weight[j] * self.sample[i][j]
44+
y = self.sign(u)
45+
if y != self.exit[i]:
46+
47+
for j in range(self.col_sample + 1):
48+
49+
self.weight[j] = self.weight[j] + self.learn_rate * (self.exit[i] - y) * self.sample[i][j]
50+
erro = True
51+
#print('Epoch: \n',epoch_count)
52+
epoch_count = epoch_count + 1
53+
# if you want controle the epoch or just by erro
54+
if erro == False:
55+
#if epoch_count > self.epoch_number or not erro:
56+
break
57+
58+
def sort(self, sample):
59+
sample.insert(0, self.bias)
60+
u = 0
61+
for i in range(self.col_sample + 1):
62+
u = u + self.weight[i] * sample[i]
63+
64+
y = self.sign(u)
65+
66+
if y == -1:
67+
print('Sample: ', sample)
68+
print('classification: P1')
69+
else:
70+
print('Sample: ', sample)
71+
print('classification: P2')
72+
73+
def sign(self, u):
74+
return 1 if u >= 0 else -1
75+
76+
77+
samples = [
78+
[-0.6508, 0.1097, 4.0009],
79+
[-1.4492, 0.8896, 4.4005],
80+
[2.0850, 0.6876, 12.0710],
81+
[0.2626, 1.1476, 7.7985],
82+
[0.6418, 1.0234, 7.0427],
83+
[0.2569, 0.6730, 8.3265],
84+
[1.1155, 0.6043, 7.4446],
85+
[0.0914, 0.3399, 7.0677],
86+
[0.0121, 0.5256, 4.6316],
87+
[-0.0429, 0.4660, 5.4323],
88+
[0.4340, 0.6870, 8.2287],
89+
[0.2735, 1.0287, 7.1934],
90+
[0.4839, 0.4851, 7.4850],
91+
[0.4089, -0.1267, 5.5019],
92+
[1.4391, 0.1614, 8.5843],
93+
[-0.9115, -0.1973, 2.1962],
94+
[0.3654, 1.0475, 7.4858],
95+
[0.2144, 0.7515, 7.1699],
96+
[0.2013, 1.0014, 6.5489],
97+
[0.6483, 0.2183, 5.8991],
98+
[-0.1147, 0.2242, 7.2435],
99+
[-0.7970, 0.8795, 3.8762],
100+
[-1.0625, 0.6366, 2.4707],
101+
[0.5307, 0.1285, 5.6883],
102+
[-1.2200, 0.7777, 1.7252],
103+
[0.3957, 0.1076, 5.6623],
104+
[-0.1013, 0.5989, 7.1812],
105+
[2.4482, 0.9455, 11.2095],
106+
[2.0149, 0.6192, 10.9263],
107+
[0.2012, 0.2611, 5.4631]
108+
]
109+
110+
exit = [-1, -1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1]
111+
112+
network = Perceptron(sample=samples, exit = exit, learn_rate=0.01, epoch_number=1000, bias=-1)
113+
114+
network.trannig()
115+
116+
while True:
117+
i = 0
118+
sample = []
119+
while i < 3:
120+
value = input('value: ')
121+
value = float(value)
122+
sample.insert(i, value)
123+
i = i + 1
124+
125+
network.sort(sample)

0 commit comments

Comments
 (0)
Please sign in to comment.