Search in sources :

Example 1 with RandomForestRegressionTrainer

use of org.apache.ignite.ml.tree.randomforest.RandomForestRegressionTrainer in project ignite by apache.

the class RandomForestRegressionExportImportExample method main.

/**
 * Run example.
 */
public static void main(String[] args) throws IOException {
    System.out.println();
    System.out.println(">>> Random Forest regression algorithm over cached dataset usage example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println("\n>>> Ignite grid started.");
        IgniteCache<Integer, Vector> dataCache = null;
        Path jsonMdlPath = null;
        try {
            dataCache = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.BOSTON_HOUSE_PRICES);
            AtomicInteger idx = new AtomicInteger(0);
            RandomForestRegressionTrainer trainer = new RandomForestRegressionTrainer(IntStream.range(0, dataCache.get(1).size() - 1).mapToObj(x -> new FeatureMeta("", idx.getAndIncrement(), false)).collect(Collectors.toList())).withAmountOfTrees(101).withFeaturesCountSelectionStrgy(FeaturesCountSelectionStrategies.ONE_THIRD).withMaxDepth(4).withMinImpurityDelta(0.).withSubSampleSize(0.3).withSeed(0);
            trainer.withEnvironmentBuilder(LearningEnvironmentBuilder.defaultBuilder().withParallelismStrategyTypeDependency(ParallelismStrategy.ON_DEFAULT_POOL).withLoggingFactoryDependency(ConsoleLogger.Factory.LOW));
            System.out.println("\n>>> Configured trainer: " + trainer.getClass().getSimpleName());
            Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
            RandomForestModel mdl = trainer.fit(ignite, dataCache, vectorizer);
            System.out.println("\n>>> Exported Random Forest regression model: " + mdl.toString(true));
            double mae = evaluateModel(dataCache, mdl);
            System.out.println("\n>>> Mean absolute error (MAE) for exported Random Forest regression model " + mae);
            jsonMdlPath = Files.createTempFile(null, null);
            mdl.toJSON(jsonMdlPath);
            RandomForestModel modelImportedFromJSON = RandomForestModel.fromJSON(jsonMdlPath);
            System.out.println("\n>>> Exported Random Forest regression model: " + modelImportedFromJSON.toString(true));
            mae = evaluateModel(dataCache, modelImportedFromJSON);
            System.out.println("\n>>> Mean absolute error (MAE) for exported Random Forest regression model " + mae);
            System.out.println("\n>>> Random Forest regression algorithm over cached dataset usage example completed.");
        } finally {
            if (dataCache != null)
                dataCache.destroy();
            if (jsonMdlPath != null)
                Files.deleteIfExists(jsonMdlPath);
        }
    } finally {
        System.out.flush();
    }
}
Also used : Path(java.nio.file.Path) SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) RandomForestModel(org.apache.ignite.ml.tree.randomforest.RandomForestModel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FeatureMeta(org.apache.ignite.ml.dataset.feature.FeatureMeta) RandomForestRegressionTrainer(org.apache.ignite.ml.tree.randomforest.RandomForestRegressionTrainer) Ignite(org.apache.ignite.Ignite) Vector(org.apache.ignite.ml.math.primitives.vector.Vector)

Example 2 with RandomForestRegressionTrainer

use of org.apache.ignite.ml.tree.randomforest.RandomForestRegressionTrainer in project ignite by apache.

the class LearningEnvironmentTest method testBasic.

/**
 */
@Test
public void testBasic() {
    RandomForestRegressionTrainer trainer = new RandomForestRegressionTrainer(IntStream.range(0, 0).mapToObj(x -> new FeatureMeta("", 0, false)).collect(Collectors.toList())).withAmountOfTrees(101).withFeaturesCountSelectionStrgy(FeaturesCountSelectionStrategies.ONE_THIRD).withMaxDepth(4).withMinImpurityDelta(0.).withSubSampleSize(0.3).withSeed(0);
    LearningEnvironmentBuilder envBuilder = LearningEnvironmentBuilder.defaultBuilder().withParallelismStrategyType(ParallelismStrategy.Type.ON_DEFAULT_POOL).withLoggingFactoryDependency(part -> ConsoleLogger.factory(MLLogger.VerboseLevel.LOW));
    trainer.withEnvironmentBuilder(envBuilder);
    assertEquals(DefaultParallelismStrategy.class, trainer.learningEnvironment().parallelismStrategy().getClass());
    assertEquals(ConsoleLogger.class, trainer.learningEnvironment().logger().getClass());
}
Also used : FeatureMeta(org.apache.ignite.ml.dataset.feature.FeatureMeta) RandomForestRegressionTrainer(org.apache.ignite.ml.tree.randomforest.RandomForestRegressionTrainer) Test(org.junit.Test)

Example 3 with RandomForestRegressionTrainer

use of org.apache.ignite.ml.tree.randomforest.RandomForestRegressionTrainer in project ignite by apache.

the class RandomForestRegressionExample method main.

/**
 * Run example.
 */
public static void main(String[] args) throws IOException {
    System.out.println();
    System.out.println(">>> Random Forest regression 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.BOSTON_HOUSE_PRICES);
            AtomicInteger idx = new AtomicInteger(0);
            RandomForestRegressionTrainer trainer = new RandomForestRegressionTrainer(IntStream.range(0, dataCache.get(1).size() - 1).mapToObj(x -> new FeatureMeta("", idx.getAndIncrement(), false)).collect(Collectors.toList())).withAmountOfTrees(101).withFeaturesCountSelectionStrgy(FeaturesCountSelectionStrategies.ONE_THIRD).withMaxDepth(4).withMinImpurityDelta(0.).withSubSampleSize(0.3).withSeed(0);
            trainer.withEnvironmentBuilder(LearningEnvironmentBuilder.defaultBuilder().withParallelismStrategyTypeDependency(ParallelismStrategy.ON_DEFAULT_POOL).withLoggingFactoryDependency(ConsoleLogger.Factory.LOW));
            System.out.println(">>> Configured trainer: " + trainer.getClass().getSimpleName());
            Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
            ModelsComposition randomForestMdl = trainer.fit(ignite, dataCache, vectorizer);
            System.out.println(">>> Trained model: " + randomForestMdl.toString(true));
            double mse = 0.0;
            double mae = 0.0;
            int totalAmount = 0;
            try (QueryCursor<Cache.Entry<Integer, Vector>> observations = dataCache.query(new ScanQuery<>())) {
                for (Cache.Entry<Integer, Vector> observation : observations) {
                    Vector val = observation.getValue();
                    Vector inputs = val.copyOfRange(1, val.size());
                    double groundTruth = val.get(0);
                    double prediction = randomForestMdl.predict(inputs);
                    mse += Math.pow(prediction - groundTruth, 2.0);
                    mae += Math.abs(prediction - groundTruth);
                    totalAmount++;
                }
                System.out.println("\n>>> Evaluated model on " + totalAmount + " data points.");
                mse /= totalAmount;
                System.out.println("\n>>> Mean squared error (MSE) " + mse);
                mae /= totalAmount;
                System.out.println("\n>>> Mean absolute error (MAE) " + mae);
                System.out.println(">>> Random Forest regression algorithm over cached dataset usage example completed.");
            }
        } finally {
            if (dataCache != null)
                dataCache.destroy();
        }
    } finally {
        System.out.flush();
    }
}
Also used : SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) ModelsComposition(org.apache.ignite.ml.composition.ModelsComposition) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FeatureMeta(org.apache.ignite.ml.dataset.feature.FeatureMeta) RandomForestRegressionTrainer(org.apache.ignite.ml.tree.randomforest.RandomForestRegressionTrainer) Ignite(org.apache.ignite.Ignite) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) IgniteCache(org.apache.ignite.IgniteCache) SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) Cache(javax.cache.Cache)

Aggregations

FeatureMeta (org.apache.ignite.ml.dataset.feature.FeatureMeta)3 RandomForestRegressionTrainer (org.apache.ignite.ml.tree.randomforest.RandomForestRegressionTrainer)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Ignite (org.apache.ignite.Ignite)2 SandboxMLCache (org.apache.ignite.examples.ml.util.SandboxMLCache)2 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)2 Path (java.nio.file.Path)1 Cache (javax.cache.Cache)1 IgniteCache (org.apache.ignite.IgniteCache)1 ModelsComposition (org.apache.ignite.ml.composition.ModelsComposition)1 RandomForestModel (org.apache.ignite.ml.tree.randomforest.RandomForestModel)1 Test (org.junit.Test)1