predicting class for new data using neuralnet

19,476

Solution 1

Hard to say in the absence of a good description of the 'test'-object, but can you see if this gives better results:

compute(nn, test[, 1:4])

Solution 2

I had the same problem. I put debugonce(neuralnet) and I discovered neuralnet was multiplying matrix from different sizes.

I solved the problem removing the y column from test with this function

columns <- c("x1","x2","x3","x4")
covariate <- subset(test, select = columns)
Share:
19,476
user1074057
Author by

user1074057

Updated on June 05, 2022

Comments

  • user1074057
    user1074057 almost 2 years

    I'm trying to predict the class (0 or 1) for a test dataset using a neural network trained using the neuralnet package in R.

    The data I have looks as follows:

    For train:

    x1          x2          x3          x4          y
    0.557       0.6217009   0.4839      0.5606936   0
    0.6549      0.6826347   0.4424      0.4117647   1
    0.529       0.5744681   0.5017      0.4148148   1
    0.6016771   0.5737052   0.3526971   0.3369565   1
    0.6353945   0.6445013   0.5404255   0.464       1
    0.5735294   0.6440678   0.4385965   0.5698925   1
    0.5252      0.5900621   0.4412      0.448       0
    0.7258687   0.7022059   0.5347222   0.4498645   1
    

    and more.

    The test set looks the exact same as the training data, just with different values (if need be I will post some samples).

    The code I use looks as follows:

    > library(neuralnet)
    > nn <- neuralnet(y ~ x1+x2+x3+x4, data=train, hidden=2, err.fct="ce", linear.output=FALSE)
    > plot(nn)
    > compute(nn, test)
    

    The network trains and I can successfully plot the network, but compute doesn't work. When I run compute it gives me the following error:

    Error in neurons[[i]] %*% weights[[i]] : non-conformable arguments
    

    So basically I'm trying to train a neural network to successfully classify the new test data.

    Any help is appreciated.

    Edit:

    A sampling of the test object is:

    x1          x2  x3          x4          y
    0.5822  0.6591  0.6445013   0.464       1
    0.4082  0.5388  0.5384616   0.4615385   0
    0.4481  0.5438  0.6072289   0.5400844   1
    0.4416  0.5034  0.5576923   0.3757576   1
    0.5038  0.6878  0.7380952   0.5784314   1
    0.4678  0.5219  0.5609756   0.3636364   1
    0.5089  0.5775  0.6183844   0.5462555   1
    0.4844  0.7117  0.6875      0.4823529   1
    0.4098  0.711   0.6801471   0.4722222   1
    

    I've also tried it with the y column empty of any values.

  • user1074057
    user1074057 almost 12 years
    That did it, as did deleting the y column from the test set. Thank you very much!
  • rhombidodecahedron
    rhombidodecahedron over 8 years
    Piggy-backing on this: if you used a formula that did not involve ALL the columns in your data frame, you will want to do something like: mf <- model.frame(fmla, data=DF); compute(nn, mf[,2:ncol(mf)]) (depending on which column is your target)
  • Ant
    Ant over 8 years
    never heard of debugonce() before. Great tool!
  • Newbie
    Newbie over 7 years
    @42 Is it possible that I train neuralnet object (nn in this example) again and again with a random subset of my huge trainData? Will that object store all the successive trainings?