Package de.jungblut.classification.tree
Class RandomForest
- java.lang.Object
-
- de.jungblut.classification.AbstractPredictor
-
- de.jungblut.classification.AbstractClassifier
-
- de.jungblut.classification.tree.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 Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description RandomForestcompile()static RandomForestcreate(int numTrees)Creates a new random forest, trains on one thread with the number of trees supplied.static RandomForestcreate(int numTrees, FeatureType[] types)Creates a new random forest, trains on one thread with the number of trees supplied.static RandomForestdeserialize(java.io.DataInput in)Reads a new forest from the given stream.RandomForestnumThreads(int numThreads)de.jungblut.math.DoubleVectorpredict(de.jungblut.math.DoubleVector features)Classifies the given features.de.jungblut.math.DoubleVectorpredictProbability(de.jungblut.math.DoubleVector features)Classifies the given features.static voidserialize(RandomForest tree, java.io.DataOutput out)Writes the given forest to the output stream.RandomForestsetFeatureTypes(FeatureType[] types)RandomForestsetMaxHeight(int max)Sets the maximum height of this random forest.RandomForestsetNumRandomFeaturesToChoose(int numRandomFeaturesToChoose)voidtrain(de.jungblut.math.DoubleVector[] features, de.jungblut.math.DoubleVector[] outcome)Trains this classifier with the given features and the outcome.RandomForestverbose()RandomForestverbose(boolean verb)-
Methods inherited from class de.jungblut.classification.AbstractClassifier
train
-
Methods inherited from class de.jungblut.classification.AbstractPredictor
extractPredictedClass, extractPredictedClass, predictedClass, predictedClass
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface de.jungblut.classification.Predictor
asClassifier, extractPredictedClass, extractPredictedClass, predictedClass, predictedClass
-
-
-
-
Method Detail
-
train
public void train(de.jungblut.math.DoubleVector[] features, de.jungblut.math.DoubleVector[] outcome)Description copied from interface:ClassifierTrains this classifier with the given features and the outcome.- Specified by:
trainin interfaceClassifier- Overrides:
trainin classAbstractClassifieroutcome- 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:PredictorClassifies 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:PredictorClassifies the given features.- Specified by:
predictProbabilityin interfacePredictor- Overrides:
predictProbabilityin classAbstractPredictor- 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
-
-