Search in sources :

Example 71 with DenseLocalOnHeapVector

use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.

the class LinearRegressionModelTest method testPredictOnAnObservationWithWrongCardinality.

/**
 */
@Test(expected = CardinalityException.class)
public void testPredictOnAnObservationWithWrongCardinality() {
    Vector weights = new DenseLocalOnHeapVector(new double[] { 2.0, 3.0 });
    LinearRegressionModel mdl = new LinearRegressionModel(weights, 1.0);
    Vector observation = new DenseLocalOnHeapVector(new double[] { 1.0 });
    mdl.apply(observation);
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Example 72 with DenseLocalOnHeapVector

use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.

the class SVMBinaryTrainerTest method testTrainWithTheLinearlySeparableCase.

/**
 * Test trainer on classification model y = x.
 */
@Test
public void testTrainWithTheLinearlySeparableCase() {
    Map<Integer, double[]> data = new HashMap<>();
    ThreadLocalRandom rndX = ThreadLocalRandom.current();
    ThreadLocalRandom rndY = ThreadLocalRandom.current();
    for (int i = 0; i < AMOUNT_OF_OBSERVATIONS; i++) {
        double x = rndX.nextDouble(-1000, 1000);
        double y = rndY.nextDouble(-1000, 1000);
        double[] vec = new double[AMOUNT_OF_FEATURES + 1];
        // assign label.
        vec[0] = y - x > 0 ? 1 : -1;
        vec[1] = x;
        vec[2] = y;
        data.put(i, vec);
    }
    SVMLinearBinaryClassificationTrainer<Integer, double[]> trainer = new SVMLinearBinaryClassificationTrainer<>();
    SVMLinearBinaryClassificationModel mdl = trainer.fit(new LocalDatasetBuilder<>(data, 10), (k, v) -> Arrays.copyOfRange(v, 1, v.length), (k, v) -> v[0], AMOUNT_OF_FEATURES);
    TestUtils.assertEquals(-1, mdl.apply(new DenseLocalOnHeapVector(new double[] { 100, 10 })), PRECISION);
    TestUtils.assertEquals(1, mdl.apply(new DenseLocalOnHeapVector(new double[] { 10, 100 })), PRECISION);
}
Also used : HashMap(java.util.HashMap) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Example 73 with DenseLocalOnHeapVector

use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.

the class SVMModelTest method testPredictWithErasedLabels.

/**
 */
@Test
public void testPredictWithErasedLabels() {
    Vector weights = new DenseLocalOnHeapVector(new double[] { 1.0, 1.0 });
    SVMLinearBinaryClassificationModel mdl = new SVMLinearBinaryClassificationModel(weights, 1.0);
    Vector observation = new DenseLocalOnHeapVector(new double[] { 1.0, 1.0 });
    TestUtils.assertEquals(1.0, mdl.apply(observation), PRECISION);
    observation = new DenseLocalOnHeapVector(new double[] { 3.0, 4.0 });
    TestUtils.assertEquals(1.0, mdl.apply(observation), PRECISION);
    observation = new DenseLocalOnHeapVector(new double[] { -1.0, -1.0 });
    TestUtils.assertEquals(-1.0, mdl.apply(observation), PRECISION);
    observation = new DenseLocalOnHeapVector(new double[] { -2.0, 1.0 });
    TestUtils.assertEquals(-1.0, mdl.apply(observation), PRECISION);
    observation = new DenseLocalOnHeapVector(new double[] { -1.0, -2.0 });
    TestUtils.assertEquals(-1.0, mdl.apply(observation), PRECISION);
    final SVMLinearBinaryClassificationModel mdlWithNewData = mdl.withIntercept(-2.0).withWeights(new DenseLocalOnHeapVector(new double[] { -2.0, -2.0 }));
    System.out.println("The SVM model is " + mdlWithNewData);
    observation = new DenseLocalOnHeapVector(new double[] { -1.0, -2.0 });
    TestUtils.assertEquals(1.0, mdl.apply(observation), PRECISION);
    TestUtils.assertEquals(-2.0, mdl.intercept(), PRECISION);
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Example 74 with DenseLocalOnHeapVector

use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.

the class SVMModelTest method testPredictWithErasedLabelsAndChangedThreshold.

/**
 */
@Test
public void testPredictWithErasedLabelsAndChangedThreshold() {
    Vector weights = new DenseLocalOnHeapVector(new double[] { 1.0, 1.0 });
    SVMLinearBinaryClassificationModel mdl = new SVMLinearBinaryClassificationModel(weights, 1.0).withThreshold(5);
    Vector observation = new DenseLocalOnHeapVector(new double[] { 1.0, 1.0 });
    TestUtils.assertEquals(-1.0, mdl.apply(observation), PRECISION);
    observation = new DenseLocalOnHeapVector(new double[] { 3.0, 4.0 });
    TestUtils.assertEquals(1.0, mdl.apply(observation), PRECISION);
    TestUtils.assertEquals(5, mdl.threshold(), PRECISION);
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Example 75 with DenseLocalOnHeapVector

use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.

the class SVMModelTest method testPredictWithRawLabels.

/**
 */
@Test
public void testPredictWithRawLabels() {
    Vector weights = new DenseLocalOnHeapVector(new double[] { 2.0, 3.0 });
    SVMLinearBinaryClassificationModel mdl = new SVMLinearBinaryClassificationModel(weights, 1.0).withRawLabels(true);
    Vector observation = new DenseLocalOnHeapVector(new double[] { 1.0, 1.0 });
    TestUtils.assertEquals(1.0 + 2.0 * 1.0 + 3.0 * 1.0, mdl.apply(observation), PRECISION);
    observation = new DenseLocalOnHeapVector(new double[] { 2.0, 1.0 });
    TestUtils.assertEquals(1.0 + 2.0 * 2.0 + 3.0 * 1.0, mdl.apply(observation), PRECISION);
    observation = new DenseLocalOnHeapVector(new double[] { 1.0, 2.0 });
    TestUtils.assertEquals(1.0 + 2.0 * 1.0 + 3.0 * 2.0, mdl.apply(observation), PRECISION);
    observation = new DenseLocalOnHeapVector(new double[] { -2.0, 1.0 });
    TestUtils.assertEquals(1.0 - 2.0 * 2.0 + 3.0 * 1.0, mdl.apply(observation), PRECISION);
    observation = new DenseLocalOnHeapVector(new double[] { 1.0, -2.0 });
    TestUtils.assertEquals(1.0 + 2.0 * 1.0 - 3.0 * 2.0, mdl.apply(observation), PRECISION);
    Assert.assertEquals(true, mdl.isKeepingRawLabels());
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Aggregations

DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)98 Vector (org.apache.ignite.ml.math.Vector)49 Test (org.junit.Test)44 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)26 Random (java.util.Random)18 HashMap (java.util.HashMap)17 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)14 Matrix (org.apache.ignite.ml.math.Matrix)12 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)11 IgniteCache (org.apache.ignite.IgniteCache)8 LabeledDataset (org.apache.ignite.ml.structures.LabeledDataset)8 Arrays (java.util.Arrays)7 Collections (java.util.Collections)6 List (java.util.List)6 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6 Stream (java.util.stream.Stream)6 Ignite (org.apache.ignite.Ignite)6 IgniteUtils (org.apache.ignite.internal.util.IgniteUtils)6 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)6