Class RandomForest

  • All Implemented Interfaces:
    Classifier, Predictor

    public final class RandomForest
    extends AbstractClassifier
    A decision tree forest, using bagging. The decision trees inside are compiled directly into byte code for fast performance. The training can be done multithreaded.
    Author:
    thomasjungblut
    • Method Detail

      • train

        public void train​(de.jungblut.math.DoubleVector[] features,
                          de.jungblut.math.DoubleVector[] outcome)
        Description copied from interface: Classifier
        Trains this classifier with the given features and the outcome.
        Specified by:
        train in interface Classifier
        Overrides:
        train in class AbstractClassifier
        outcome - the outcome must have classes labeled as doubles. E.G. in the binary case you have a single element and decide between 0d and 1d. In higher dimensional cases you have each of these single elements mapped to a dimension.
      • predict

        public de.jungblut.math.DoubleVector predict​(de.jungblut.math.DoubleVector features)
        Description copied from interface: Predictor
        Classifies the given features.
        Returns:
        the vector that contains an indicator at the index of the class. Usually zero or 1, in some cases it is a probability or activation value.
      • predictProbability

        public de.jungblut.math.DoubleVector predictProbability​(de.jungblut.math.DoubleVector features)
        Description copied from interface: Predictor
        Classifies the given features.
        Specified by:
        predictProbability in interface Predictor
        Overrides:
        predictProbability in class AbstractPredictor
        Returns:
        a vector that returns the probability of all outcomes. The output vector should sum to one.
      • compile

        public RandomForest compile()
        Returns:
        sets this instance to compile and returns it.
      • verbose

        public RandomForest verbose()
        Returns:
        sets this instance to verbose and returns it.
      • verbose

        public RandomForest verbose​(boolean verb)
        Returns:
        sets this instance to verbose and returns it.
      • setMaxHeight

        public RandomForest setMaxHeight​(int max)
        Sets the maximum height of this random forest.
        Returns:
        this instance.
      • numThreads

        public RandomForest numThreads​(int numThreads)
        Returns:
        this instance, set the number of threads for training the forest.
      • setNumRandomFeaturesToChoose

        public RandomForest setNumRandomFeaturesToChoose​(int numRandomFeaturesToChoose)
        Returns:
        this instance, set to the number of random features to choose at every decision tree level.
      • setFeatureTypes

        public RandomForest setFeatureTypes​(FeatureType[] types)
        Returns:
        sets the feature types of the decision tree.
      • create

        public static RandomForest create​(int numTrees)
        Creates a new random forest, trains on one thread with the number of trees supplied. It chooses sqrt(#features) random features at each tree level. In addition, it treats all features as categorical values.
      • create

        public static RandomForest create​(int numTrees,
                                          FeatureType[] types)
        Creates a new random forest, trains on one thread with the number of trees supplied. It chooses log(#features) random features at each tree level.
      • serialize

        public static void serialize​(RandomForest tree,
                                     java.io.DataOutput out)
                              throws java.io.IOException
        Writes the given forest to the output stream. Note that the stream isn't closed here.
        Throws:
        java.io.IOException
      • deserialize

        public static RandomForest deserialize​(java.io.DataInput in)
                                        throws java.io.IOException
        Reads a new forest from the given stream. Note that the stream isn't closed here.
        Throws:
        java.io.IOException