-
Notifications
You must be signed in to change notification settings - Fork 0
/
grnn.R
49 lines (37 loc) · 1.06 KB
/
grnn.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
calcR <- function(trainMatr, x) {
apply(trainMatr, 1, function(row) sqrt(sum(( row - x )^2)))
}
calcD <- function(r, sigma = 1) {
exp(-(r/sigma)^2)
}
calcY <- function(d, y) sum(d*y)/sum(d)
grnn <- function(trainWithResults, x, sigma = 1) {
train <- trainWithResults[, 1:2]
y_train <- trainWithResults[,3]
r <- calcR(train, x)
d <- calcD(r, sigma)
y <- calcY(d, y_train)
y
}
generateInputData <- function(n) {
set.seed(101)
x1 <- runif(n, -1, 1)
x2 <- runif(n, -1, 1)
y <- (1-x1^2)+2*(1-x2)^2
x <- data.frame(v1=x1, v2=x2, v3=y)
}
calculateMeanSquareError <- function(dataframe) {
sum((dataframe$y - dataframe$predicted_y)^2) / (nrow(dataframe))
}
n <- 10
train_coef <- 0.75
input <- generateInputData(n)
smp_size <- floor(train_coef * nrow(input))
set.seed(123)
train_ind <- sample(seq_len(nrow(input)), size = smp_size)
train <- input[train_ind, ]
test <- input[-train_ind, ]
result <- apply(test, 1, function(x) grnn(train, as.numeric(x[1:2])) )
colnames(test) <- c("x1", "x2", "y")
test["predicted_y"] = result
calculateMeanSquareError(test)