javamatrixmachine-learningjama

java.lang.IllegalArgumentException: Matrix inner dimensions must agree


Here is my code:

package algorithms;
import Jama.Matrix;
import java.io.File;
import java.util.Arrays;
public class ThetaGetter {
    //First column is one, second is price and third is BHK
    private static double[][] variables = {
        {1,1130,2},
        {1,1100,2},
        {1,2055,3},
        {1,1047,2},
        {1,1927,3},
        {1,2667,3},
        {1,1146,2},
        {1,2020,3},
        {1,1190,2},
        {1,2165,3},
        {1,1250,2},
        {1,1185,2},
        {1,2825,4},
        {1,1200,2},
        {1,1580,3},
        {1,3200,3},
        {1,715,1},
        {1,1270,2},
        {1,2403,3},
        {1,1465,3},
        {1,1345,2}
    };

    private static double[][] prices = {
        {69.65},
        {60},
        {115},
        {55},
        {140},
        {225},
        {76.78},
        {120},
        {73.11},
        {140},
        {56},
        {79.39},
        {161},
        {73.69},
        {80},
        {145},
        {34.87},
        {77.72},
        {165},
        {98},
        {82}
    };
    private static Matrix X = new Matrix(variables);
    private static Matrix y = new Matrix(prices);
    public static void main(String[] args) {
        File file = new File("theta.dat");
        if(file.exists()){
            System.out.println("Theta has already been calculated!");
            return;
        }
        //inverse(Tra(X)*X)*tra(X)*y
        Matrix transposeX = X.transpose();
        Matrix inverse = X.times(transposeX).inverse();
        System.out.println(y.getArray().length);
        System.out.println(X.getArray().length);
        Matrix test = inverse.times(transposeX);
        Matrix theta = test.times(y);
        System.out.println(Arrays.deepToString(theta.getArray()));
    }
}

This algorithm basically tries to take housing prices and then get a few constants which are then used to guess prices of houses. However I am getting an exception on the line 'Matrix theta = test.times(y);' The error message is pretty much what's in the question. Is there some sort issue with the dimensions? Both of them have 21 items, so I don't know what's going on.


Solution

  • The mistake you are making is in the following line of code:

    Matrix inverse = X.times(transposeX).inverse();
    

    The formula you commented above is:

    //inverse(Tra(X)*X)*tra(X)*y
    

    but what you are actually calculating in code is: (X*Tra(X) instead of Tra(X)*X)

    //inverse(X*Tra(X))*tra(X)*y
    

    If the dimension of X is (m,n) where

    and the dimension of Y is (m,1), using the multiplications you used above you will have the following:

    inverse(X * Tra(X)) *Tra(X)*Y = inverse * Tra(X) * Y = result * y

    inverse((m,n)(n,m))(n,m)*(m,1)= (m,m) * (n,m) => which results in the error because the inner dimensions for a matrix multiplication must be equal

    What would fix your code would be replacing the following line:

    Matrix inverse = X.times(transposeX).inverse();
    

    with

     Matrix inverse = transposeX.times(X).inverse();