We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Try to write the multiply() and transpose() functions on your own for practice. You may want to use a premade invert() function to find the inverse of a matrix if it seems too cumbersome for this exercise.
importjava.util.Scanner;importjava.util.Arrays;publicclassSolution{publicstaticvoidmain(String[]args){/* Read input: Create and fill X,Y arrays */Scannerscan=newScanner(System.in);intm=scan.nextInt();intn=scan.nextInt();double[][]X=newdouble[n][m+1];double[][]Y=newdouble[n][1];for(introw=0;row<n;row++){X[row][0]=1;for(intcol=1;col<=m;col++){X[row][col]=scan.nextDouble();}Y[row][0]=scan.nextDouble();}/* Calculate B */double[][]xtx=multiply(transpose(X),X);double[][]xtxInv=invert(xtx);double[][]xty=multiply(transpose(X),Y);double[][]B=multiply(xtxInv,xty);intsizeB=B.length;/* Calculate and print values for the "q" feature sets */intq=scan.nextInt();for(inti=0;i<q;i++){doubleresult=B[0][0];for(introw=1;row<sizeB;row++){result+=scan.nextDouble()*B[row][0];}System.out.println(result);}}/* Multiplies 2 matrices in O(n^3) time */publicstaticdouble[][]multiply(double[][]A,double[][]B){intaRows=A.length;intaCols=A[0].length;intbRows=B.length;intbCols=B[0].length;double[][]C=newdouble[aRows][bCols];intcRows=C.length;intcCols=C[0].length;for(introw=0;row<cRows;row++){for(intcol=0;col<cCols;col++){for(intk=0;k<aCols;k++){C[row][col]+=A[row][k]*B[k][col];}}}returnC;}publicstaticdouble[][]transpose(double[][]matrix){/* Create new array with switched dimensions */intoriginalRows=matrix.length;intoriginalCols=matrix[0].length;introws=originalCols;intcols=originalRows;double[][]result=newdouble[rows][cols];/* Fill our new 2D array */for(introw=0;row<originalRows;row++){for(intcol=0;col<originalCols;col++){result[col][row]=matrix[row][col];}}returnresult;}/******************************************************************//* Matrix Inversion code (shown below) is from: *//* http://www.sanfoundry.com/java-program-find-inverse-matrix/ *//******************************************************************/publicstaticdouble[][]invert(doublea[][]){intn=a.length;doublex[][]=newdouble[n][n];doubleb[][]=newdouble[n][n];intindex[]=newint[n];for(inti=0;i<n;++i)b[i][i]=1;// Transform the matrix into an upper trianglegaussian(a,index);// Update the matrix b[i][j] with the ratios storedfor(inti=0;i<n-1;++i)for(intj=i+1;j<n;++j)for(intk=0;k<n;++k)b[index[j]][k]-=a[index[j]][i]*b[index[i]][k];// Perform backward substitutionsfor(inti=0;i<n;++i){x[n-1][i]=b[index[n-1]][i]/a[index[n-1]][n-1];for(intj=n-2;j>=0;--j){x[j][i]=b[index[j]][i];for(intk=j+1;k<n;++k){x[j][i]-=a[index[j]][k]*x[k][i];}x[j][i]/=a[index[j]][j];}}returnx;}// Method to carry out the partial-pivoting Gaussian// elimination. Here index[] stores pivoting order.publicstaticvoidgaussian(doublea[][],intindex[]){intn=index.length;doublec[]=newdouble[n];// Initialize the indexfor(inti=0;i<n;++i)index[i]=i;// Find the rescaling factors, one from each rowfor(inti=0;i<n;++i){doublec1=0;for(intj=0;j<n;++j){doublec0=Math.abs(a[i][j]);if(c0>c1)c1=c0;}c[i]=c1;}// Search the pivoting element from each columnintk=0;for(intj=0;j<n-1;++j){doublepi1=0;for(inti=j;i<n;++i){doublepi0=Math.abs(a[index[i]][j]);pi0/=c[index[i]];if(pi0>pi1){pi1=pi0;k=i;}}// Interchange rows according to the pivoting orderintitmp=index[j];index[j]=index[k];index[k]=itmp;for(inti=j+1;i<n;++i){doublepj=a[index[i]][j]/a[index[j]][j];// Record pivoting ratios below the diagonala[index[i]][j]=pj;// Modify other elements accordinglyfor(intl=j+1;l<n;++l)a[index[i]][l]-=pj*a[index[j]][l];}}}}
Day 9: Multiple Linear Regression
You are viewing a single comment's thread. Return to all comments →
Java solution - passes 100% of test cases
Try to write the multiply() and transpose() functions on your own for practice. You may want to use a premade invert() function to find the inverse of a matrix if it seems too cumbersome for this exercise.
From my HackerRank solutions.