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

@ -226,7 +226,7 @@ public class Matrix {
* Matrix Vector subtraction, each column in the matrix will be subtracted
* with the vector.
*
* @return a new vector with subtracted elements
* @return a new matrix with subtracted elements
*/
public static double[][] subtract(double[][] matrix, double[] vector){
vectorPreCheck(matrix, vector);
@ -247,15 +247,13 @@ public class Matrix {
*
* @return a new vector with the result
*/
public static double[][] multiply(double[][] matrix, double[] vector){
public static double[] multiply(double[][] matrix, double[] vector){
vectorPreCheck(matrix, vector);
double[][] result = new double[matrix.length][1];
double[] result = new double[matrix.length];
for (int y=0; y < result.length; ++y) {
for (int y=0; y < matrix.length; ++y) {
for (int x=0; x<matrix[0].length; ++x) {
for (int i=0; i < result[y].length; ++i){
result[y][i] += matrix[y][x] * vector[x];
}
result[y] += matrix[y][x] * vector[x];
}
}
return result;
@ -299,7 +297,7 @@ public class Matrix {
}
private static void vectorPreCheck(double[][] matrix, double[] vector) {
if (matrix[0].length != vector.length)
throw new IllegalArgumentException("Matrix columns need to have same length as vector length: " +
throw new IllegalArgumentException("Matrix columns need to have same length as the vector length: " +
"matrix " + matrix.length + "x" + matrix[0].length + ", " +
"vector " + vector.length + "x1");
}

View file

@ -16,12 +16,12 @@ public class LinearRegression {
* h(x) = theta0 * x0 + theta1 * x1 + ... + thetan * xn => transpose(theta) * x
* </i>
*/
protected static double[][] calculateHypotesis(double[][] x, double[] theta){
protected static double[] calculateHypothesis(double[][] x, double[] theta){
return Matrix.multiply(x, theta);
}
/**
* Linear Regresion cost method.
* Linear Regression cost method.
* <br /><br />
* <i>
* J(O) = 1 / (2 * m) * Σ { ( h(Xi) - Yi )^2 }
@ -30,10 +30,11 @@ public class LinearRegression {
* @return a number indicating the error rate
*/
protected static double calculateCost(double[][] x, double[] y, double[] theta){
double[] hypothesis = calculateHypothesis(x, theta);
double[] normalized = Matrix.subtract(hypothesis, y);
return 1.0 / (2.0 * x.length) * Matrix.sum(
Matrix.Elemental.pow(
Matrix.subtract(calculateHypotesis(x, theta), y),
2));
Matrix.Elemental.pow(normalized,2));
}
/**
@ -45,13 +46,14 @@ public class LinearRegression {
*
* @return the theta that was found to minimize the cost function
*/
public static double[] gradientAscent(double[][] x, double[] y, double[] theta, double alpha){
public static double[] gradientDescent(double[][] x, double[] y, double[] theta, double alpha){
double[] newTheta = new double[theta.length];
double m = y.length;
double[][] hypotesisCache = Matrix.subtract(calculateHypotesis(x, theta), y);
double[] hypothesis = calculateHypothesis(x, theta);
double[] normalized = Matrix.subtract(hypothesis, y);
for (int j= 0; j < theta.length; j++) {
newTheta[j] = theta[j] - alpha * (1.0/m) * Matrix.sum(Matrix.add(hypotesisCache, Matrix.getColumn(x, j)));
newTheta[j] = theta[j] - alpha * (1.0/m) * Matrix.sum(Matrix.add(normalized, Matrix.getColumn(x, j)));
}
return newTheta;