Search in sources :

Example 16 with DecisionTreeModel

use of org.apache.ignite.ml.tree.DecisionTreeModel in project ignite by apache.

the class CrossValidationTest method testScoreWithBadDataset.

/**
 */
@Test
public void testScoreWithBadDataset() {
    Map<Integer, double[]> data = new HashMap<>();
    for (int i = 0; i < 1000; i++) data.put(i, new double[] { i, i % 2 == 0 ? 1.0 : 0.0 });
    Vectorizer<Integer, double[], Integer, Double> vectorizer = new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST);
    DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(1, 0);
    DebugCrossValidation<DecisionTreeModel, Integer, double[]> scoreCalculator = new DebugCrossValidation<>();
    int folds = 4;
    scoreCalculator.withUpstreamMap(data).withAmountOfParts(1).withTrainer(trainer).withMetric(MetricName.ACCURACY).withPreprocessor(vectorizer).withAmountOfFolds(folds).isRunningOnPipeline(false);
    double[] scores = scoreCalculator.scoreByFolds();
    assertEquals(folds, scores.length);
    for (int i = 0; i < folds; i++) assertTrue(scores[i] < 0.6);
}
Also used : DecisionTreeClassificationTrainer(org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer) HashMap(java.util.HashMap) DecisionTreeModel(org.apache.ignite.ml.tree.DecisionTreeModel) Test(org.junit.Test)

Example 17 with DecisionTreeModel

use of org.apache.ignite.ml.tree.DecisionTreeModel in project ignite by apache.

the class CrossValidationTest method testScoreWithGoodDatasetAndBinaryMetrics.

/**
 */
@Test
public void testScoreWithGoodDatasetAndBinaryMetrics() {
    Map<Integer, double[]> data = new HashMap<>();
    for (int i = 0; i < 1000; i++) data.put(i, new double[] { i > 500 ? 1.0 : 0.0, i });
    Vectorizer<Integer, double[], Integer, Double> vectorizer = new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
    DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(1, 0);
    DebugCrossValidation<DecisionTreeModel, Integer, double[]> scoreCalculator = new DebugCrossValidation<>();
    int folds = 4;
    scoreCalculator.withUpstreamMap(data).withAmountOfParts(1).withTrainer(trainer).withMetric(MetricName.ACCURACY).withPreprocessor(vectorizer).withAmountOfFolds(folds).isRunningOnPipeline(false);
    verifyScores(folds, scoreCalculator.scoreByFolds());
}
Also used : DecisionTreeClassificationTrainer(org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer) HashMap(java.util.HashMap) DecisionTreeModel(org.apache.ignite.ml.tree.DecisionTreeModel) Test(org.junit.Test)

Example 18 with DecisionTreeModel

use of org.apache.ignite.ml.tree.DecisionTreeModel in project ignite by apache.

the class CrossValidationTest method testScoreWithGoodDataset.

/**
 */
@Test
public void testScoreWithGoodDataset() {
    Map<Integer, double[]> data = new HashMap<>();
    for (int i = 0; i < 1000; i++) data.put(i, new double[] { i > 500 ? 1.0 : 0.0, i });
    DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(1, 0);
    DebugCrossValidation<DecisionTreeModel, Integer, double[]> scoreCalculator = new DebugCrossValidation<>();
    Vectorizer<Integer, double[], Integer, Double> vectorizer = new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
    int folds = 4;
    scoreCalculator.withUpstreamMap(data).withAmountOfParts(1).withTrainer(trainer).withMetric(MetricName.ACCURACY).withPreprocessor(vectorizer).withAmountOfFolds(folds).isRunningOnPipeline(false);
    verifyScores(folds, scoreCalculator.scoreByFolds());
}
Also used : DecisionTreeClassificationTrainer(org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer) HashMap(java.util.HashMap) DecisionTreeModel(org.apache.ignite.ml.tree.DecisionTreeModel) Test(org.junit.Test)

Example 19 with DecisionTreeModel

use of org.apache.ignite.ml.tree.DecisionTreeModel 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 20 with DecisionTreeModel

use of org.apache.ignite.ml.tree.DecisionTreeModel 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)

Aggregations

DecisionTreeModel (org.apache.ignite.ml.tree.DecisionTreeModel)32 Ignite (org.apache.ignite.Ignite)27 DecisionTreeClassificationTrainer (org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer)26 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)20 FileNotFoundException (java.io.FileNotFoundException)18 EncoderTrainer (org.apache.ignite.ml.preprocessing.encoding.EncoderTrainer)12 CrossValidation (org.apache.ignite.ml.selection.cv.CrossValidation)9 CrossValidationResult (org.apache.ignite.ml.selection.cv.CrossValidationResult)7 ParamGrid (org.apache.ignite.ml.selection.paramgrid.ParamGrid)7 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)6 LabeledVector (org.apache.ignite.ml.structures.LabeledVector)6 HashMap (java.util.HashMap)5 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)5 NormalizationTrainer (org.apache.ignite.ml.preprocessing.normalization.NormalizationTrainer)5 Test (org.junit.Test)4 Random (java.util.Random)3 SandboxMLCache (org.apache.ignite.examples.ml.util.SandboxMLCache)3 LabeledDummyVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.LabeledDummyVectorizer)3 Path (java.nio.file.Path)2 List (java.util.List)2