Search in sources :

Example 26 with DoubleArrayVectorizer

use of org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer in project ignite by apache.

the class KNNRegressionTest method testUpdate.

/**
 */
@Test
public void testUpdate() {
    Map<Integer, double[]> data = new HashMap<>();
    data.put(0, new double[] { 11.0, 0, 0, 0, 0, 0 });
    data.put(1, new double[] { 12.0, 2.0, 0, 0, 0, 0 });
    data.put(2, new double[] { 13.0, 0, 3.0, 0, 0, 0 });
    data.put(3, new double[] { 14.0, 0, 0, 4.0, 0, 0 });
    data.put(4, new double[] { 15.0, 0, 0, 0, 5.0, 0 });
    data.put(5, new double[] { 16.0, 0, 0, 0, 0, 6.0 });
    KNNRegressionTrainer trainer = new KNNRegressionTrainer().withK(1).withDistanceMeasure(new EuclideanDistance()).withWeighted(false);
    KNNRegressionModel originalMdlOnEmptyDataset = trainer.fit(new HashMap<>(), parts, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST));
    KNNRegressionModel updatedOnDataset = trainer.update(originalMdlOnEmptyDataset, data, parts, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST));
    Vector vector = VectorUtils.of(0.0, 0.0, 0.0, 5.0, 0.0);
    assertNull(originalMdlOnEmptyDataset.predict(vector));
    assertEquals(Double.valueOf(15.0), updatedOnDataset.predict(vector));
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) KNNRegressionTrainer(org.apache.ignite.ml.knn.regression.KNNRegressionTrainer) DoubleArrayVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer) HashMap(java.util.HashMap) KNNRegressionModel(org.apache.ignite.ml.knn.regression.KNNRegressionModel) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) TrainerTest(org.apache.ignite.ml.common.TrainerTest) Test(org.junit.Test)

Example 27 with DoubleArrayVectorizer

use of org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer in project ignite by apache.

the class KNNRegressionTest method testSimpleRegressionWithOneNeighbour.

/**
 */
@Test
public void testSimpleRegressionWithOneNeighbour() {
    Map<Integer, double[]> data = new HashMap<>();
    data.put(0, new double[] { 11.0, 0.0, 0.0, 0.0, 0.0, 0.0 });
    data.put(1, new double[] { 12.0, 2.0, 0.0, 0.0, 0.0, 0.0 });
    data.put(2, new double[] { 13.0, 0.0, 3.0, 0.0, 0.0, 0.0 });
    data.put(3, new double[] { 14.0, 0.0, 0.0, 4.0, 0.0, 0.0 });
    data.put(4, new double[] { 15.0, 0.0, 0.0, 0.0, 5.0, 0.0 });
    data.put(5, new double[] { 16.0, 0.0, 0.0, 0.0, 0.0, 6.0 });
    KNNRegressionTrainer trainer = new KNNRegressionTrainer().withK(1).withDistanceMeasure(new EuclideanDistance()).withWeighted(false);
    KNNRegressionModel knnMdl = trainer.fit(new LocalDatasetBuilder<>(data, parts), new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST));
    assertEquals(15, knnMdl.predict(VectorUtils.of(0.0, 0.0, 0.0, 5.0, 0.0)), 1E-12);
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) KNNRegressionTrainer(org.apache.ignite.ml.knn.regression.KNNRegressionTrainer) DoubleArrayVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer) HashMap(java.util.HashMap) KNNRegressionModel(org.apache.ignite.ml.knn.regression.KNNRegressionModel) TrainerTest(org.apache.ignite.ml.common.TrainerTest) Test(org.junit.Test)

Example 28 with DoubleArrayVectorizer

use of org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer in project ignite by apache.

the class KNNClassificationTest method testBinaryClassificationFarPointsWithWeightedStrategy.

/**
 */
@Test
public void testBinaryClassificationFarPointsWithWeightedStrategy() {
    Map<Integer, double[]> data = new HashMap<>();
    data.put(0, new double[] { 10.0, 10.0, 1.0 });
    data.put(1, new double[] { 10.0, 20.0, 1.0 });
    data.put(2, new double[] { -1.0, -1.0, 1.0 });
    data.put(3, new double[] { -2.0, -2.0, 2.0 });
    data.put(4, new double[] { -1.0, -2.0, 2.0 });
    data.put(5, new double[] { -2.0, -1.0, 2.0 });
    KNNClassificationTrainer trainer = new KNNClassificationTrainer().withK(3).withDistanceMeasure(new EuclideanDistance()).withWeighted(true);
    KNNClassificationModel knnMdl = trainer.fit(data, parts, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
    assertEquals(1.0, knnMdl.predict(VectorUtils.of(-1.01, -1.01)), 0);
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) DoubleArrayVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer) HashMap(java.util.HashMap) KNNClassificationTrainer(org.apache.ignite.ml.knn.classification.KNNClassificationTrainer) KNNClassificationModel(org.apache.ignite.ml.knn.classification.KNNClassificationModel) Test(org.junit.Test)

Example 29 with DoubleArrayVectorizer

use of org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer in project ignite by apache.

the class KNNClassificationTest method testBinaryClassificationWithSmallestK.

/**
 */
@Test
public void testBinaryClassificationWithSmallestK() {
    Map<Integer, double[]> data = new HashMap<>();
    data.put(0, new double[] { 1.0, 1.0, 1.0 });
    data.put(1, new double[] { 1.0, 2.0, 1.0 });
    data.put(2, new double[] { 2.0, 1.0, 1.0 });
    data.put(3, new double[] { -1.0, -1.0, 2.0 });
    data.put(4, new double[] { -1.0, -2.0, 2.0 });
    data.put(5, new double[] { -2.0, -1.0, 2.0 });
    KNNClassificationTrainer trainer = new KNNClassificationTrainer().withK(1).withDistanceMeasure(new EuclideanDistance()).withWeighted(false);
    KNNClassificationModel knnMdl = trainer.fit(data, parts, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
    assertEquals(1.0, knnMdl.predict(VectorUtils.of(2.0, 2.0)), 0);
    assertEquals(2.0, knnMdl.predict(VectorUtils.of(-2.0, -2.0)), 0);
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) DoubleArrayVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer) HashMap(java.util.HashMap) KNNClassificationTrainer(org.apache.ignite.ml.knn.classification.KNNClassificationTrainer) KNNClassificationModel(org.apache.ignite.ml.knn.classification.KNNClassificationModel) Test(org.junit.Test)

Example 30 with DoubleArrayVectorizer

use of org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer in project ignite by apache.

the class KNNClassificationTest method testUpdate.

/**
 */
@Test
public void testUpdate() {
    Map<Integer, double[]> data = new HashMap<>();
    data.put(0, new double[] { 10.0, 10.0, 1.0 });
    data.put(1, new double[] { 10.0, 20.0, 1.0 });
    data.put(2, new double[] { -1, -1, 1.0 });
    data.put(3, new double[] { -2, -2, 2.0 });
    data.put(4, new double[] { -1.0, -2.0, 2.0 });
    data.put(5, new double[] { -2.0, -1.0, 2.0 });
    KNNClassificationTrainer trainer = new KNNClassificationTrainer().withK(3).withDistanceMeasure(new EuclideanDistance()).withWeighted(false);
    KNNClassificationModel originalMdlOnEmptyDataset = trainer.fit(new HashMap<>(), parts, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
    KNNClassificationModel updatedOnData = trainer.update(originalMdlOnEmptyDataset, data, parts, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
    Vector vector = VectorUtils.of(-1.01, -1.01);
    assertNull(originalMdlOnEmptyDataset.predict(vector));
    assertEquals(Double.valueOf(2.0), updatedOnData.predict(vector));
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) DoubleArrayVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer) HashMap(java.util.HashMap) KNNClassificationTrainer(org.apache.ignite.ml.knn.classification.KNNClassificationTrainer) KNNClassificationModel(org.apache.ignite.ml.knn.classification.KNNClassificationModel) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) Test(org.junit.Test)

Aggregations

DoubleArrayVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer)30 Test (org.junit.Test)23 HashMap (java.util.HashMap)17 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)14 TrainerTest (org.apache.ignite.ml.common.TrainerTest)11 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)10 Ignite (org.apache.ignite.Ignite)5 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)5 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)5 MeanAbsValueConvergenceCheckerFactory (org.apache.ignite.ml.composition.boosting.convergence.mean.MeanAbsValueConvergenceCheckerFactory)5 KNNClassificationModel (org.apache.ignite.ml.knn.classification.KNNClassificationModel)5 KNNClassificationTrainer (org.apache.ignite.ml.knn.classification.KNNClassificationTrainer)5 GDBModel (org.apache.ignite.ml.composition.boosting.GDBModel)4 GDBTrainer (org.apache.ignite.ml.composition.boosting.GDBTrainer)4 VectorUtils (org.apache.ignite.ml.math.primitives.vector.VectorUtils)4 SimpleGDUpdateCalculator (org.apache.ignite.ml.optimization.updatecalculators.SimpleGDUpdateCalculator)4 Path (java.nio.file.Path)3 Random (java.util.Random)3 KNNRegressionModel (org.apache.ignite.ml.knn.regression.KNNRegressionModel)3 KNNRegressionTrainer (org.apache.ignite.ml.knn.regression.KNNRegressionTrainer)3