Hii there,

Hmm… my first program exceeding 20 lines.

Basically, it is a gradient descent problem (don’t know much about it 😛 ). As I am taking Machine Learning course on Cousera I wanna solve some problems on ML. I found some AI problems (don’t know much about this too) on HakerRank site and started solving this one. This guy is an output of my 5 hours of work. 😀

import java.util.Scanner; public class houseCosts { public static void main(String[] args){ System.out.println(&quot;Enter&quot;); Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); //System.out.println(n+&quot;&quot;+m); float[][] x = new float[n+2][m]; float[] t = new float[n+1]; float[] temp = new float[n+1]; float alpha = (float) 0.3; //alpha //inputs for(int j = 0; j&lt;m; j++){ for(int i=1; i&lt;n+2; i++){ x[i][j] = in.nextFloat(); //System.out.println(&quot;x &quot;+i+&quot; &quot;+j+&quot;= &quot;+x[i][j]); } } int num = in.nextInt(); //System.out.println(num); float[][] out = new float[n+1][num]; for(int j = 0; j&lt;num; j++){ for(int i=1; i&lt;n+1; i++){ out[i][j] = in.nextFloat(); //System.out.println(&quot;out &quot;+i+&quot; &quot;+j+&quot;= &quot;+out[i][j]); } } for(int i=0; i&lt;num; i++){ out[0][i] = 1; //System.out.println(&quot;x &quot;+0+&quot; &quot;+i+&quot;= &quot;+x[0][i]); } //Initializations for(int i=0; i&lt;m; i++){ x[0][i] = 1; //System.out.println(&quot;x &quot;+0+&quot; &quot;+i+&quot;= &quot;+x[0][i]); } for(int j=0; j&lt;n+1; j++){ t[j] = 0; //theta value initializing } for(int p = 0; p&lt;500; p++){ //no. of times //body for(int k=0; k&lt;n+1; k++){ float dum = 0; for(int j=0; j&lt;m; j++){ float ans = 0; for(int i=0; i&lt;n+1; i++){ ans+= t[i] * x[i][j]; //System.out.println(ans); } ans-= x[n+1][j]; //System.out.println(ans); ans*= x[k][j]; dum+=ans; //System.out.println(&quot;x &quot;+k+&quot; &quot;+j+&quot; =&quot;+x[k][j]); //System.out.println(ans); } //System.out.println(dum); temp[k] = (float) (t[k]-(alpha * dum * (1.0/m))); //System.out.println(temp[k]); } for(int k=0; k&lt;=n; k++){ t[k]=temp[k]; //System.out.print(t[k]+&quot; &quot;); } //System.out.println(&quot; &quot;); } for(int i = 0; i&lt;num; i++){ float foo = 0; for(int j=0; j&lt;n+1; j++){ foo+=out[j][i] * t[j]; } System.out.println(foo); } } }

Don’t judge me by this code coz, I don’t know much about algorithms and ML. And there are so many stdouts because I dunno how to debug in eclipse or any IDE for that mater. BTW, I got ten on ten for this problem.

Something Productive- CHECK!

Don’t forget to travel in time.

Cheerios

–Surya