Search in sources :

Example 6 with KNNClassificationTrainer

use of org.apache.ignite.ml.knn.classification.KNNClassificationTrainer in project ignite by apache.

the class IrisClassificationExample method main.

/**
 * Runs example.
 */
public static void main(String[] args) throws IOException {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println(">>> Ignite grid started.");
        IgniteCache<Integer, Vector> dataCache = null;
        try {
            System.out.println(">>> Fill dataset cache.");
            dataCache = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.IRIS);
            KNNClassificationTrainer trainer = ((KNNClassificationTrainer) new KNNClassificationTrainer().withEnvironmentBuilder(LearningEnvironmentBuilder.defaultBuilder().withRNGSeed(0))).withK(3).withDistanceMeasure(new EuclideanDistance()).withWeighted(true);
            // This vectorizer works with values in cache of Vector class.
            Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(// FIRST means "label are stored at first coordinate of vector"
            Vectorizer.LabelCoordinate.FIRST);
            // Splits dataset to train and test samples with 60/40 proportion.
            TrainTestSplit<Integer, Vector> split = new TrainTestDatasetSplitter<Integer, Vector>().split(0.6);
            System.out.println(">>> Start traininig.");
            KNNClassificationModel mdl = trainer.fit(ignite, dataCache, split.getTrainFilter(), vectorizer);
            System.out.println(">>> Perform scoring.");
            double accuracy = Evaluator.evaluate(dataCache, split.getTestFilter(), mdl, vectorizer, new Accuracy<>());
            System.out.println(">> Model accuracy: " + accuracy);
        } finally {
            if (dataCache != null)
                dataCache.destroy();
        }
    } finally {
        System.out.flush();
    }
}
Also used : SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) KNNClassificationTrainer(org.apache.ignite.ml.knn.classification.KNNClassificationTrainer) KNNClassificationModel(org.apache.ignite.ml.knn.classification.KNNClassificationModel) EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) Ignite(org.apache.ignite.Ignite) Vector(org.apache.ignite.ml.math.primitives.vector.Vector)

Example 7 with KNNClassificationTrainer

use of org.apache.ignite.ml.knn.classification.KNNClassificationTrainer in project ignite by apache.

the class BinaryClassificationEvaluatorTest method testEvaluatorWithFilter.

/**
 * Test evaluator and trainer on classification model y = x.
 */
@Test
public void testEvaluatorWithFilter() {
    Map<Integer, Vector> cacheMock = new HashMap<>();
    for (int i = 0; i < twoLinearlySeparableClasses.length; i++) cacheMock.put(i, VectorUtils.of(twoLinearlySeparableClasses[i]));
    KNNClassificationTrainer trainer = new KNNClassificationTrainer().withK(3);
    TrainTestSplit<Integer, Vector> split = new TrainTestDatasetSplitter<Integer, Vector>(new SHA256UniformMapper<>(new Random(100))).split(0.75);
    Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
    KNNClassificationModel mdl = trainer.fit(cacheMock, split.getTrainFilter(), parts, vectorizer);
    double score = Evaluator.evaluate(cacheMock, split.getTestFilter(), mdl, vectorizer, MetricName.ACCURACY);
    assertEquals(0.9769230769230769, score, 1e-12);
}
Also used : SHA256UniformMapper(org.apache.ignite.ml.selection.split.mapper.SHA256UniformMapper) HashMap(java.util.HashMap) KNNClassificationTrainer(org.apache.ignite.ml.knn.classification.KNNClassificationTrainer) KNNClassificationModel(org.apache.ignite.ml.knn.classification.KNNClassificationModel) Random(java.util.Random) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) TrainerTest(org.apache.ignite.ml.common.TrainerTest) Test(org.junit.Test)

Example 8 with KNNClassificationTrainer

use of org.apache.ignite.ml.knn.classification.KNNClassificationTrainer 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 9 with KNNClassificationTrainer

use of org.apache.ignite.ml.knn.classification.KNNClassificationTrainer 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 10 with KNNClassificationTrainer

use of org.apache.ignite.ml.knn.classification.KNNClassificationTrainer 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

KNNClassificationModel (org.apache.ignite.ml.knn.classification.KNNClassificationModel)10 KNNClassificationTrainer (org.apache.ignite.ml.knn.classification.KNNClassificationTrainer)10 HashMap (java.util.HashMap)7 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)7 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)7 Test (org.junit.Test)7 DoubleArrayVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer)5 Ignite (org.apache.ignite.Ignite)3 SandboxMLCache (org.apache.ignite.examples.ml.util.SandboxMLCache)2 TrainerTest (org.apache.ignite.ml.common.TrainerTest)2 FileNotFoundException (java.io.FileNotFoundException)1 Random (java.util.Random)1 EncoderTrainer (org.apache.ignite.ml.preprocessing.encoding.EncoderTrainer)1 NormalizationTrainer (org.apache.ignite.ml.preprocessing.normalization.NormalizationTrainer)1 SHA256UniformMapper (org.apache.ignite.ml.selection.split.mapper.SHA256UniformMapper)1