Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

predicting class for new data using neuralnet

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.

like image 497
user1074057 Avatar asked Jun 07 '12 20:06

user1074057


People also ask

Can we use neural network for prediction?

Predictive neural networks are a sophisticated data mining application that imitate the function of the brain to detect patterns in data sets. These mathematical models can detect the most subtle and complex relationships between your variables.

Can we use ANN for classification?

Artificial Neural Networks(ANN) are part of supervised machine learning where we will be having input as well as corresponding output present in our dataset. Our whole aim is to figure out a way of mapping this input to the respective output. ANN can be used for solving both regression and classification problems.


2 Answers

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])
like image 174
IRTFM Avatar answered Sep 29 '22 11:09

IRTFM


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)
like image 33
Aquiles Avatar answered Sep 29 '22 11:09

Aquiles