Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R neuralNet: "non-conformable arguments"

Argh! I keep getting the following error when attempting to compute with my neural network:

> net.compute <- compute(net, matrix.train2)
Error in neurons[[i]] %*% weights[[i]] : non-conformable arguments

I can't figure out what the problem is. Below I'll provide you with an example data and formatting from my matrices and then I'll show you the code I'm attempting to run.

  • matrix.train1 is used for training the network

    > matrix.train1
        (Intercept) survived pclass sexmale    age sibsp parch     fare embarkedC embarkedQ embarkedS
    1             1        0      3       1  22.00     1     0   7.2500         0         0             1
    2             1        1      1       0  38.00     1     0  71.2833         1         0         0
    3             1        1      3       0  26.00     0     0   7.9250         0         0         1
    4             1        1      1       0  35.00     1     0  53.1000         0         0         1
    5             1        0      3       1  35.00     0     0   8.0500         0         0         1
    6             1        0      3       1 999.00     0     0   8.4583         0         1         0
    7             1        0      1       1  54.00     0     0  51.8625         0         0         1
    8             1        0      3       1   2.00     3     1  21.0750         0         0         1
    9             1        1      3       0  27.00     0     2  11.1333         0         0         1
    10            1        1      2       0  14.00     1     0  30.0708         1         0         0
    11            1        1      3       0   4.00     1     1  16.7000         0         0         1
    
  • matrix.train2 is a slice of the training data used for testing the model

    > matrix.train2
        (Intercept) pclass sexmale    age sibsp parch     fare embarkedC embarkedQ embarkedS
    1             1      1       1  49.00     1     1 110.8833         1         0         0
    2             1      3       1  42.00     0     0   7.6500         0         0         1
    3             1      1       0  18.00     1     0 227.5250         1         0         0
    4             1      1       1  35.00     0     0  26.2875         0         0         1
    5             1      3       0  18.00     0     1  14.4542         1         0         0
    6             1      3       1  25.00     0     0   7.7417         0         1         0
    7             1      3       1  26.00     1     0   7.8542         0         0         1
    8             1      2       1  39.00     0     0  26.0000         0         0         1
    9             1      2       0  45.00     0     0  13.5000         0         0         1
    10            1      1       1  42.00     0     0  26.2875         0         0         1
    11            1      1       0  22.00     0     0 151.5500         0         0         1
    

The only real difference between the two matrices is that matrix.train2 doesn't contain the survived column.

Here's the R code I'm attempting to run:

#Build a matrix from training data 
matrix.train1 <- model.matrix(
  ~ survived + pclass + sex + age + sibsp + parch + fare + embarked, 
  data=train1
)

library(neuralnet)

#Train the neural net
net <- neuralnet(
  survived ~ pclass + sexmale + age + sibsp + parch + fare + embarkedC + 
  embarkedQ + embarkedS, data=matrix.train1, hidden=10, threshold=0.01
)

#Build a matrix from test data
matrix.train2 <- model.matrix(
  ~ pclass + sex + age + sibsp + parch + fare + embarked, 
  data=train2
)

#Apply neural net to test matrix 
net.results <- compute(
  net, matrix.train2
)

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

Can anyone tell me what I'm doing wrong here?

Thanks!


Updates based on comments so far:

  1. Using the solution from "Predicting class for new data using neuralnet" doesn't seem to work.

    > net.compute <- compute(net, matrix.train2[,1:10])
    Error in neurons[[i]] %*% weights[[i]] : non-conformable arguments
    
  2. I'm manually putting my train1 and train2 data frames into matrices via model.matrix because if I don't I get the following error:

    > Error in neurons[[i]] %*% weights[[i]] : 
    requires numeric/complex matrix/vector arguments
    

Note: see the following thread for more details on why I'm using model.matrix: "Working with neuralnet in R for the first time: get “requires numeric/complex matrix/vector arguments” but don't know how to correct".

like image 773
user2548029 Avatar asked Jul 09 '13 14:07

user2548029


1 Answers

It looks like you need to remove the predictor variable. Try this:

nn_pred<-compute(nn,test[,3:11])
like image 72
A L Avatar answered Oct 11 '22 08:10

A L