-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelta_rule_salmon.py
More file actions
240 lines (227 loc) · 2.93 KB
/
delta_rule_salmon.py
File metadata and controls
240 lines (227 loc) · 2.93 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# USAGE
# python3 delta_rule_salmon.py
# import the necessary packages
from modules.nn import Perceptron1
import numpy as np
# construct the Salmon (freshwater and saltater) dataset
X = np.array([[83, 510],
[86, 505],
[94, 490],
[118, 490],
[86, 480],
[98, 480],
[101, 472],
[120, 472],
[90, 470],
[100, 470],
[101, 470],
[105, 470],
[75, 450],
[83, 452],
[85, 450],
[85, 442],
[75, 440],
[93, 440],
[105, 440],
[52.5, 425],
[78, 431],
[82, 430],
[95, 431],
[105, 432],
[87, 422],
[95, 427],
[102, 428],
[114, 427],
[109, 420],
[111, 421],
[126, 422],
[95, 411],
[70, 397],
[80, 399],
[84, 399],
[87, 402],
[92, 404],
[98, 403],
[98, 402],
[104, 404],
[121, 402],
[106, 439],
[109, 398],
[112, 394],
[114, 397],
[107, 368],
[118, 382],
[126, 371],
[136, 357],
[95, 430],
[135, 440],
[129, 420],
[156, 420],
[128, 400],
[144, 403],
[152.5, 403],
[178, 408],
[129, 390],
[140, 390],
[149, 392],
[152.5, 394],
[154, 390],
[128, 382],
[134, 382],
[148, 382],
[152, 381],
[170, 395],
[120, 359],
[133, 373],
[138, 371],
[140, 373],
[148, 372],
[163, 370],
[170, 375],
[123, 352],
[140, 351],
[162.5, 369],
[90, 385],
[115, 355],
[117, 356],
[135, 356],
[145, 356],
[150, 355],
[152.5, 354],
[155, 352],
[123, 350],
[125, 343],
[126, 342],
[131, 342],
[144, 342],
[107, 340],
[116, 344],
[124, 341],
[144, 339],
[150, 340],
[112.5, 330],
[114, 323],
[122, 304],
[152, 301],
[118,381],
])
y = np.array([[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[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],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1]])
#IMPORTANT: Due to fact that the weights are randomly chosen it is necessary to run the algorithm several times to check and compare results.
#RESULTS: The boundary classification equation would be x2 = -6.81x1 + 3.58 (x2 = -(w1/w2)x1 - (b/w2))
# define our perceptron and train it
print("[INFO] training perceptron...")
p = Perceptron1(X.shape[1], alpha=0.00001) #Same values than Samarasinghe (alpha=0.00001 y epochs=80)
p.fit(X, y, epochs=80)
# now that our perceptron is trained we can evaluate it
print("[INFO] testing perceptron...")
# now that our network is trained, loop over the data points
i = 1
for (x, target) in zip(X, y):
# make a prediction on the data point and display the result
# to our console
pred = p.predict(x)
print("[INFO] data={}, punto={}, ground-truth={}, pred={}".format(
x, i, target[0], pred))
i = i + 1
#Print the weight vector
W = p.weights()
print(W)