Search in sources :

Example 16 with SandboxMLCache

use of org.apache.ignite.examples.ml.util.SandboxMLCache in project ignite by apache.

the class EncoderExample method main.

/**
 * Run example.
 */
public static void main(String[] args) {
    System.out.println();
    System.out.println(">>> Train Decision Tree model on mushrooms.csv dataset.");
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        try {
            IgniteCache<Integer, Object[]> dataCache = new SandboxMLCache(ignite).fillObjectCacheWithDoubleLabels(MLSandboxDatasets.MUSHROOMS);
            final Vectorizer<Integer, Object[], Integer, Object> vectorizer = new ObjectArrayVectorizer<Integer>(1, 2, 3).labeled(0);
            Preprocessor<Integer, Object[]> encoderPreprocessor = new EncoderTrainer<Integer, Object[]>().withEncoderType(EncoderType.STRING_ENCODER).withEncodedFeature(0).withEncodedFeature(1).withEncodedFeature(2).fit(ignite, dataCache, vectorizer);
            DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0);
            // Train decision tree model.
            DecisionTreeModel mdl = trainer.fit(ignite, dataCache, encoderPreprocessor);
            System.out.println("\n>>> Trained model: " + mdl);
            double accuracy = Evaluator.evaluate(dataCache, mdl, encoderPreprocessor, new Accuracy<>());
            System.out.println("\n>>> Accuracy " + accuracy);
            System.out.println("\n>>> Test Error " + (1 - accuracy));
            System.out.println(">>> Train Decision Tree model on mushrooms.csv dataset.");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    } finally {
        System.out.flush();
    }
}
Also used : SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) DecisionTreeModel(org.apache.ignite.ml.tree.DecisionTreeModel) FileNotFoundException(java.io.FileNotFoundException) DecisionTreeClassificationTrainer(org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer) Ignite(org.apache.ignite.Ignite)

Example 17 with SandboxMLCache

use of org.apache.ignite.examples.ml.util.SandboxMLCache in project ignite by apache.

the class LabelEncoderExample method main.

/**
 * Run example.
 */
public static void main(String[] args) {
    System.out.println();
    System.out.println(">>> Train Decision Tree model on mushrooms.csv dataset.");
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        try {
            IgniteCache<Integer, Object[]> dataCache = new SandboxMLCache(ignite).fillObjectCacheWithCategoricalData(MLSandboxDatasets.MUSHROOMS);
            final Vectorizer<Integer, Object[], Integer, Object> vectorizer = new ObjectArrayVectorizer<Integer>(1, 2).labeled(0);
            Preprocessor<Integer, Object[]> strEncoderPreprocessor = new EncoderTrainer<Integer, Object[]>().withEncoderType(EncoderType.STRING_ENCODER).withEncodedFeature(0).withEncodedFeature(1).fit(ignite, dataCache, vectorizer);
            Preprocessor<Integer, Object[]> lbEncoderPreprocessor = new EncoderTrainer<Integer, Object[]>().withEncoderType(EncoderType.LABEL_ENCODER).fit(ignite, dataCache, strEncoderPreprocessor);
            DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0);
            // Train decision tree model.
            DecisionTreeModel mdl = trainer.fit(ignite, dataCache, lbEncoderPreprocessor);
            System.out.println("\n>>> Trained model: " + mdl);
            double accuracy = Evaluator.evaluate(dataCache, mdl, lbEncoderPreprocessor, new Accuracy());
            System.out.println("\n>>> Accuracy " + accuracy);
            System.out.println("\n>>> Test Error " + (1 - accuracy));
            System.out.println(">>> Train Decision Tree model on mushrooms.csv dataset.");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    } finally {
        System.out.flush();
    }
}
Also used : SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) DecisionTreeModel(org.apache.ignite.ml.tree.DecisionTreeModel) FileNotFoundException(java.io.FileNotFoundException) DecisionTreeClassificationTrainer(org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer) Accuracy(org.apache.ignite.ml.selection.scoring.metric.classification.Accuracy) Ignite(org.apache.ignite.Ignite) EncoderTrainer(org.apache.ignite.ml.preprocessing.encoding.EncoderTrainer)

Example 18 with SandboxMLCache

use of org.apache.ignite.examples.ml.util.SandboxMLCache in project ignite by apache.

the class MovieLensExample method loadMovieLensDataset.

/**
 * Loads MovieLens dataset into cache.
 *
 * @param ignite Ignite instance.
 * @param cnt Number of rating point to be loaded.
 * @return Ignite cache with loaded MovieLens dataset.
 * @throws IOException If dataset not found.
 */
private static IgniteCache<Integer, RatingPoint> loadMovieLensDataset(Ignite ignite, int cnt) throws IOException {
    CacheConfiguration<Integer, RatingPoint> cacheConfiguration = new CacheConfiguration<>();
    cacheConfiguration.setAffinity(new RendezvousAffinityFunction(false, 100));
    cacheConfiguration.setName("MOVIELENS");
    IgniteCache<Integer, RatingPoint> dataCache = ignite.createCache(cacheConfiguration);
    int seq = 0;
    for (String s : new SandboxMLCache(ignite).loadDataset(MLSandboxDatasets.MOVIELENS)) {
        String[] line = s.split(",");
        int userId = Integer.valueOf(line[0]);
        int movieId = Integer.valueOf(line[1]);
        double rating = Double.valueOf(line[2]);
        dataCache.put(seq++, new RatingPoint(movieId, userId, rating));
        if (seq == cnt)
            break;
    }
    return dataCache;
}
Also used : SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 19 with SandboxMLCache

use of org.apache.ignite.examples.ml.util.SandboxMLCache in project ignite by apache.

the class BostonHousePricesPredictionExample 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.BOSTON_HOUSE_PRICES);
            DatasetTrainer<LinearRegressionModel, Double> trainer = new LinearRegressionLSQRTrainer().withEnvironmentBuilder(LearningEnvironmentBuilder.defaultBuilder().withRNGSeed(0));
            // 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 80/20 proportion.
            TrainTestSplit<Integer, Vector> split = new TrainTestDatasetSplitter<Integer, Vector>().split(0.8);
            System.out.println(">>> Start training.");
            LinearRegressionModel mdl = trainer.fit(ignite, dataCache, split.getTrainFilter(), vectorizer);
            System.out.println(">>> Perform scoring.");
            double score = Evaluator.evaluate(dataCache, split.getTestFilter(), mdl, vectorizer, MetricName.R2);
            System.out.println(">>> Model: " + toString(mdl));
            System.out.println(">>> R^2 score: " + score);
        } finally {
            if (dataCache != null)
                dataCache.destroy();
        }
    } finally {
        System.out.flush();
    }
}
Also used : SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) LinearRegressionModel(org.apache.ignite.ml.regressions.linear.LinearRegressionModel) LinearRegressionLSQRTrainer(org.apache.ignite.ml.regressions.linear.LinearRegressionLSQRTrainer) Ignite(org.apache.ignite.Ignite) Vector(org.apache.ignite.ml.math.primitives.vector.Vector)

Example 20 with SandboxMLCache

use of org.apache.ignite.examples.ml.util.SandboxMLCache in project ignite by apache.

the class KNNClassificationExample method main.

/**
 * Run example.
 */
public static void main(String[] args) throws IOException {
    System.out.println();
    System.out.println(">>> kNN 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, Vector> dataCache = null;
        try {
            dataCache = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.TWO_CLASSED_IRIS);
            KNNClassificationTrainer trainer = new KNNClassificationTrainer().withK(3).withDistanceMeasure(new EuclideanDistance()).withWeighted(true).withDataTtl(60);
            Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
            KNNClassificationModel mdl = trainer.fit(ignite, dataCache, vectorizer);
            double accuracy = Evaluator.evaluate(dataCache, mdl, vectorizer, MetricName.ACCURACY);
            System.out.println("\n>>> Accuracy " + accuracy);
        } finally {
            if (dataCache != null)
                dataCache.destroy();
        }
    } finally {
        System.out.flush();
    }
}
Also used : SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) KNNClassificationTrainer(org.apache.ignite.ml.knn.classification.KNNClassificationTrainer) Ignite(org.apache.ignite.Ignite) KNNClassificationModel(org.apache.ignite.ml.knn.classification.KNNClassificationModel) Vector(org.apache.ignite.ml.math.primitives.vector.Vector)

Aggregations

SandboxMLCache (org.apache.ignite.examples.ml.util.SandboxMLCache)41 Ignite (org.apache.ignite.Ignite)38 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)34 Path (java.nio.file.Path)9 IgniteCache (org.apache.ignite.IgniteCache)7 LinearRegressionModel (org.apache.ignite.ml.regressions.linear.LinearRegressionModel)7 Cache (javax.cache.Cache)6 LinearRegressionLSQRTrainer (org.apache.ignite.ml.regressions.linear.LinearRegressionLSQRTrainer)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 DummyVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer)5 DecisionTreeClassificationTrainer (org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer)5 FileNotFoundException (java.io.FileNotFoundException)4 FeatureMeta (org.apache.ignite.ml.dataset.feature.FeatureMeta)4 GaussianNaiveBayesTrainer (org.apache.ignite.ml.naivebayes.gaussian.GaussianNaiveBayesTrainer)4 LogisticRegressionSGDTrainer (org.apache.ignite.ml.regressions.logistic.LogisticRegressionSGDTrainer)4 KMeansModel (org.apache.ignite.ml.clustering.kmeans.KMeansModel)3 KMeansTrainer (org.apache.ignite.ml.clustering.kmeans.KMeansTrainer)3 ModelsComposition (org.apache.ignite.ml.composition.ModelsComposition)3 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)3 DiscreteNaiveBayesTrainer (org.apache.ignite.ml.naivebayes.discrete.DiscreteNaiveBayesTrainer)3