nntrf: nntrf: a supervised transformation function based on neural...

Description Usage Arguments Value See Also Examples

View source: R/nntrf.R

Description

This function transforms a dataset into the activations of the neurons of the hidden layer of a neural network. This is done by training a neural network and then computing the activations of the neural network for each input pattern. It uses the nnet package under the hood.

Usage

1
nntrf(keep_nn = TRUE, repetitions = 1, random_seed = NULL, ...)

Arguments

keep_nn

(default TRUE) Keep NN model. In most cases, the actual NN and associated results obtained by nnet is not required

repetitions

(default 1) Repeat nnet several times with different random seeds and select the best run using nnet's minimum value. This is useful because some random initialization of weights may lead to local minima.

random_seed

(default NULL)

...

See nnet params. Most important:

  • formula

  • data :dataframe with data. The last column must be the dependent variable. The dependent variable must be a factor for classification problems and numeric for regression problems. The input/independent variables must contain numeric values only.

  • size :number of units in the hidden layer.

  • maxit : the number of iterations of the net.

Value

list of:

See Also

nnet

Examples

 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
data("doughnutRandRotated")
rd <- doughnutRandRotated

# Make sure it is a classification problem
rd[,ncol(rd)] <- as.factor(rd[,ncol(rd)])
n <- nrow(rd)

# Split data into training and test
set.seed(0)
training_index <- sample(1:n, round(0.6*n))
train <- rd[training_index,]
test <- rd[-training_index,]
x_train <- train[,-ncol(train)]
y_train <- train[,ncol(train)]
x_test <- test[,-ncol(test)]
y_test <- test[,ncol(test)]

# Train nntrf transformation
set.seed(0)
nnpo <- nntrf(formula=V11~.,
              data=train,
              size=4, maxit=50, trace=TRUE)
              
# Apply nntrf transformation to the train and test splits               
trf_x_train <- nnpo$trf(x=x_train, use_sigmoid=FALSE)
trf_x_test <- nnpo$trf(x=x_test, use_sigmoid=FALSE)      

# Compute the success rate of KNN on the transformed feature space                   
outputs <- FNN::knn(trf_x_train, trf_x_test, y_train)
success <- mean(outputs == y_test)
print(success)

nntrf documentation built on Feb. 26, 2021, 5:08 p.m.