Search in sources :

Example 1 with OnMajorityPredictionsAggregator

use of org.apache.ignite.ml.composition.predictionsaggregator.OnMajorityPredictionsAggregator in project ignite by apache.

the class Step_10_Bagging method main.

/**
 * Run example.
 */
public static void main(String[] args) {
    System.out.println();
    System.out.println(">>> Tutorial step 10 (Bagging) example started.");
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        try {
            IgniteCache<Integer, Vector> dataCache = TitanicUtils.readPassengers(ignite);
            // Extracts "pclass", "sibsp", "parch", "sex", "embarked", "age", "fare".
            final Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>(0, 3, 4, 5, 6, 8, 10).labeled(1);
            TrainTestSplit<Integer, Vector> split = new TrainTestDatasetSplitter<Integer, Vector>().split(0.75);
            Preprocessor<Integer, Vector> strEncoderPreprocessor = new EncoderTrainer<Integer, Vector>().withEncoderType(EncoderType.STRING_ENCODER).withEncodedFeature(1).withEncodedFeature(// <--- Changed index here.
            6).fit(ignite, dataCache, vectorizer);
            Preprocessor<Integer, Vector> imputingPreprocessor = new ImputerTrainer<Integer, Vector>().fit(ignite, dataCache, strEncoderPreprocessor);
            Preprocessor<Integer, Vector> minMaxScalerPreprocessor = new MinMaxScalerTrainer<Integer, Vector>().fit(ignite, dataCache, imputingPreprocessor);
            Preprocessor<Integer, Vector> normalizationPreprocessor = new NormalizationTrainer<Integer, Vector>().withP(1).fit(ignite, dataCache, minMaxScalerPreprocessor);
            DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0);
            BaggedTrainer<Double> baggedTrainer = TrainerTransformers.makeBagged(trainer, 10, 0.6, 4, 3, new OnMajorityPredictionsAggregator()).withEnvironmentBuilder(LearningEnvironmentBuilder.defaultBuilder().withRNGSeed(1));
            BaggedModel mdl = baggedTrainer.fit(ignite, dataCache, split.getTrainFilter(), normalizationPreprocessor);
            System.out.println("\n>>> Trained model: " + mdl);
            double accuracy = Evaluator.evaluate(dataCache, split.getTestFilter(), mdl, normalizationPreprocessor, MetricName.ACCURACY);
            System.out.println("\n>>> Accuracy " + accuracy);
            System.out.println("\n>>> Test Error " + (1 - accuracy));
            System.out.println(">>> Tutorial step 10 (Bagging) example completed.");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    } finally {
        System.out.flush();
    }
}
Also used : OnMajorityPredictionsAggregator(org.apache.ignite.ml.composition.predictionsaggregator.OnMajorityPredictionsAggregator) FileNotFoundException(java.io.FileNotFoundException) NormalizationTrainer(org.apache.ignite.ml.preprocessing.normalization.NormalizationTrainer) DecisionTreeClassificationTrainer(org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer) Ignite(org.apache.ignite.Ignite) EncoderTrainer(org.apache.ignite.ml.preprocessing.encoding.EncoderTrainer) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) BaggedModel(org.apache.ignite.ml.composition.bagging.BaggedModel)

Example 2 with OnMajorityPredictionsAggregator

use of org.apache.ignite.ml.composition.predictionsaggregator.OnMajorityPredictionsAggregator in project ignite by apache.

the class BaggedLogisticRegressionSGDTrainerExample method main.

/**
 * Run example.
 */
public static void main(String[] args) throws IOException {
    System.out.println();
    System.out.println(">>> Logistic regression model over partitioned 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);
            System.out.println(">>> Create new logistic regression trainer object.");
            LogisticRegressionSGDTrainer trainer = new LogisticRegressionSGDTrainer().withUpdatesStgy(new UpdatesStrategy<>(new SimpleGDUpdateCalculator(0.2), SimpleGDParameterUpdate.SUM_LOCAL, SimpleGDParameterUpdate.AVG)).withMaxIterations(100).withLocIterations(10).withBatchSize(10).withSeed(123L);
            System.out.println(">>> Perform the training to get the model.");
            BaggedTrainer<Double> baggedTrainer = TrainerTransformers.makeBagged(trainer, 10, 0.6, 4, 3, new OnMajorityPredictionsAggregator()).withEnvironmentBuilder(LearningEnvironmentBuilder.defaultBuilder().withRNGSeed(1));
            System.out.println(">>> Perform evaluation of the model.");
            Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
            double accuracy = Evaluator.evaluate(dataCache, baggedTrainer.fit(ignite, dataCache, vectorizer), vectorizer, MetricName.ACCURACY);
            System.out.println(">>> ---------------------------------");
            System.out.println("\n>>> Accuracy " + accuracy);
            System.out.println(">>> Bagged logistic regression model over partitioned dataset usage example completed.");
        } finally {
            if (dataCache != null)
                dataCache.destroy();
        }
    } finally {
        System.out.flush();
    }
}
Also used : LogisticRegressionSGDTrainer(org.apache.ignite.ml.regressions.logistic.LogisticRegressionSGDTrainer) SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) OnMajorityPredictionsAggregator(org.apache.ignite.ml.composition.predictionsaggregator.OnMajorityPredictionsAggregator) SimpleGDUpdateCalculator(org.apache.ignite.ml.optimization.updatecalculators.SimpleGDUpdateCalculator) Ignite(org.apache.ignite.Ignite) Vector(org.apache.ignite.ml.math.primitives.vector.Vector)

Example 3 with OnMajorityPredictionsAggregator

use of org.apache.ignite.ml.composition.predictionsaggregator.OnMajorityPredictionsAggregator in project ignite by apache.

the class BaggingTest method testNaiveBaggingLogRegression.

/**
 * Test that bagged log regression makes correct predictions.
 */
@Test
public void testNaiveBaggingLogRegression() {
    Map<Integer, double[]> cacheMock = getCacheMock(twoLinearlySeparableClasses);
    DatasetTrainer<LogisticRegressionModel, Double> trainer = new LogisticRegressionSGDTrainer().withUpdatesStgy(new UpdatesStrategy<>(new SimpleGDUpdateCalculator(0.2), SimpleGDParameterUpdate.SUM_LOCAL, SimpleGDParameterUpdate.AVG)).withMaxIterations(30000).withLocIterations(100).withBatchSize(10).withSeed(123L);
    BaggedTrainer<Double> baggedTrainer = TrainerTransformers.makeBagged(trainer, 7, 0.7, 2, 2, new OnMajorityPredictionsAggregator()).withEnvironmentBuilder(TestUtils.testEnvBuilder());
    BaggedModel mdl = baggedTrainer.fit(cacheMock, parts, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST));
    Vector weights = ((LogisticRegressionModel) ((AdaptableDatasetModel) ((ModelsParallelComposition) ((AdaptableDatasetModel) mdl.model()).innerModel()).submodels().get(0)).innerModel()).weights();
    TestUtils.assertEquals(firstMdlWeights.get(parts), weights, 0.0);
    TestUtils.assertEquals(0, mdl.predict(VectorUtils.of(100, 10)), PRECISION);
    TestUtils.assertEquals(1, mdl.predict(VectorUtils.of(10, 100)), PRECISION);
}
Also used : LogisticRegressionSGDTrainer(org.apache.ignite.ml.regressions.logistic.LogisticRegressionSGDTrainer) DoubleArrayVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer) OnMajorityPredictionsAggregator(org.apache.ignite.ml.composition.predictionsaggregator.OnMajorityPredictionsAggregator) LogisticRegressionModel(org.apache.ignite.ml.regressions.logistic.LogisticRegressionModel) SimpleGDUpdateCalculator(org.apache.ignite.ml.optimization.updatecalculators.SimpleGDUpdateCalculator) AdaptableDatasetModel(org.apache.ignite.ml.trainers.AdaptableDatasetModel) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) TrainerTest(org.apache.ignite.ml.common.TrainerTest) Test(org.junit.Test)

Aggregations

OnMajorityPredictionsAggregator (org.apache.ignite.ml.composition.predictionsaggregator.OnMajorityPredictionsAggregator)3 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)3 Ignite (org.apache.ignite.Ignite)2 SimpleGDUpdateCalculator (org.apache.ignite.ml.optimization.updatecalculators.SimpleGDUpdateCalculator)2 LogisticRegressionSGDTrainer (org.apache.ignite.ml.regressions.logistic.LogisticRegressionSGDTrainer)2 FileNotFoundException (java.io.FileNotFoundException)1 SandboxMLCache (org.apache.ignite.examples.ml.util.SandboxMLCache)1 TrainerTest (org.apache.ignite.ml.common.TrainerTest)1 BaggedModel (org.apache.ignite.ml.composition.bagging.BaggedModel)1 DoubleArrayVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer)1 EncoderTrainer (org.apache.ignite.ml.preprocessing.encoding.EncoderTrainer)1 NormalizationTrainer (org.apache.ignite.ml.preprocessing.normalization.NormalizationTrainer)1 LogisticRegressionModel (org.apache.ignite.ml.regressions.logistic.LogisticRegressionModel)1 AdaptableDatasetModel (org.apache.ignite.ml.trainers.AdaptableDatasetModel)1 DecisionTreeClassificationTrainer (org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer)1 Test (org.junit.Test)1