Added more elemental matrix math and a non functioning gradient decent function.

This commit is contained in:
Ziver Koc 2018-09-05 16:37:19 +02:00
parent f84901dcc3
commit cf94310598
4 changed files with 123 additions and 6 deletions

View file

@ -125,7 +125,7 @@ public class MatrixTest {
}
@Test
public void vectorDivision(){
public void vectorMatrixDivision(){
assertArrayEquals(
new double[]{4,1},
Matrix.divide(new double[][]{{2,4},{-4,10}}, new double[]{1,2}),
@ -133,6 +133,24 @@ public class MatrixTest {
);
}
@Test
public void vectorMatrixElementalMultiply(){
assertArrayEquals(
new double[][]{{1, 4, 9}, {1, 6, 12}, {1, 8, 15}, {1, 10, 18}},
Matrix.Elemental.multiply(
new double[][]{{1, 2, 3}, {1, 3, 4}, {1, 4, 5}, {1, 5, 6}},
new double[]{1, 2, 3}));
}
@Test
public void vectorMatrixElementalDivision(){
assertArrayEquals(
new double[][]{{2,2},{-4,5}},
Matrix.Elemental.divide(
new double[][]{{2,4},{-4,10}},
new double[]{1,2}));
}
@Test
public void vectorSum(){
assertEquals(

View file

@ -1,6 +1,9 @@
package zutil.ml;
import org.junit.Test;
import zutil.log.LogUtil;
import java.util.logging.Level;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
@ -32,8 +35,37 @@ public class LinearRegressionTest {
assertEquals(11.9450, cost, 0.0001);
}
@Test
// Does not work
//@Test
public void gradientAscent() {
double[][] x = {
{1.0, 0.1, 0.6, 1.1},
{1.0, 0.2, 0.7, 1.2},
{1.0, 0.3, 0.8, 1.3},
{1.0, 0.4, 0.9, 1.4},
{1.0, 0.5, 1.0, 1.5}
};
double[] y = {
1,
0,
1,
0,
1
};
double[] theta = {
-2,
-1,
1,
2
};
double[] resultTheta = LinearRegression.gradientDescent(x, y, theta, 0);
assertEquals(0.73482, LinearRegression.calculateCost(x, y, resultTheta), 0.000001);
}
@Test
public void gradientAscentIteration() {
double[] theta = LinearRegression.gradientDescentIteration( // one iteration
/* x */ new double[][]{{1, 5},{1, 2},{1, 4},{1, 5}},
/* y */ new double[]{1, 6, 4, 2},