/
GradientDecentLinearRegressionMultivariable.java
206 lines (185 loc) · 5.95 KB
/
GradientDecentLinearRegressionMultivariable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import java.util.Random;
import java.lang.Math;
public class GradientDecentLinearRegressionMultivariable
{
public static void main(String[] args)
{
/*Setting up the training examples
* By giving some random data points
*/
final int nOfDataPoints=5; // Number of training examples
final int numberOfFeatures=1;
double[][] x= new double[nOfDataPoints][numberOfFeatures+1]; // x values of a training examples
double[][] y= new double[nOfDataPoints][1]; // the y values of training examples
for(int i=0;i<nOfDataPoints;i++)
{
x[i][0]=1;
for(int j=1;j<numberOfFeatures+1;j++)
x[i][j] = Math.random()*10; // get random value for x ranging from 1 to 100
y[i][0]= aFunction(x[i]); // give y values according to x values and function aFunction defined below
}
printMatrix(x);
printMatrix(y);
/* We now apply gradient decent on the input examples.
* We are now trying to minimize the cost function (1/(2*nOfDataPoints))*SumOverAllDataPOoints((a1x[i]+a0-y[i])^2)
* a0=a0-alpha(partial(costFunction)/partial(a0))
* a1=a1-alpha(partial(costFunction)/partial(a0))
*/
final double alpha=0.05;// set the learning rate to be 0.5
double[][] thetas=new double[numberOfFeatures+1][1];
double[][] temp= new double[numberOfFeatures+1][1];
int count=0;
for(int i=0;i<thetas.length;i++)
thetas[i][0]=Math.random()*10;
for(int i=0;i<thetas.length;i++)
temp[i][0]=thetas[i][0];
printMatrix(thetas);
do
{
for(int i=0;i<thetas.length;i++)
thetas[i][0]=temp[i][0];
temp=updateThetas(x,y,alpha,thetas);
count++;
System.out.println("Iteration "+count+" The value of thetas is: ");
printMatrix(thetas);
System.out.println("And it is updated to: ");
printMatrix(temp);
if(count==500)
break;
}
while(isCloseTo(0.00001,temp,thetas));
}
/* check if every value of a matrix is close to one and the other*/
public static boolean isCloseTo(double n,double[][]A, double [][]B )
{
int aRows = A.length;
int aColumns = A[0].length;
int bRows = B.length;
int bColumns = B[0].length;
if (aColumns != bColumns||aRows != bRows)
{
throw new IllegalArgumentException("A:Rows: " + aColumns + " did not match B:Columns " + bRows + ".");
}
for(int i=0;i<aRows;i++)
{
for(int j=0;j<aColumns;j++)
{
if(Math.abs(A[i][j]-B[i][j])>=n)
return true;
}
}
return false;
}
/* change a vector to a matrix*/
// public static double[][] chageToMatrix(double[] a)
// {
// int length=a.length;
// double[][] result=new double[1][length];
// for(int i=0;i<length;i++)
// result[0][i]=a[i];
// return result;
// }
/* Function to update Thetas*/
public static double[][] updateThetas(double[][] x, double[][] y,double alpha, double[][] thetas)
{
double[][] eval=mMultiplication(x,thetas);
System.out.println("X multiply by thetas is:");
printMatrix(eval);
double[][] result=new double[thetas.length][1];
for(int j=0;j<thetas.length;j++) // Go through each thetas one by one
{
double sum=0; // use to store the result
for(int i=0;i<x.length;i++) // iterate through each data points
{
sum=sum+(eval[i][0]-y[i][0])*x[i][j];
}
result[j][0]= result[j][0]-alpha*(sum/x.length);
}
return result;
}
/* A function that used to simulate the linear regression*/
public static double aFunction(double[] x) // a function to produce random data
{
// int length=x.length;
// double sum=0;
// for(int i=0;i<length;i++)
// {
// sum=sum+x[i]*(double)(i+1);
// }
return x[0]+9*x[1];
}
/* A function that add two matrices*/
// public static double[][] addMatrices(double[][] A, double[][] B)
// {
// int aRows = A.length;
// int aColumns = A[0].length;
// int bRows = B.length;
// int bColumns = B[0].length;
// if (aColumns != bColumns||aRows != bRows)
// {
// throw new IllegalArgumentException("A:Rows: " + aColumns + " did not match B:Columns " + bRows + ".");
// }
// double[][] C = new double[aRows][bColumns];
// for (int i = 0; i < aRows; i++)
// {
// for (int j = 0; j < bColumns; j++)
// {
// C[i][j] = 0.00000;
// }
// }
// for (int i = 0; i < aRows; i++)
// { // aRow
// for (int j = 0; j < bColumns; j++)
// { // bColumn
// C[i][j]=A[i][j]+B[i][j];
// }
// }
// return C;
// }
/*A function to multiply two matrices*/
public static double[][] mMultiplication(double[][] A, double[][] B)
{
int aRows = A.length;
int aColumns = A[0].length;
int bRows = B.length;
int bColumns = B[0].length;
if (aColumns != bRows)
{
throw new IllegalArgumentException("A:Rows: " + aColumns + " did not match B:Columns " + bRows + ".");
}
double[][] C = new double[aRows][bColumns];
for (int i = 0; i < aRows; i++)
{
for (int j = 0; j < bColumns; j++)
{
C[i][j] = 0.00000;
}
}
for (int i = 0; i < aRows; i++) { // aRow
for (int j = 0; j < bColumns; j++) { // bColumn
for (int k = 0; k < aColumns; k++) { // aColumn
C[i][j] += A[i][k] * B[k][j];
}
}
}
return C;
}
/*A function that print a matrix on the screen*/
public static void printMatrix(double[][] m)
{
try{
int rows = m.length;
int columns = m[0].length;
String str = "|\t";
for(int i=0;i<rows;i++){
for(int j=0;j<columns;j++){
str += m[i][j] + "\t";
}
System.out.println(str + "|");
str = "|\t";
}
System.out.println();
}
catch(Exception e){System.out.println("Matrix is empty!!");}
}
}