Search in sources :

Example 1 with LabeledDataset

use of org.apache.ignite.ml.structures.LabeledDataset in project ignite by apache.

the class LocalModelsTest method importExportKNNModelTest.

/**
 */
@Test
public void importExportKNNModelTest() throws IOException {
    executeModelTest(mdlFilePath -> {
        double[][] mtx = new double[][] { { 1.0, 1.0 }, { 1.0, 2.0 }, { 2.0, 1.0 }, { -1.0, -1.0 }, { -1.0, -2.0 }, { -2.0, -1.0 } };
        double[] lbs = new double[] { 1.0, 1.0, 1.0, 2.0, 2.0, 2.0 };
        LabeledDataset training = new LabeledDataset(mtx, lbs);
        KNNModel mdl = new KNNModel(3, new EuclideanDistance(), KNNStrategy.SIMPLE, training);
        Exporter<KNNModelFormat, String> exporter = new FileExporter<>();
        mdl.saveModel(exporter, mdlFilePath);
        KNNModelFormat load = exporter.load(mdlFilePath);
        Assert.assertNotNull(load);
        KNNModel importedMdl = new KNNModel(load.getK(), load.getDistanceMeasure(), load.getStgy(), load.getTraining());
        Assert.assertTrue("", mdl.equals(importedMdl));
        return null;
    });
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) KNNModelFormat(org.apache.ignite.ml.knn.models.KNNModelFormat) KNNModel(org.apache.ignite.ml.knn.models.KNNModel) LabeledDataset(org.apache.ignite.ml.structures.LabeledDataset) Test(org.junit.Test)

Example 2 with LabeledDataset

use of org.apache.ignite.ml.structures.LabeledDataset in project ignite by apache.

the class KNNClassificationTest method testBinaryClassificationWithSmallestKTest.

/**
 */
public void testBinaryClassificationWithSmallestKTest() {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    double[][] mtx = new double[][] { { 1.0, 1.0 }, { 1.0, 2.0 }, { 2.0, 1.0 }, { -1.0, -1.0 }, { -1.0, -2.0 }, { -2.0, -1.0 } };
    double[] lbs = new double[] { 1.0, 1.0, 1.0, 2.0, 2.0, 2.0 };
    LabeledDataset training = new LabeledDataset(mtx, lbs);
    KNNModel knnMdl = new KNNModel(1, new EuclideanDistance(), KNNStrategy.SIMPLE, training);
    Vector firstVector = new DenseLocalOnHeapVector(new double[] { 2.0, 2.0 });
    assertEquals(knnMdl.apply(firstVector), 1.0);
    Vector secondVector = new DenseLocalOnHeapVector(new double[] { -2.0, -2.0 });
    assertEquals(knnMdl.apply(secondVector), 2.0);
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) KNNModel(org.apache.ignite.ml.knn.models.KNNModel) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) LabeledDataset(org.apache.ignite.ml.structures.LabeledDataset) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Example 3 with LabeledDataset

use of org.apache.ignite.ml.structures.LabeledDataset in project ignite by apache.

the class KNNClassificationTest method testBinaryClassificationFarPointsWithSimpleStrategy.

/**
 */
public void testBinaryClassificationFarPointsWithSimpleStrategy() {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    double[][] mtx = new double[][] { { 10.0, 10.0 }, { 10.0, 20.0 }, { -1, -1 }, { -2, -2 }, { -1.0, -2.0 }, { -2.0, -1.0 } };
    double[] lbs = new double[] { 1.0, 1.0, 1.0, 2.0, 2.0, 2.0 };
    LabeledDataset training = new LabeledDataset(mtx, lbs);
    KNNModel knnMdl = new KNNModel(3, new EuclideanDistance(), KNNStrategy.SIMPLE, training);
    Vector vector = new DenseLocalOnHeapVector(new double[] { -1.01, -1.01 });
    assertEquals(knnMdl.apply(vector), 2.0);
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) KNNModel(org.apache.ignite.ml.knn.models.KNNModel) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) LabeledDataset(org.apache.ignite.ml.structures.LabeledDataset) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Example 4 with LabeledDataset

use of org.apache.ignite.ml.structures.LabeledDataset in project ignite by apache.

the class KNNClassificationTest method testLargeKValue.

/**
 */
public void testLargeKValue() {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    double[][] mtx = new double[][] { { 10.0, 10.0 }, { 10.0, 20.0 }, { -1, -1 }, { -2, -2 }, { -1.0, -2.0 }, { -2.0, -1.0 } };
    double[] lbs = new double[] { 1.0, 1.0, 1.0, 2.0, 2.0, 2.0 };
    LabeledDataset training = new LabeledDataset(mtx, lbs);
    try {
        new KNNModel(7, new EuclideanDistance(), KNNStrategy.SIMPLE, training);
        fail("SmallTrainingDatasetSizeException");
    } catch (SmallTrainingDatasetSizeException e) {
        return;
    }
    fail("SmallTrainingDatasetSizeException");
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) SmallTrainingDatasetSizeException(org.apache.ignite.ml.math.exceptions.knn.SmallTrainingDatasetSizeException) KNNModel(org.apache.ignite.ml.knn.models.KNNModel) LabeledDataset(org.apache.ignite.ml.structures.LabeledDataset)

Example 5 with LabeledDataset

use of org.apache.ignite.ml.structures.LabeledDataset in project ignite by apache.

the class KNNMultipleLinearRegressionTest method testLonglyWithWeightedStrategyAndNormalization.

/**
 */
public void testLonglyWithWeightedStrategyAndNormalization() {
    y = new double[] { 60323, 61122, 60171, 61187, 63221, 63639, 64989, 63761, 66019, 68169, 66513, 68655, 69564, 69331, 70551 };
    x = new double[15][];
    x[0] = new double[] { 83.0, 234289, 2356, 1590, 107608, 1947 };
    x[1] = new double[] { 88.5, 259426, 2325, 1456, 108632, 1948 };
    x[2] = new double[] { 88.2, 258054, 3682, 1616, 109773, 1949 };
    x[3] = new double[] { 89.5, 284599, 3351, 1650, 110929, 1950 };
    x[4] = new double[] { 96.2, 328975, 2099, 3099, 112075, 1951 };
    x[5] = new double[] { 98.1, 346999, 1932, 3594, 113270, 1952 };
    x[6] = new double[] { 99.0, 365385, 1870, 3547, 115094, 1953 };
    x[7] = new double[] { 100.0, 363112, 3578, 3350, 116219, 1954 };
    x[8] = new double[] { 101.2, 397469, 2904, 3048, 117388, 1955 };
    x[9] = new double[] { 108.4, 442769, 2936, 2798, 120445, 1957 };
    x[10] = new double[] { 110.8, 444546, 4681, 2637, 121950, 1958 };
    x[11] = new double[] { 112.6, 482704, 3813, 2552, 123366, 1959 };
    x[12] = new double[] { 114.2, 502601, 3931, 2514, 125368, 1960 };
    x[13] = new double[] { 115.7, 518173, 4806, 2572, 127852, 1961 };
    x[14] = new double[] { 116.9, 554894, 4007, 2827, 130081, 1962 };
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    LabeledDataset training = new LabeledDataset(x, y);
    final LabeledDataset normalizedTrainingDataset = (LabeledDataset) Normalizer.normalizeWithMiniMax(training);
    KNNMultipleLinearRegression knnMdl = new KNNMultipleLinearRegression(5, new EuclideanDistance(), KNNStrategy.WEIGHTED, normalizedTrainingDataset);
    Vector vector = new DenseLocalOnHeapVector(new double[] { 104.6, 419180, 2822, 2857, 118734, 1956 });
    System.out.println(knnMdl.apply(vector));
    Assert.assertEquals(67857, knnMdl.apply(vector), 2000);
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) KNNMultipleLinearRegression(org.apache.ignite.ml.knn.regression.KNNMultipleLinearRegression) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) LabeledDataset(org.apache.ignite.ml.structures.LabeledDataset) Vector(org.apache.ignite.ml.math.Vector) SparseBlockDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Aggregations

LabeledDataset (org.apache.ignite.ml.structures.LabeledDataset)25 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)13 Vector (org.apache.ignite.ml.math.Vector)12 KNNModel (org.apache.ignite.ml.knn.models.KNNModel)10 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)10 KNNMultipleLinearRegression (org.apache.ignite.ml.knn.regression.KNNMultipleLinearRegression)6 LabeledDatasetTestTrainPair (org.apache.ignite.ml.structures.LabeledDatasetTestTrainPair)5 SparseBlockDistributedVector (org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector)4 IgniteThread (org.apache.ignite.thread.IgniteThread)4 Path (java.nio.file.Path)3 LabeledVector (org.apache.ignite.ml.structures.LabeledVector)3 File (java.io.File)2 IOException (java.io.IOException)2 Ignite (org.apache.ignite.Ignite)2 ManhattanDistance (org.apache.ignite.ml.math.distances.ManhattanDistance)2 ArrayList (java.util.ArrayList)1 KNNModelFormat (org.apache.ignite.ml.knn.models.KNNModelFormat)1 NoDataException (org.apache.ignite.ml.math.exceptions.NoDataException)1 EmptyFileException (org.apache.ignite.ml.math.exceptions.knn.EmptyFileException)1 FileParsingException (org.apache.ignite.ml.math.exceptions.knn.FileParsingException)1