Search in sources :

Example 36 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.

the class OLSMultipleLinearRegressionTest method testPerfectFit.

/** */
@Test
public void testPerfectFit() {
    double[] betaHat = regression.estimateRegressionParameters();
    TestUtils.assertEquals(new double[] { 11.0, 1.0 / 2.0, 2.0 / 3.0, 3.0 / 4.0, 4.0 / 5.0, 5.0 / 6.0 }, betaHat, 1e-13);
    double[] residuals = regression.estimateResiduals();
    TestUtils.assertEquals(new double[] { 0d, 0d, 0d, 0d, 0d, 0d }, residuals, 1e-13);
    Matrix errors = regression.estimateRegressionParametersVariance();
    final double[] s = { 1.0, -1.0 / 2.0, -1.0 / 3.0, -1.0 / 4.0, -1.0 / 5.0, -1.0 / 6.0 };
    Matrix refVar = new DenseLocalOnHeapMatrix(s.length, s.length);
    for (int i = 0; i < refVar.rowSize(); i++) for (int j = 0; j < refVar.columnSize(); j++) {
        if (i == 0) {
            refVar.setX(i, j, s[j]);
            continue;
        }
        double x = s[i] * s[j];
        refVar.setX(i, j, (i == j) ? 2 * x : x);
    }
    Assert.assertEquals(0.0, TestUtils.maximumAbsoluteRowSum(errors.minus(refVar)), 5.0e-16 * TestUtils.maximumAbsoluteRowSum(refVar));
    Assert.assertEquals(1, ((OLSMultipleLinearRegression) regression).calculateRSquared(), 1E-12);
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Test(org.junit.Test)

Example 37 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.

the class OLSMultipleLinearRegressionTest method testMathIllegalArgumentException.

/** */
@Test(expected = MathIllegalArgumentException.class)
public void testMathIllegalArgumentException() {
    OLSMultipleLinearRegression mdl = new OLSMultipleLinearRegression();
    mdl.validateSampleData(new DenseLocalOnHeapMatrix(1, 2), new DenseLocalOnHeapVector(1));
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Test(org.junit.Test)

Example 38 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.

the class OLSMultipleLinearRegressionTest method testSingularCalculateBeta.

/**
     * Anything requiring beta calculation should advertise SME.
     */
@Test(expected = SingularMatrixException.class)
public void testSingularCalculateBeta() {
    OLSMultipleLinearRegression mdl = new OLSMultipleLinearRegression(1e-15);
    mdl.newSampleData(new double[] { 1, 2, 3, 1, 2, 3, 1, 2, 3 }, 3, 2, new DenseLocalOnHeapMatrix());
    mdl.calculateBeta();
}
Also used : DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Test(org.junit.Test)

Example 39 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.

the class OLSMultipleLinearRegressionTest method testNewSample2.

/**
     * Verifies that setting X and Y separately has the same effect as newSample(X,Y).
     */
@Test
public void testNewSample2() {
    double[] y = new double[] { 1, 2, 3, 4 };
    double[][] x = new double[][] { { 19, 22, 33 }, { 20, 30, 40 }, { 25, 35, 45 }, { 27, 37, 47 } };
    OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
    regression.newSampleData(new DenseLocalOnHeapVector(y), new DenseLocalOnHeapMatrix(x));
    Matrix combinedX = regression.getX().copy();
    Vector combinedY = regression.getY().copy();
    regression.newXSampleData(new DenseLocalOnHeapMatrix(x));
    regression.newYSampleData(new DenseLocalOnHeapVector(y));
    Assert.assertEquals(combinedX, regression.getX());
    Assert.assertEquals(combinedY, regression.getY());
    // No intercept
    regression.setNoIntercept(true);
    regression.newSampleData(new DenseLocalOnHeapVector(y), new DenseLocalOnHeapMatrix(x));
    combinedX = regression.getX().copy();
    combinedY = regression.getY().copy();
    regression.newXSampleData(new DenseLocalOnHeapMatrix(x));
    regression.newYSampleData(new DenseLocalOnHeapVector(y));
    Assert.assertEquals(combinedX, regression.getX());
    Assert.assertEquals(combinedY, regression.getY());
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Example 40 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.

the class OLSMultipleLinearRegressionTest method cannotAddSampleDataWithSizeMismatch.

/** */
@Test(expected = MathIllegalArgumentException.class)
public void cannotAddSampleDataWithSizeMismatch() {
    double[] y = new double[] { 1.0, 2.0 };
    double[][] x = new double[1][];
    x[0] = new double[] { 1.0, 0 };
    createRegression().newSampleData(new DenseLocalOnHeapVector(y), new DenseLocalOnHeapMatrix(x));
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Test(org.junit.Test)

Aggregations

DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)67 Test (org.junit.Test)33 Matrix (org.apache.ignite.ml.math.Matrix)28 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)19 Vector (org.apache.ignite.ml.math.Vector)17 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)14 ArrayList (java.util.ArrayList)8 DistanceMeasure (org.apache.ignite.ml.math.distances.DistanceMeasure)6 Random (java.util.Random)5 MathIllegalArgumentException (org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)5 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 Comparator (java.util.Comparator)4 MLPArchitecture (org.apache.ignite.ml.nn.architecture.MLPArchitecture)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 Ignite (org.apache.ignite.Ignite)3 KMeansLocalClusterer (org.apache.ignite.ml.clustering.KMeansLocalClusterer)3 BaseFuzzyCMeansClusterer (org.apache.ignite.ml.clustering.BaseFuzzyCMeansClusterer)2 FuzzyCMeansLocalClusterer (org.apache.ignite.ml.clustering.FuzzyCMeansLocalClusterer)2