Search in sources :

Example 1 with SVMLinearMultiClassClassificationModel

use of org.apache.ignite.ml.svm.SVMLinearMultiClassClassificationModel in project ignite by apache.

the class SVMMultiClassClassificationExample method main.

/**
 * Run example.
 */
public static void main(String[] args) throws InterruptedException {
    System.out.println();
    System.out.println(">>> SVM Multi-class classification model 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.");
        IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), SVMMultiClassClassificationExample.class.getSimpleName(), () -> {
            IgniteCache<Integer, double[]> dataCache = getTestCache(ignite);
            SVMLinearMultiClassClassificationTrainer<Integer, double[]> trainer = new SVMLinearMultiClassClassificationTrainer<>();
            SVMLinearMultiClassClassificationModel mdl = trainer.fit(new CacheBasedDatasetBuilder<>(ignite, dataCache), (k, v) -> Arrays.copyOfRange(v, 1, v.length), (k, v) -> v[0], 5);
            System.out.println(">>> SVM Multi-class model");
            System.out.println(mdl.toString());
            NormalizationTrainer<Integer, double[]> normalizationTrainer = new NormalizationTrainer<>();
            NormalizationPreprocessor<Integer, double[]> preprocessor = normalizationTrainer.fit(new CacheBasedDatasetBuilder<>(ignite, dataCache), (k, v) -> Arrays.copyOfRange(v, 1, v.length), 5);
            SVMLinearMultiClassClassificationModel mdlWithNormalization = trainer.fit(new CacheBasedDatasetBuilder<>(ignite, dataCache), preprocessor, (k, v) -> v[0], 5);
            System.out.println(">>> SVM Multi-class model with normalization");
            System.out.println(mdlWithNormalization.toString());
            System.out.println(">>> ----------------------------------------------------------------");
            System.out.println(">>> | Prediction\t| Prediction with Normalization\t| Ground Truth\t|");
            System.out.println(">>> ----------------------------------------------------------------");
            int amountOfErrors = 0;
            int amountOfErrorsWithNormalization = 0;
            int totalAmount = 0;
            // Build confusion matrix. See https://en.wikipedia.org/wiki/Confusion_matrix
            int[][] confusionMtx = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
            int[][] confusionMtxWithNormalization = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
            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];
                    double prediction = mdl.apply(new DenseLocalOnHeapVector(inputs));
                    double predictionWithNormalization = mdlWithNormalization.apply(new DenseLocalOnHeapVector(inputs));
                    totalAmount++;
                    // Collect data for model
                    if (groundTruth != prediction)
                        amountOfErrors++;
                    int idx1 = (int) prediction == 1 ? 0 : ((int) prediction == 3 ? 1 : 2);
                    int idx2 = (int) groundTruth == 1 ? 0 : ((int) groundTruth == 3 ? 1 : 2);
                    confusionMtx[idx1][idx2]++;
                    // Collect data for model with normalization
                    if (groundTruth != predictionWithNormalization)
                        amountOfErrorsWithNormalization++;
                    idx1 = (int) predictionWithNormalization == 1 ? 0 : ((int) predictionWithNormalization == 3 ? 1 : 2);
                    idx2 = (int) groundTruth == 1 ? 0 : ((int) groundTruth == 3 ? 1 : 2);
                    confusionMtxWithNormalization[idx1][idx2]++;
                    System.out.printf(">>> | %.4f\t\t| %.4f\t\t\t\t\t\t| %.4f\t\t|\n", prediction, predictionWithNormalization, groundTruth);
                }
                System.out.println(">>> ----------------------------------------------------------------");
                System.out.println("\n>>> -----------------SVM model-------------");
                System.out.println("\n>>> Absolute amount of errors " + amountOfErrors);
                System.out.println("\n>>> Accuracy " + (1 - amountOfErrors / (double) totalAmount));
                System.out.println("\n>>> Confusion matrix is " + Arrays.deepToString(confusionMtx));
                System.out.println("\n>>> -----------------SVM model with Normalization-------------");
                System.out.println("\n>>> Absolute amount of errors " + amountOfErrorsWithNormalization);
                System.out.println("\n>>> Accuracy " + (1 - amountOfErrorsWithNormalization / (double) totalAmount));
                System.out.println("\n>>> Confusion matrix is " + Arrays.deepToString(confusionMtxWithNormalization));
            }
        });
        igniteThread.start();
        igniteThread.join();
    }
}
Also used : SVMLinearMultiClassClassificationModel(org.apache.ignite.ml.svm.SVMLinearMultiClassClassificationModel) SVMLinearMultiClassClassificationTrainer(org.apache.ignite.ml.svm.SVMLinearMultiClassClassificationTrainer) NormalizationTrainer(org.apache.ignite.ml.preprocessing.normalization.NormalizationTrainer) Ignite(org.apache.ignite.Ignite) IgniteThread(org.apache.ignite.thread.IgniteThread) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 2 with SVMLinearMultiClassClassificationModel

use of org.apache.ignite.ml.svm.SVMLinearMultiClassClassificationModel in project ignite by apache.

the class LocalModelsTest method importExportSVMMulticlassClassificationModelTest.

/**
 */
@Test
public void importExportSVMMulticlassClassificationModelTest() throws IOException {
    executeModelTest(mdlFilePath -> {
        SVMLinearBinaryClassificationModel binaryMdl1 = new SVMLinearBinaryClassificationModel(new DenseLocalOnHeapVector(new double[] { 1, 2 }), 3);
        SVMLinearBinaryClassificationModel binaryMdl2 = new SVMLinearBinaryClassificationModel(new DenseLocalOnHeapVector(new double[] { 2, 3 }), 4);
        SVMLinearBinaryClassificationModel binaryMdl3 = new SVMLinearBinaryClassificationModel(new DenseLocalOnHeapVector(new double[] { 3, 4 }), 5);
        SVMLinearMultiClassClassificationModel mdl = new SVMLinearMultiClassClassificationModel();
        mdl.add(1, binaryMdl1);
        mdl.add(2, binaryMdl2);
        mdl.add(3, binaryMdl3);
        Exporter<SVMLinearMultiClassClassificationModel, String> exporter = new FileExporter<>();
        mdl.saveModel(exporter, mdlFilePath);
        SVMLinearMultiClassClassificationModel load = exporter.load(mdlFilePath);
        Assert.assertNotNull(load);
        Assert.assertEquals("", mdl, load);
        return null;
    });
}
Also used : SVMLinearMultiClassClassificationModel(org.apache.ignite.ml.svm.SVMLinearMultiClassClassificationModel) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) SVMLinearBinaryClassificationModel(org.apache.ignite.ml.svm.SVMLinearBinaryClassificationModel) Test(org.junit.Test)

Aggregations

DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)2 SVMLinearMultiClassClassificationModel (org.apache.ignite.ml.svm.SVMLinearMultiClassClassificationModel)2 Cache (javax.cache.Cache)1 Ignite (org.apache.ignite.Ignite)1 IgniteCache (org.apache.ignite.IgniteCache)1 NormalizationTrainer (org.apache.ignite.ml.preprocessing.normalization.NormalizationTrainer)1 SVMLinearBinaryClassificationModel (org.apache.ignite.ml.svm.SVMLinearBinaryClassificationModel)1 SVMLinearMultiClassClassificationTrainer (org.apache.ignite.ml.svm.SVMLinearMultiClassClassificationTrainer)1 IgniteThread (org.apache.ignite.thread.IgniteThread)1 Test (org.junit.Test)1