Search in sources :

Example 16 with DecisionTreeClassificationTrainer

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

the class CrossValidationExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String... args) {
    System.out.println(">>> Cross validation score calculator example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println(">>> Ignite grid started.");
        // Create cache with training data.
        CacheConfiguration<Integer, LabeledVector<Double>> trainingSetCfg = new CacheConfiguration<>();
        trainingSetCfg.setName("TRAINING_SET");
        trainingSetCfg.setAffinity(new RendezvousAffinityFunction(false, 10));
        IgniteCache<Integer, LabeledVector<Double>> trainingSet = null;
        try {
            trainingSet = ignite.createCache(trainingSetCfg);
            Random rnd = new Random(0);
            // Fill training data.
            for (int i = 0; i < 1000; i++) trainingSet.put(i, generatePoint(rnd));
            // Create classification trainer.
            DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(4, 0);
            LabeledDummyVectorizer<Integer, Double> vectorizer = new LabeledDummyVectorizer<>();
            CrossValidation<DecisionTreeModel, Integer, LabeledVector<Double>> scoreCalculator = new CrossValidation<>();
            double[] accuracyScores = scoreCalculator.withIgnite(ignite).withUpstreamCache(trainingSet).withTrainer(trainer).withMetric(MetricName.ACCURACY).withPreprocessor(vectorizer).withAmountOfFolds(4).isRunningOnPipeline(false).scoreByFolds();
            System.out.println(">>> Accuracy: " + Arrays.toString(accuracyScores));
            double[] balancedAccuracyScores = scoreCalculator.withMetric(MetricName.ACCURACY).scoreByFolds();
            System.out.println(">>> Balanced Accuracy: " + Arrays.toString(balancedAccuracyScores));
            System.out.println(">>> Cross validation score calculator example completed.");
        } finally {
            trainingSet.destroy();
        }
    } finally {
        System.out.flush();
    }
}
Also used : DecisionTreeModel(org.apache.ignite.ml.tree.DecisionTreeModel) LabeledVector(org.apache.ignite.ml.structures.LabeledVector) LabeledDummyVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.LabeledDummyVectorizer) DecisionTreeClassificationTrainer(org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer) Random(java.util.Random) Ignite(org.apache.ignite.Ignite) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) CrossValidation(org.apache.ignite.ml.selection.cv.CrossValidation) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 17 with DecisionTreeClassificationTrainer

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

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

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

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

Aggregations

DecisionTreeClassificationTrainer (org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer)31 Ignite (org.apache.ignite.Ignite)28 DecisionTreeModel (org.apache.ignite.ml.tree.DecisionTreeModel)26 FileNotFoundException (java.io.FileNotFoundException)21 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)21 EncoderTrainer (org.apache.ignite.ml.preprocessing.encoding.EncoderTrainer)14 NormalizationTrainer (org.apache.ignite.ml.preprocessing.normalization.NormalizationTrainer)9 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 SandboxMLCache (org.apache.ignite.examples.ml.util.SandboxMLCache)5 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)4 LabeledVector (org.apache.ignite.ml.structures.LabeledVector)4 HashMap (java.util.HashMap)3 Random (java.util.Random)3 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)3 LabeledDummyVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.LabeledDummyVectorizer)3 MinMaxScalerTrainer (org.apache.ignite.ml.preprocessing.minmaxscaling.MinMaxScalerTrainer)3 Test (org.junit.Test)3 List (java.util.List)2