Random Forest is an extension of Bagging that is applied only to DecisionTree DecisionTrees. It works in a similar manner, but also only uses a random sub set of the features for each tree trained. This provides increased performance in accuracy of predictions, and reduced training t
| 43 | * @see Bagging |
| 44 | */ |
| 45 | public class RandomForest implements Classifier, Regressor, Parameterized |
| 46 | { |
| 47 | //TODO implement Out of Bag estimates of proximity, importance, and outlier detection |
| 48 | |
| 49 | private static final long serialVersionUID = 2725020584282958141L; |
| 50 | /** |
| 51 | * Only used when training for a classification problem |
| 52 | */ |
| 53 | private CategoricalData predicting; |
| 54 | private int extraSamples; |
| 55 | /** |
| 56 | * Setting the number of features to use. Default value is -1, indicating the heuristic |
| 57 | * of sqrt(N) or N/3 should be used for classification and regression respectively. This |
| 58 | * value should be set away from -1 before training work begins, and set back if it |
| 59 | * was not set explicitly by the used |
| 60 | */ |
| 61 | private int featureSamples; |
| 62 | private int maxForestSize; |
| 63 | private boolean useOutOfBagError = false; |
| 64 | private boolean useOutOfBagImportance = false; |
| 65 | private TreeFeatureImportanceInference importanceMeasure = new MDI(); |
| 66 | private OnLineStatistics[] feature_importance = null; |
| 67 | private double outOfBagError; |
| 68 | private RandomDecisionTree baseLearner; |
| 69 | private List<DecisionTree> forest; |
| 70 | |
| 71 | public RandomForest() |
| 72 | { |
| 73 | this(100); |
| 74 | } |
| 75 | |
| 76 | public RandomForest(int maxForestSize) |
| 77 | { |
| 78 | setExtraSamples(0); |
| 79 | setMaxForestSize(maxForestSize); |
| 80 | autoFeatureSample(); |
| 81 | baseLearner = new RandomDecisionTree(1, Integer.MAX_VALUE, 3, TreePruner.PruningMethod.NONE, 1e-15); |
| 82 | baseLearner.setGainMethod(ImpurityMeasure.GINI); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * RandomForest performs Bagging. Bagging samples from the training set with replacement, and draws |
| 87 | * a sampleWithReplacement at least as large as the training set. This controls how many extra samples are |
| 88 | * taken. If negative, fewer samples will be taken. Using negative values is not recommended. |
| 89 | * |
| 90 | * @param i how many extra samples to take |
| 91 | */ |
| 92 | public void setExtraSamples(int i) |
| 93 | { |
| 94 | extraSamples = i; |
| 95 | } |
| 96 | |
| 97 | public int getExtraSamples() |
| 98 | { |
| 99 | return extraSamples; |
| 100 | } |
| 101 | |
| 102 | /** |
nothing calls this directly
no outgoing calls
no test coverage detected