-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpercep.py
More file actions
66 lines (44 loc) · 1.13 KB
/
Copy pathpercep.py
File metadata and controls
66 lines (44 loc) · 1.13 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
import numpy as np
from math import sqrt
from math import pi
from math import exp
from sklearn.datasets import load_linnerud
data = load_linnerud()
#data = linnerud['target']
target = data['target']
#target = linnerud['data']
features = data['data']
chins = [i[0] for i in features]
medchins = np.median(chins)
binchins = []
for i in chins:
if i > medchins:
binchins.append(0)
else:
binchins.append(1)
chins2d = np.reshape(binchins, (-1, 1))
#print(chins)
dataset = np.append(target, chins2d, axis=1)
#print(dataset)
weights = np.zeros([3,1])
for i in range(0, 1000):
counter = 0
converged = True
for rowvalue in target:
predVal = np.dot(rowvalue,weights)
if predVal < 0:
predicted = 0
else:
predicted = 1
if predicted != binchins[counter]:
converged = False
if binchins[counter] == 0 :
weights = weights - np.expand_dims(rowvalue,1)
else:
weights = weights + np.expand_dims(rowvalue,1)
counter = counter + 1
if converged == True:
print("Error occurred")
break
Predict = np.dot(target, weights)
np.savetxt("perceptron_results.txt", Predict, newline=" \n")