Search in sources :

Example 41 with Vector

use of org.apache.ignite.ml.math.Vector in project ignite by apache.

the class VectorToMatrixTest method testCross.

/** */
@Test
public void testCross() {
    consumeSampleVectors((v, desc) -> {
        if (!availableForTesting(v))
            return;
        fillWithNonZeroes(v);
        for (int delta : new int[] { -1, 0, 1 }) {
            final int size2 = v.size() + delta;
            if (size2 < 1)
                return;
            final Vector v2 = new DenseLocalOnHeapVector(size2);
            for (Vector.Element e : v2.all()) e.set(size2 - e.index());
            assertCross(v, v2, desc);
        }
    });
}
Also used : Vector(org.apache.ignite.ml.math.Vector) Test(org.junit.Test)

Example 42 with Vector

use of org.apache.ignite.ml.math.Vector 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)

Example 43 with Vector

use of org.apache.ignite.ml.math.Vector in project ignite by apache.

the class CacheVectorTest method testTimesVector.

/** */
public void testTimesVector() {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    IdentityValueMapper valMapper = new IdentityValueMapper();
    CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper);
    cacheVector.assign(1d);
    Vector testVec = new DenseLocalOnHeapVector(IntStream.range(0, size).asDoubleStream().toArray());
    try {
        cacheVector.times(testVec);
        TestCase.fail();
    } catch (UnsupportedOperationException ignored) {
    }
}
Also used : IdentityValueMapper(org.apache.ignite.ml.math.IdentityValueMapper) Vector(org.apache.ignite.ml.math.Vector) UnsupportedOperationException(org.apache.ignite.ml.math.exceptions.UnsupportedOperationException)

Example 44 with Vector

use of org.apache.ignite.ml.math.Vector 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)

Aggregations

Vector (org.apache.ignite.ml.math.Vector)44 Test (org.junit.Test)17 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)13 DenseLocalOffHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVector)7 IdentityValueMapper (org.apache.ignite.ml.math.IdentityValueMapper)5 Matrix (org.apache.ignite.ml.math.Matrix)5 UnsupportedOperationException (org.apache.ignite.ml.math.exceptions.UnsupportedOperationException)5 SparseLocalVector (org.apache.ignite.ml.math.impls.vector.SparseLocalVector)5 ExternalizeTest (org.apache.ignite.ml.math.ExternalizeTest)4 RandomVector (org.apache.ignite.ml.math.impls.vector.RandomVector)4 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)3 BiConsumer (java.util.function.BiConsumer)1 Function (java.util.function.Function)1 CardinalityException (org.apache.ignite.ml.math.exceptions.CardinalityException)1 PivotedMatrixView (org.apache.ignite.ml.math.impls.matrix.PivotedMatrixView)1 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)1 AbstractVector (org.apache.ignite.ml.math.impls.vector.AbstractVector)1 MatrixUtil.likeVector (org.apache.ignite.ml.math.util.MatrixUtil.likeVector)1 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)1 Assert.assertFalse (org.junit.Assert.assertFalse)1