predicting class for new data using neuralnet

2019-03-18 15:11发布

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.

3条回答
We Are One
2楼-- · 2019-03-18 15:27

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)
查看更多
Fickle 薄情
3楼-- · 2019-03-18 15:44

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])
查看更多
太酷不给撩
4楼-- · 2019-03-18 15:45

I know this is an old post, but I came across a unique piece that may help someone in the future. Thought this post was most applicable as it throws the same error.

Scaling of a dataset must be converted back into a data.frame for use in compute

#scaled data
scaledData=scale(data)
nn=neuralnet(y~x,data=scaledData[train,])

#this repeatedly failed for me
compute(nn,scaledData[test,])

#this worked 
compute(nn,as.data.frame(scaledData)[test,])
查看更多
登录 后发表回答