mil
mil

Reputation: 301

classification in r neuralnet

I'm using neuralnet in R to predict 3 classes based on 17 inputs. I have 3 classes: 1, 0 and 2. I have 2 files: training and testing. Training has 64 cases of 17 inputs and 18 column is output.

x1              x2              x3          etc...    x17                   y
-0.002307       0.034095    -0.002733            0              1
0.004461       -0.041385     0.137767          -0.294394        0
-0.25254       -0.094523     0                  0.074733        0
-0.25254       -0.094523     0                  0.074733        2

and more. 64 rows in total for training.

The test set is exactly same as the training data (16 rows), just with different values. The code I use

library(neuralnet)

nn <- neuralnet(y ~ x1+x2+x3
                  +x4+x5+x6+x7+x8+x9+x10+x11+x12+x13+x14+x15+x16+x17, 
                data=train,lifesign="full", hidden=15, err.fct="ce", 
                linear.output=FALSE)
an1 <-  compute(nn, Test[1:17])

I can do prediction for nn training

prediction (nn)

Which gives me prediction classes columns y for training case sets but I cannot do same with

prediction (an1): error message

Error in matrix(covariate[not.duplicated, ], nrow = nrow.notdupl) : 
  'data' must be of a vector type

I'm not entirely sure I need predict, or compute should be enough. But results for compute I get are:

$net.result
             [,1]
 [1,] 0.7503498233120
 [2,] 0.9982475522024
....
 [14,] 0.0007727434740
 [15,] 0.9999287879015

Which I don't know how to interpret it. I need something like

  2                1            0
  [1,] 0.964182671 0.022183652 0.013633677
  [2,] 0.952685528 0.032202528 0.015111944
  [3,] 0.966094194 0.021206723 0.012699083..

with probability distribution to each class.

I tried to use ifelse

 At2 <-(ifelse(Train$y==2,"2", ifelse(Train$y==1, "1","0")))

but still get the same 1 column for net.result.

Anyone could help to point out what line am I missing here to get what I want? Also I think ifelse does not do what I want - predict class Y based on 17 inputs. Is it so?

Upvotes: 1

Views: 13023

Answers (2)

mil
mil

Reputation: 301

I was able to get what I want by using nnet package and in particular predict function there.

idC <-class.ind(Train$y)
NN1=nnet(Train, idC[Train], size=15, maxit = 200, softmax=TRUE)
predict(NN1, data=Test,type = "class")

many thanks for all responses! :)

Upvotes: 2

Fernando
Fernando

Reputation: 7895

In the docs is says compute() returns a list of results, and prediction() takes a neuralnet fitted model...so i guess you're using it the wrong way.

Upvotes: 1

Related Questions