-
Notifications
You must be signed in to change notification settings - Fork 2
/
baycann_functions.R
64 lines (59 loc) · 2.05 KB
/
baycann_functions.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
# =================================================
# functions
prepare_data <- function(xtrain, ytrain, xtest, ytest){
y_names <- colnames(ytrain)
x_names <- colnames(xtrain)
n_train <- nrow(xtrain)
n_test <- nrow(xtest)
x <- rbind(xtrain, xtest)
y <- rbind(ytrain, ytest)
n <- nrow(x)
n_inputs <- length(x_names)
n_outputs <- length(y_names)
# scale the PSA inputs and outputs
xresults <- scale_data(x)
yresults <- scale_data(y)
xscaled <- xresults$scaled_data
yscaled <- yresults$scaled_data
xmins <- xresults$vec.mins
xmaxs <- xresults$vec.maxs
ymins <- yresults$vec.mins
ymaxs <- yresults$vec.maxs
xtrain_scaled <- xscaled[1:n_train, ]
ytrain_scaled <- yscaled[1:n_train, ]
xtest_scaled <- xscaled[(n_train+1):n, ]
ytest_scaled <- yscaled[(n_train+1):n, ]
return(list(n_inputs = n_inputs,
n_outputs = n_outputs,
n_train = n_train,
n_test = n_test,
x_names = x_names,
y_names = y_names,
xscaled = xscaled,
yscaled = yscaled,
xtrain_scaled = xtrain_scaled,
ytrain_scaled = ytrain_scaled,
xtest_scaled = xtest_scaled ,
ytest_scaled = ytest_scaled,
xmins = xmins,
xmaxs = xmaxs,
ymins = ymins,
ymaxs = ymaxs
))
}
scale_data <- function(unscaled_data){
vec.maxs <- apply(unscaled_data, 2, max)
vec.mins <- apply(unscaled_data, 2, min)
vec.ones <- matrix(1, nrow = nrow(unscaled_data), 1)
mat.maxs <- vec.ones %*% vec.maxs
mat.mins <- vec.ones %*% vec.mins
scaled_data <- 2 * (unscaled_data - mat.mins) / (mat.maxs - mat.mins) - 1
results <- list(scaled_data = scaled_data, vec.mins = vec.mins, vec.maxs = vec.maxs)
return(results)
}
unscale_data <- function(scaled_data, vec.mins, vec.maxs){
vec.ones <- matrix(1, nrow = nrow(scaled_data), 1)
mat.mins <- vec.ones %*% vec.mins
mat.maxs <- vec.ones %*% vec.maxs
unscaled_data <- (scaled_data + 1) * (mat.maxs - mat.mins) / 2 + mat.mins
}