RandomForestClassifierTrainer extends RandomForestTrainer<ObjectHistogram<BootstrappedVector>, GiniHistogram, RandomForestClassifierTrainer> { @Override public RandomForestClassifierTrainer withEnvironmentBuilder(LearningEnvironmentBuilder envBuilder) { return (RandomForestClassifierTrainer)super.withEnvironmentBuilder(envBuilder); } RandomForestClassifierTrainer(List<FeatureMeta> meta); @Override RandomForestClassifierTrainer withEnvironmentBuilder(LearningEnvironmentBuilder envBuilder);  }
@Test public void testFit() { int sampleSize = 500; Map<Integer, LabeledVector<Double>> sample = new HashMap<>(); for (int i = 0; i < sampleSize; i++) { double x1 = i; double x2 = x1 / 10.0; double x3 = x2 / 10.0; double x4 = x3 / 10.0; sample.put(i, VectorUtils.of(x1, x2, x3, x4).labeled((double) i % 2)); } ArrayList<FeatureMeta> meta = new ArrayList<>(); for (int i = 0; i < 4; i++) meta.add(new FeatureMeta("", i, false)); DatasetTrainer<ModelsComposition, Double> trainer = new RandomForestClassifierTrainer(meta) .withAmountOfTrees(5) .withFeaturesCountSelectionStrgy(x -> 2) .withEnvironmentBuilder(TestUtils.testEnvBuilder()); ModelsComposition mdl = trainer.fit(sample, parts, new LabeledDummyVectorizer<>()); assertTrue(mdl.getPredictionsAggregator() instanceof OnMajorityPredictionsAggregator); assertEquals(5, mdl.getModels().size()); }
@Test public void testUpdate() { int sampleSize = 500; Map<Integer, LabeledVector<Double>> sample = new HashMap<>(); for (int i = 0; i < sampleSize; i++) { double x1 = i; double x2 = x1 / 10.0; double x3 = x2 / 10.0; double x4 = x3 / 10.0; sample.put(i, VectorUtils.of(x1, x2, x3, x4).labeled((double) i % 2)); } ArrayList<FeatureMeta> meta = new ArrayList<>(); for (int i = 0; i < 4; i++) meta.add(new FeatureMeta("", i, false)); DatasetTrainer<ModelsComposition, Double> trainer = new RandomForestClassifierTrainer(meta) .withAmountOfTrees(32) .withFeaturesCountSelectionStrgy(x -> 2) .withEnvironmentBuilder(TestUtils.testEnvBuilder()); ModelsComposition originalMdl = trainer.fit(sample, parts, new LabeledDummyVectorizer<>()); ModelsComposition updatedOnSameDS = trainer.update(originalMdl, sample, parts, new LabeledDummyVectorizer<>()); ModelsComposition updatedOnEmptyDS = trainer.update(originalMdl, new HashMap<Integer, LabeledVector<Double>>(), parts, new LabeledDummyVectorizer<>()); Vector v = VectorUtils.of(5, 0.5, 0.05, 0.005); assertEquals(originalMdl.predict(v), updatedOnSameDS.predict(v), 0.01); assertEquals(originalMdl.predict(v), updatedOnEmptyDS.predict(v), 0.01); }