Fixed some ml test cases

This commit is contained in:
Ziver Koc 2018-05-21 16:06:46 +02:00
parent 2160976406
commit 45b1f51685
6 changed files with 83 additions and 23 deletions

View file

@ -106,8 +106,11 @@ public class MatrixTest {
@Test
public void vectorMultiply(){
assertArrayEquals(
new double[][]{{8},{14}},
Matrix.multiply(new double[][]{{2,3},{-4,9}}, new double[]{1,2}));
new double[]{1.4, 1.9, 2.4, 2.9},
Matrix.multiply(
new double[][]{{1, 2, 3}, {1, 3, 4}, {1, 4, 5}, {1, 5, 6}},
new double[]{0.1, 0.2, 0.3}),
0.001);
}
@Test

View file

@ -12,12 +12,12 @@ public class LinearRegressionTest {
@Test
public void calculateHypotesis() {
double[][] hypotesis = LinearRegression.calculateHypotesis(
/* x */ new double[][]{{1, 2}, {1, 3}, {1, 4}, {1, 5}},
/* theta */ new double[]{0.1, 0.2}
double[] hypotesis = LinearRegression.calculateHypothesis(
/* x */ new double[][]{{1, 2, 3}, {1, 3, 4}, {1, 4, 5}, {1, 5, 6}},
/* theta */ new double[]{0.1, 0.2, 0.3}
);
assertArrayEquals(new double[][]{{0.5}, {0.7}, {0.9}, {1.1}}, hypotesis);
assertArrayEquals(new double[]{1.4, 1.9, 2.4, 2.9}, hypotesis, 0.001);
}
@Test
@ -33,7 +33,7 @@ public class LinearRegressionTest {
@Test
public void gradientAscent() {
double[] theta = LinearRegression.gradientAscent(
double[] theta = LinearRegression.gradientDescent( // one iteration
/* x */ new double[][]{{1, 5},{1, 2},{1, 4},{1, 5}},
/* y */ new double[]{1, 6, 4, 2},
/* theta */ new double[]{0, 0},

View file

@ -0,0 +1,47 @@
package zutil.test;
import org.junit.Assert;
import org.junit.internal.ArrayComparisonFailure;
import org.junit.internal.InexactComparisonCriteria;
/**
* Some additional assert functions that are missing from JUnit
*/
public class ZutilAssert extends Assert {
private ZutilAssert() {}
/**
* Asserts that two short arrays are equal. If they are not, an
* {@link AssertionError} is thrown.
*
* @param expected double array with expected values.
* @param actual double array with actual values
*/
public static void assertArrayEquals(double[][] expected, double[][] actual, double delta) {
ZutilAssert.assertArrayEquals(null, expected, actual, delta);
}
/**
* Asserts that two int arrays are equal. If they are not, an
* {@link AssertionError} is thrown with the given message.
*
* @param message the identifying message for the {@link AssertionError} (<code>null</code>
* okay)
* @param expected double array with expected values.
* @param actual double array with actual values
*/
public static void assertArrayEquals(String message, double[][] expected,
double[][] actual, double delta) throws ArrayComparisonFailure {
// If both arrays are referencing the same object or null
if (expected == actual)
return;
// Check array lengths
if (expected.length != actual.length)
fail(message + ". The array lengths of the first dimensions do not match.");
// Check all sub arrays
new InexactComparisonCriteria(delta).arrayEquals(message, expected, actual);
}
}