Search in sources :

Example 1 with NNClassificationModel

use of org.apache.ignite.ml.knn.NNClassificationModel in project ignite by apache.

the class ANNClassificationExample method main.

/**
 * Run example.
 */
public static void main(String[] args) {
    System.out.println();
    System.out.println(">>> ANN multi-class classification algorithm over cached dataset usage example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println(">>> Ignite grid started.");
        IgniteCache<Integer, double[]> dataCache = null;
        try {
            dataCache = getTestCache(ignite);
            ANNClassificationTrainer trainer = new ANNClassificationTrainer().withDistance(new ManhattanDistance()).withK(50).withMaxIterations(1000).withEpsilon(1e-2);
            long startTrainingTime = System.currentTimeMillis();
            NNClassificationModel knnMdl = trainer.fit(ignite, dataCache, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST)).withK(5).withDistanceMeasure(new EuclideanDistance()).withWeighted(true);
            long endTrainingTime = System.currentTimeMillis();
            System.out.println(">>> ---------------------------------");
            System.out.println(">>> | Prediction\t| Ground Truth\t|");
            System.out.println(">>> ---------------------------------");
            int amountOfErrors = 0;
            int totalAmount = 0;
            long totalPredictionTime = 0L;
            try (QueryCursor<Cache.Entry<Integer, double[]>> observations = dataCache.query(new ScanQuery<>())) {
                for (Cache.Entry<Integer, double[]> observation : observations) {
                    double[] val = observation.getValue();
                    double[] inputs = Arrays.copyOfRange(val, 1, val.length);
                    double groundTruth = val[0];
                    long startPredictionTime = System.currentTimeMillis();
                    double prediction = knnMdl.predict(new DenseVector(inputs));
                    long endPredictionTime = System.currentTimeMillis();
                    totalPredictionTime += (endPredictionTime - startPredictionTime);
                    totalAmount++;
                    if (!Precision.equals(groundTruth, prediction, Precision.EPSILON))
                        amountOfErrors++;
                    System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);
                }
                System.out.println(">>> ---------------------------------");
                System.out.println("Training costs = " + (endTrainingTime - startTrainingTime));
                System.out.println("Prediction costs = " + totalPredictionTime);
                System.out.println("\n>>> Absolute amount of errors " + amountOfErrors);
                System.out.println("\n>>> Accuracy " + (1 - amountOfErrors / (double) totalAmount));
                System.out.println(totalAmount);
                System.out.println(">>> ANN multi-class classification algorithm over cached dataset usage example completed.");
            }
        } finally {
            dataCache.destroy();
        }
    } finally {
        System.out.flush();
    }
}
Also used : NNClassificationModel(org.apache.ignite.ml.knn.NNClassificationModel) EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) Ignite(org.apache.ignite.Ignite) ANNClassificationTrainer(org.apache.ignite.ml.knn.ann.ANNClassificationTrainer) ManhattanDistance(org.apache.ignite.ml.math.distances.ManhattanDistance) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 2 with NNClassificationModel

use of org.apache.ignite.ml.knn.NNClassificationModel in project ignite by apache.

the class LocalModelsTest method importExportANNModelTest.

/**
 */
@Test
public void importExportANNModelTest() throws IOException {
    executeModelTest(mdlFilePath -> {
        final LabeledVectorSet<LabeledVector> centers = new LabeledVectorSet<>();
        NNClassificationModel mdl = new ANNClassificationModel(centers, new ANNClassificationTrainer.CentroidStat()).withK(4).withDistanceMeasure(new ManhattanDistance()).withWeighted(true);
        Exporter<KNNModelFormat, String> exporter = new FileExporter<>();
        mdl.saveModel(exporter, mdlFilePath);
        ANNModelFormat load = (ANNModelFormat) exporter.load(mdlFilePath);
        Assert.assertNotNull(load);
        NNClassificationModel importedMdl = new ANNClassificationModel(load.getCandidates(), new ANNClassificationTrainer.CentroidStat()).withK(load.getK()).withDistanceMeasure(load.getDistanceMeasure()).withWeighted(true);
        Assert.assertEquals("", mdl, importedMdl);
        return null;
    });
}
Also used : ANNClassificationModel(org.apache.ignite.ml.knn.ann.ANNClassificationModel) NNClassificationModel(org.apache.ignite.ml.knn.NNClassificationModel) FileExporter(org.apache.ignite.ml.FileExporter) KNNModelFormat(org.apache.ignite.ml.knn.ann.KNNModelFormat) LabeledVector(org.apache.ignite.ml.structures.LabeledVector) ANNClassificationModel(org.apache.ignite.ml.knn.ann.ANNClassificationModel) ANNModelFormat(org.apache.ignite.ml.knn.ann.ANNModelFormat) LabeledVectorSet(org.apache.ignite.ml.structures.LabeledVectorSet) ManhattanDistance(org.apache.ignite.ml.math.distances.ManhattanDistance) Test(org.junit.Test)

Aggregations

NNClassificationModel (org.apache.ignite.ml.knn.NNClassificationModel)2 ManhattanDistance (org.apache.ignite.ml.math.distances.ManhattanDistance)2 Cache (javax.cache.Cache)1 Ignite (org.apache.ignite.Ignite)1 IgniteCache (org.apache.ignite.IgniteCache)1 FileExporter (org.apache.ignite.ml.FileExporter)1 ANNClassificationModel (org.apache.ignite.ml.knn.ann.ANNClassificationModel)1 ANNClassificationTrainer (org.apache.ignite.ml.knn.ann.ANNClassificationTrainer)1 ANNModelFormat (org.apache.ignite.ml.knn.ann.ANNModelFormat)1 KNNModelFormat (org.apache.ignite.ml.knn.ann.KNNModelFormat)1 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)1 DenseVector (org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)1 LabeledVector (org.apache.ignite.ml.structures.LabeledVector)1 LabeledVectorSet (org.apache.ignite.ml.structures.LabeledVectorSet)1 Test (org.junit.Test)1