@Test public void testPrune() { System.out.println("USPS"); RandomForest model = RandomForest.fit(USPS.formula, USPS.train, 200, 16, SplitRule.GINI, 20, 2000, 1, 1.0, null, Arrays.stream(seeds)); double[] importance = model.importance(); for (int i = 0; i < importance.length; i++) { System.out.format("%-15s %.4f%n", model.schema().fieldName(i), importance[i]); } int[] prediction = Validation.test(model, USPS.test); int error = Error.of(USPS.testy, prediction); System.out.println("Error = " + error); assertEquals(118, error); RandomForest lean = model.prune(USPS.test); importance = lean.importance(); for (int i = 0; i < importance.length; i++) { System.out.format("%-15s %.4f%n", lean.schema().fieldName(i), importance[i]); } prediction = Validation.test(model, USPS.test); error = Error.of(USPS.testy, prediction); System.out.println("Error of old model after pruning = " + error); assertEquals(118, error); prediction = Validation.test(lean, USPS.test); error = Error.of(USPS.testy, prediction); System.out.println("Error of pruned model after pruning = " + error); assertEquals(86, error); }
public RandomForest prune(DataFrame test) { List<Tree> forest = trees.stream().parallel() .map(tree -> new Tree(tree.tree.prune(test, formula, labels), tree.weight)) .collect(Collectors.toList()); return new RandomForest(formula, k, forest, error, importance(forest), labels); }
RandomForest implements SoftClassifier<Tuple>, DataFrameClassifier, TreeSHAP { public RandomForest prune(DataFrame test) { List<Tree> forest = trees.stream().parallel() .map(tree -> new Tree(tree.tree.prune(test, formula, labels), tree.weight)) .collect(Collectors.toList()); return new RandomForest(formula, k, forest, error, importance(forest), labels); } }
RandomForest implements SoftClassifier<Tuple>, DataFrameClassifier, TreeSHAP { public RandomForest prune(DataFrame test) { List<Tree> forest = trees.stream().parallel() .map(tree -> new Tree(tree.tree.prune(test, formula, labels), tree.weight)) .collect(Collectors.toList()); return new RandomForest(formula, k, forest, error, importance(forest), labels); } RandomForest(Formula formula, int k, List<Tree> trees, double error, double[] importance);  RandomForest(Formula formula, int k, List<Tree> trees, double error, double[] importance, IntSet labels); }
RandomForest implements SoftClassifier<Tuple>, DataFrameClassifier, TreeSHAP { public RandomForest prune(DataFrame test) { List<Tree> forest = trees.stream().parallel() .map(tree -> new Tree(tree.tree.prune(test, formula, labels), tree.weight)) .collect(Collectors.toList()); return new RandomForest(formula, k, forest, error, importance(forest), labels); } RandomForest(Formula formula, int k, List<Tree> trees, double error, double[] importance);  RandomForest(Formula formula, int k, List<Tree> trees, double error, double[] importance, IntSet labels); static RandomForest fit(Formula formula, DataFrame data); static RandomForest fit(Formula formula, DataFrame data, Properties prop); static RandomForest fit(Formula formula, DataFrame data, int ntrees, int mtry,
                                   SplitRule rule, int maxDepth, int maxNodes, int nodeSize, double subsample); static RandomForest fit(Formula formula, DataFrame data, int ntrees, int mtry,
                                   SplitRule rule, int maxDepth, int maxNodes, int nodeSize,
                                   double subsample, int[] classWeight); static RandomForest fit(Formula formula, DataFrame data, int ntrees, int mtry,
                                   SplitRule rule, int maxDepth, int maxNodes, int nodeSize,
                                   double subsample, int[] classWeight, LongStream seeds); @Override Formula formula(); @Override StructType schema(); double error(); double[] importance(); int size(); DecisionTree[] trees(); void trim(int ntrees); @Override int predict(Tuple x); @Override int predict(Tuple x, double[] posteriori); int vote(Tuple x, double[] posteriori); int[][] test(DataFrame data); RandomForest prune(DataFrame test); }
RandomForest implements SoftClassifier<Tuple>, DataFrameClassifier, TreeSHAP { public RandomForest prune(DataFrame test) { List<Tree> forest = trees.stream().parallel() .map(tree -> new Tree(tree.tree.prune(test, formula, labels), tree.weight)) .collect(Collectors.toList()); return new RandomForest(formula, k, forest, error, importance(forest), labels); } RandomForest(Formula formula, int k, List<Tree> trees, double error, double[] importance);  RandomForest(Formula formula, int k, List<Tree> trees, double error, double[] importance, IntSet labels); static RandomForest fit(Formula formula, DataFrame data); static RandomForest fit(Formula formula, DataFrame data, Properties prop); static RandomForest fit(Formula formula, DataFrame data, int ntrees, int mtry,
                                   SplitRule rule, int maxDepth, int maxNodes, int nodeSize, double subsample); static RandomForest fit(Formula formula, DataFrame data, int ntrees, int mtry,
                                   SplitRule rule, int maxDepth, int maxNodes, int nodeSize,
                                   double subsample, int[] classWeight); static RandomForest fit(Formula formula, DataFrame data, int ntrees, int mtry,
                                   SplitRule rule, int maxDepth, int maxNodes, int nodeSize,
                                   double subsample, int[] classWeight, LongStream seeds); @Override Formula formula(); @Override StructType schema(); double error(); double[] importance(); int size(); DecisionTree[] trees(); void trim(int ntrees); @Override int predict(Tuple x); @Override int predict(Tuple x, double[] posteriori); int vote(Tuple x, double[] posteriori); int[][] test(DataFrame data); RandomForest prune(DataFrame test);  }