Search in sources :

Example 31 with DenseLocalOnHeapMatrix

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

the class OLSMultipleLinearRegressionTest method testWampler2.

/**
     * This is a test based on the Wampler2 data set
     * http://www.itl.nist.gov/div898/strd/lls/data/Wampler2.shtml
     */
@Test
public void testWampler2() {
    double[] data = new double[] { 1.00000, 0, 1.11111, 1, 1.24992, 2, 1.42753, 3, 1.65984, 4, 1.96875, 5, 2.38336, 6, 2.94117, 7, 3.68928, 8, 4.68559, 9, 6.00000, 10, 7.71561, 11, 9.92992, 12, 12.75603, 13, 16.32384, 14, 20.78125, 15, 26.29536, 16, 33.05367, 17, 41.26528, 18, 51.16209, 19, 63.00000, 20 };
    OLSMultipleLinearRegression mdl = new OLSMultipleLinearRegression();
    final int nvars = 5;
    final int nobs = 21;
    double[] tmp = new double[(nvars + 1) * nobs];
    int off = 0;
    int off2 = 0;
    for (int i = 0; i < nobs; i++) {
        tmp[off2] = data[off];
        tmp[off2 + 1] = data[off + 1];
        tmp[off2 + 2] = tmp[off2 + 1] * tmp[off2 + 1];
        tmp[off2 + 3] = tmp[off2 + 1] * tmp[off2 + 2];
        tmp[off2 + 4] = tmp[off2 + 1] * tmp[off2 + 3];
        tmp[off2 + 5] = tmp[off2 + 1] * tmp[off2 + 4];
        off2 += (nvars + 1);
        off += 2;
    }
    mdl.newSampleData(tmp, nobs, nvars, new DenseLocalOnHeapMatrix());
    double[] betaHat = mdl.estimateRegressionParameters();
    TestUtils.assertEquals(betaHat, new double[] { 1.0, 1.0e-1, 1.0e-2, 1.0e-3, 1.0e-4, 1.0e-5 }, 1E-8);
    double[] se = mdl.estimateRegressionParametersStandardErrors();
    TestUtils.assertEquals(se, new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, 1E-8);
    TestUtils.assertEquals(1.0, mdl.calculateRSquared(), 1.0e-10);
    TestUtils.assertEquals(0, mdl.estimateErrorVariance(), 1.0e-7);
    TestUtils.assertEquals(0.00, mdl.calculateResidualSumOfSquares(), 1.0e-6);
}
Also used : DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Test(org.junit.Test)

Example 32 with DenseLocalOnHeapMatrix

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

the class AbstractMultipleLinearRegressionTest method testNewSampleInvalidData.

/** */
@Test(expected = MathIllegalArgumentException.class)
public void testNewSampleInvalidData() {
    double[] data = new double[] { 1, 2, 3, 4 };
    createRegression().newSampleData(data, 2, 3, new DenseLocalOnHeapMatrix());
}
Also used : DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Test(org.junit.Test)

Example 33 with DenseLocalOnHeapMatrix

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

the class AbstractMultipleLinearRegressionTest method testNewSampleInsufficientData.

/** */
@Test(expected = MathIllegalArgumentException.class)
public void testNewSampleInsufficientData() {
    double[] data = new double[] { 1, 2, 3, 4 };
    createRegression().newSampleData(data, 1, 3, new DenseLocalOnHeapMatrix());
}
Also used : DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Test(org.junit.Test)

Example 34 with DenseLocalOnHeapMatrix

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

the class AbstractMultipleLinearRegressionTest method testNewSampleNullData.

/** */
@Test(expected = NullArgumentException.class)
public void testNewSampleNullData() {
    double[] data = null;
    createRegression().newSampleData(data, 2, 3, new DenseLocalOnHeapMatrix());
}
Also used : DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Test(org.junit.Test)

Example 35 with DenseLocalOnHeapMatrix

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

the class AbstractMultipleLinearRegressionTest method testNewSample.

/**
     * Verifies that newSampleData methods consistently insert unitary columns
     * in design matrix.  Confirms the fix for MATH-411.
     */
@Test
public void testNewSample() {
    double[] design = new double[] { 1, 19, 22, 33, 2, 20, 30, 40, 3, 25, 35, 45, 4, 27, 37, 47 };
    double[] y = new double[] { 1, 2, 3, 4 };
    double[][] x = new double[][] { { 19, 22, 33 }, { 20, 30, 40 }, { 25, 35, 45 }, { 27, 37, 47 } };
    AbstractMultipleLinearRegression regression = createRegression();
    regression.newSampleData(design, 4, 3, new DenseLocalOnHeapMatrix());
    Matrix flatX = regression.getX().copy();
    Vector flatY = regression.getY().copy();
    regression.newXSampleData(new DenseLocalOnHeapMatrix(x));
    regression.newYSampleData(new DenseLocalOnHeapVector(y));
    Assert.assertEquals(flatX, regression.getX());
    Assert.assertEquals(flatY, regression.getY());
    // No intercept
    regression.setNoIntercept(true);
    regression.newSampleData(design, 4, 3, new DenseLocalOnHeapMatrix());
    flatX = regression.getX().copy();
    flatY = regression.getY().copy();
    regression.newXSampleData(new DenseLocalOnHeapMatrix(x));
    regression.newYSampleData(new DenseLocalOnHeapVector(y));
    Assert.assertEquals(flatX, regression.getX());
    Assert.assertEquals(flatY, 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)

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