Search in sources :

Example 91 with Vector

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

the class RegressionMetricsTest method testCalculation.

/**
 */
@Test
public void testCalculation() {
    Map<Vector, Double> linearSet = new HashMap<Vector, Double>() {

        {
            put(VectorUtils.of(0.), 0.);
            put(VectorUtils.of(1.), 1.);
            put(VectorUtils.of(2.), 2.);
            put(VectorUtils.of(3.), 3.);
        }
    };
    IgniteModel<Vector, Double> linearModel = v -> v.get(0);
    IgniteModel<Vector, Double> squareModel = v -> Math.pow(v.get(0), 2);
    EvaluationResult linearRes = Evaluator.evaluateRegression(linearSet, linearModel, Vector::labeled);
    assertEquals(0., linearRes.get(MetricName.MAE), 0.01);
    assertEquals(0., linearRes.get(MetricName.MSE), 0.01);
    assertEquals(0., linearRes.get(MetricName.R2), 0.01);
    assertEquals(0., linearRes.get(MetricName.RSS), 0.01);
    assertEquals(0., linearRes.get(MetricName.RMSE), 0.01);
    EvaluationResult squareRes = Evaluator.evaluateRegression(linearSet, squareModel, Vector::labeled);
    assertEquals(2., squareRes.get(MetricName.MAE), 0.01);
    assertEquals(10., squareRes.get(MetricName.MSE), 0.01);
    assertEquals(8., squareRes.get(MetricName.R2), 0.01);
    assertEquals(40., squareRes.get(MetricName.RSS), 0.01);
    assertEquals(Math.sqrt(10), squareRes.get(MetricName.RMSE), 0.01);
}
Also used : VectorUtils(org.apache.ignite.ml.math.primitives.vector.VectorUtils) Evaluator(org.apache.ignite.ml.selection.scoring.evaluator.Evaluator) MetricName(org.apache.ignite.ml.selection.scoring.metric.MetricName) Map(java.util.Map) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) EvaluationResult(org.apache.ignite.ml.selection.scoring.evaluator.EvaluationResult) HashMap(java.util.HashMap) Test(org.junit.Test) IgniteModel(org.apache.ignite.ml.IgniteModel) Assert.assertEquals(org.junit.Assert.assertEquals) HashMap(java.util.HashMap) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) EvaluationResult(org.apache.ignite.ml.selection.scoring.evaluator.EvaluationResult) Test(org.junit.Test)

Example 92 with Vector

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

the class RandomForestClassifierTrainerTest method testUpdate.

/**
 */
@Test
public void testUpdate() {
    int sampleSize = 1000;
    Map<Integer, LabeledVector<Double>> sample = new HashMap<>();
    for (int i = 0; i < sampleSize; i++) {
        double x1 = i;
        double x2 = x1 / 10.0;
        double x3 = x2 / 10.0;
        double x4 = x3 / 10.0;
        sample.put(i, VectorUtils.of(x1, x2, x3, x4).labeled((double) i % 2));
    }
    ArrayList<FeatureMeta> meta = new ArrayList<>();
    for (int i = 0; i < 4; i++) meta.add(new FeatureMeta("", i, false));
    DatasetTrainer<RandomForestModel, Double> trainer = new RandomForestClassifierTrainer(meta).withAmountOfTrees(100).withFeaturesCountSelectionStrgy(x -> 2).withEnvironmentBuilder(TestUtils.testEnvBuilder());
    RandomForestModel originalMdl = trainer.fit(sample, parts, new LabeledDummyVectorizer<>());
    RandomForestModel updatedOnSameDS = trainer.update(originalMdl, sample, parts, new LabeledDummyVectorizer<>());
    RandomForestModel updatedOnEmptyDS = trainer.update(originalMdl, new HashMap<Integer, LabeledVector<Double>>(), parts, new LabeledDummyVectorizer<>());
    Vector v = VectorUtils.of(5, 0.5, 0.05, 0.005);
    assertEquals(originalMdl.predict(v), updatedOnSameDS.predict(v), 0.01);
    assertEquals(originalMdl.predict(v), updatedOnEmptyDS.predict(v), 0.01);
}
Also used : TrainerTest(org.apache.ignite.ml.common.TrainerTest) OnMajorityPredictionsAggregator(org.apache.ignite.ml.composition.predictionsaggregator.OnMajorityPredictionsAggregator) TestUtils(org.apache.ignite.ml.TestUtils) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) Assert.assertTrue(org.junit.Assert.assertTrue) HashMap(java.util.HashMap) Test(org.junit.Test) DatasetTrainer(org.apache.ignite.ml.trainers.DatasetTrainer) LabeledDummyVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.LabeledDummyVectorizer) ArrayList(java.util.ArrayList) FeatureMeta(org.apache.ignite.ml.dataset.feature.FeatureMeta) LabeledVector(org.apache.ignite.ml.structures.LabeledVector) VectorUtils(org.apache.ignite.ml.math.primitives.vector.VectorUtils) Map(java.util.Map) Assert.assertEquals(org.junit.Assert.assertEquals) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LabeledVector(org.apache.ignite.ml.structures.LabeledVector) FeatureMeta(org.apache.ignite.ml.dataset.feature.FeatureMeta) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) LabeledVector(org.apache.ignite.ml.structures.LabeledVector) TrainerTest(org.apache.ignite.ml.common.TrainerTest) Test(org.junit.Test)

Example 93 with Vector

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

the class LogisticRegressionModelTest method testPredictOnAnObservationWithWrongCardinality.

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

Example 94 with Vector

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

the class SVMModelTest method testPredictOnAnObservationWithWrongCardinality.

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

Example 95 with Vector

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

the class DataStreamGeneratorTest method testAsDatasetBuilder.

/**
 */
@Test
public void testAsDatasetBuilder() throws Exception {
    AtomicInteger cntr = new AtomicInteger();
    DataStreamGenerator generator = new DataStreamGenerator() {

        @Override
        public Stream<LabeledVector<Double>> labeled() {
            return Stream.generate(() -> {
                int val = cntr.getAndIncrement();
                return new LabeledVector<>(VectorUtils.of(val), (double) val % 2);
            });
        }
    };
    int N = 100;
    cntr.set(0);
    DatasetBuilder<Vector, Double> b1 = generator.asDatasetBuilder(N, 2);
    cntr.set(0);
    DatasetBuilder<Vector, Double> b2 = generator.asDatasetBuilder(N, (v, l) -> l == 0, 2);
    cntr.set(0);
    DatasetBuilder<Vector, Double> b3 = generator.asDatasetBuilder(N, (v, l) -> l == 1, 2, new UpstreamTransformerBuilder() {

        @Override
        public UpstreamTransformer build(LearningEnvironment env) {
            return new UpstreamTransformerForTest();
        }
    });
    checkDataset(N, b1, v -> (Double) v.label() == 0 || (Double) v.label() == 1);
    checkDataset(N / 2, b2, v -> (Double) v.label() == 0);
    checkDataset(N / 2, b3, v -> (Double) v.label() < 0);
}
Also used : LearningEnvironment(org.apache.ignite.ml.environment.LearningEnvironment) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UpstreamTransformer(org.apache.ignite.ml.dataset.UpstreamTransformer) LabeledVector(org.apache.ignite.ml.structures.LabeledVector) UpstreamTransformerBuilder(org.apache.ignite.ml.dataset.UpstreamTransformerBuilder) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) LabeledVector(org.apache.ignite.ml.structures.LabeledVector) Test(org.junit.Test)

Aggregations

Vector (org.apache.ignite.ml.math.primitives.vector.Vector)265 DenseVector (org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)95 Test (org.junit.Test)94 Ignite (org.apache.ignite.Ignite)78 LabeledVector (org.apache.ignite.ml.structures.LabeledVector)49 HashMap (java.util.HashMap)39 SandboxMLCache (org.apache.ignite.examples.ml.util.SandboxMLCache)38 DummyVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer)26 FileNotFoundException (java.io.FileNotFoundException)22 TrainerTest (org.apache.ignite.ml.common.TrainerTest)22 DecisionTreeClassificationTrainer (org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer)21 DecisionTreeModel (org.apache.ignite.ml.tree.DecisionTreeModel)21 Serializable (java.io.Serializable)19 IgniteCache (org.apache.ignite.IgniteCache)18 EncoderTrainer (org.apache.ignite.ml.preprocessing.encoding.EncoderTrainer)16 Cache (javax.cache.Cache)15 DoubleArrayVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer)15 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)14 ArrayList (java.util.ArrayList)12 ModelsComposition (org.apache.ignite.ml.composition.ModelsComposition)12