Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Error in x - y : non-conformable arrays

Tags:

r

I'm trying to train a neural network in R with the following dataset (small part)

      Age   Salary Mortrate Clientrate Savrate PartialPrate
 [1,]  62 2381.140    0.047       7.05     3.1            0
 [2,]  52 1777.970    0.047       6.10     3.1            0
 [3,]  53 2701.210    0.047       6.40     3.1            0
 [4,]  52 4039.460    0.047       7.00     3.1            0
 [5,]  56  602.240    0.047       6.20     3.1            0
 [6,]  43 2951.090    0.047       6.80     3.1            0
 [7,]  49 4648.860    0.047       7.50     3.1            0
 [8,]  44 3304.110    0.047       7.10     3.1            0
 [9,]  56 1300.000    0.047       6.10     3.1            0
[10,]  50 1761.440    0.047       6.95     3.1            0

If I try doing it for small set of data as above the code works, but if I take more data then the neuralnet() gives the error:

Neuralnet error Error in x - y : non-conformable arrays. 

What does this error mean and how do I fix it?

Code:

trainingsoutput <- AllData$PartialPrepay
trainingdata <- cbind(AllData$LEEFTIJD, AllData$MEDSAL2, AllData$rate5Y,
                      AllData$CRate, AllData$SavRate, trainingsoutput)
dimnames(trainingdata) <- list(NULL, 
                               c("Age","Salary","Mortrate","Clientrate", 
                                 "Savrate","PartialPrate"))

nn <- neuralnet(PartialPrate ~ Age + Salary + Mortrate + Clientrate + Savrate,
                data = trainingdata ,hidden=3, err.fct="sse", threshold=0.01)
like image 714
user2298382 Avatar asked Jul 25 '13 12:07

user2298382


2 Answers

Error message description:

The phrase Conformable arrays is linear algebra jargon for "arrays that can sensibly be operated on together". The bare asterisk operator (as well as the ( + - and / ) operators in R do an Element-by-element, aka Element-wise multiplication. They can be different orientations, but they must be the same length.

How to reproduce this error in R:

x = matrix(c(1, 2, 3))         #has dimension 3 1
y = matrix(c(1, 2))            #has dimension 2 1
e = x * y                      #Error in x * y : non-conformable arrays
e

The asterisk operator between two matrices or vectors must be of compatible dimensions:

To fix the above, make sure the matrices are of compatible dimensions:

x = matrix(c(1, 2, 3))
y = matrix(c(c(1, 2, 3)))
e = x * y
e

Prints:

     [,1]
[1,]    1
[2,]    4
[3,]    9

Other ways to reproduce this error:

matrix(1,2,3)    + matrix(1,2)       #Error, non-conformable arrays
matrix(1:6)      / matrix(1:5)       #Error, non-conformable arrays
matrix(c(1,2))   / matrix(5)         #Error, non-conformable arrays
matrix(c(1,2,3)) * matrix(c(1,2))    #Error, non-conformable arrays
matrix(c(1,2))   * matrix(c(1))      #Error, non-conformable arrays
matrix(c(1,2))   * matrix(1)         #Error, non-conformable arrays
like image 60
Eric Leschinski Avatar answered Nov 11 '22 12:11

Eric Leschinski


I was just having the same issue and it appears to have been fixed when I removed any NANs from my predictors (or replaced them with some sane default value).

like image 43
user2721897 Avatar answered Nov 11 '22 13:11

user2721897