-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequential_perceptron_in_R.R
81 lines (53 loc) · 1.17 KB
/
sequential_perceptron_in_R.R
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
##Start time
start_time <- Sys.time()
#Read in input data
D = read.csv('iris.csv')
#Get length of dataframe
DLength = ncol(D)
#slice dataframe into all input data vectors
x = D[,1:DLength-1]
#and label vectors
y = D[,DLength]
#learning rate
n = 0.3
#initialize normal vector, offset, and radius
w = integer(ncol(x))
b = 0
r = 0
#plot Data before running algorithm
#plot(x)
#Calculate the radius by obtaining the max length of all input data vectors
for (row in 1:nrow(x)) {
sum = 0
for (col in 1:ncol(x)) {
sum = sum + (x[row,col] * x[row,col])
}
length = sqrt(length)
if(length > r) {
r = length
}
}
classified = 0
while (classified!=nrow(x)) {
for (row in 1:nrow(x)) {
sum = 0
for (col in 1:ncol(x)) {
sum = sum + w[col] * x[row,col]
}
if (sign(sum-b) != y[row]) {
#Update free parameters
w = w + n*y[row]*x[row,]
b = b - n*y[row]*r*r
#Reset classified counter because we have an error
classified = 0
}
#Update classified counter
classified = classified + 1
}
#Keep plotting linear decision surface
#abline(b,w)
}
#end time
end_time <- Sys.time()
#execution time
end_time - start_time