Package de.jungblut.classification.tree
Class DecisionTree
- java.lang.Object
-
- de.jungblut.classification.AbstractPredictor
-
- de.jungblut.classification.AbstractClassifier
-
- de.jungblut.classification.tree.DecisionTree
-
- All Implemented Interfaces:
Classifier,Predictor
public final class DecisionTree extends AbstractClassifier
A decision tree that can be used for classification with numerical or categorical features. The tree is built by maximizing information gain using the ID3 algorithm. If no featureTypes were supplied, the default is assumed to be nominal features at all feature dimensions.
Instances can be created by the static factory methods #create().- Author:
- thomasjungblut
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcompileTree()Compiles this current tree representation into byte code and loads it into this class.static DecisionTreecreate()static DecisionTreecreate(FeatureType[] featureTypes)Creates a new decision tree with the given feature types.static DecisionTreecreateCompiledTree()static DecisionTreecreateCompiledTree(FeatureType[] featureTypes)Creates a new compiled decision tree with the given feature types.static DecisionTreedeserialize(java.io.DataInput in)Reads a new tree from the given stream.de.jungblut.math.DoubleVectorpredict(de.jungblut.math.DoubleVector features)Classifies the given features.static voidserialize(DecisionTree tree, java.io.DataOutput out)Writes the given tree to the output stream.DecisionTreesetCompiled(boolean compiled)If set to true, this tree will be compiled after training time automatically.DecisionTreesetFeatureTypes(FeatureType[] featureTypes)Sets the type of feature per index.DecisionTreesetMaxHeight(int max)Sets the maximum height of this tree.DecisionTreesetNumRandomFeaturesToChoose(int numRandomFeaturesToChoose)Sets the number of random features to choose from all features.Zero, negative numbers or numbers greater than the really available features indicate all features to be used.DecisionTreesetSeed(long seed)Sets the seed for a random number generator if used.voidtrain(de.jungblut.math.DoubleVector[] features, de.jungblut.math.DoubleVector[] outcome)Trains this classifier with the given features and the outcome.-
Methods inherited from class de.jungblut.classification.AbstractClassifier
train
-
Methods inherited from class de.jungblut.classification.AbstractPredictor
extractPredictedClass, extractPredictedClass, predictedClass, predictedClass, predictProbability
-
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, predictProbability
-
-
-
-
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.
-
compileTree
public void compileTree() throws java.lang.ExceptionCompiles this current tree representation into byte code and loads it into this class. This is considered faster, as the interpreted code can be optimized by the hotspot JVM.- Throws:
java.lang.Exception- some error might happen during compilation or loading.
-
setFeatureTypes
public DecisionTree setFeatureTypes(FeatureType[] featureTypes)
Sets the type of feature per index. This should match the inputted number of features in the training method. If this isn't set at all, all attributes are assumed to be nominal.- Returns:
- this decision tree instance.
-
setNumRandomFeaturesToChoose
public DecisionTree setNumRandomFeaturesToChoose(int numRandomFeaturesToChoose)
Sets the number of random features to choose from all features.Zero, negative numbers or numbers greater than the really available features indicate all features to be used.- Returns:
- this decision tree instance.
-
setCompiled
public DecisionTree setCompiled(boolean compiled)
If set to true, this tree will be compiled after training time automatically.- Returns:
- this decision tree instance.
-
setMaxHeight
public DecisionTree setMaxHeight(int max)
Sets the maximum height of this tree.- Returns:
- this instance.
-
setSeed
public DecisionTree setSeed(long seed)
Sets the seed for a random number generator if used.
-
serialize
public static void serialize(DecisionTree tree, java.io.DataOutput out) throws java.io.IOException
Writes the given tree to the output stream. Note that the stream isn't closed here.- Throws:
java.io.IOException
-
deserialize
public static DecisionTree deserialize(java.io.DataInput in) throws java.io.IOException
Reads a new tree from the given stream. Note that the stream isn't closed here.- Throws:
java.io.IOException
-
create
public static DecisionTree create()
- Returns:
- a default decision tree with all features beeing nominal.
-
create
public static DecisionTree create(FeatureType[] featureTypes)
Creates a new decision tree with the given feature types.- Parameters:
featureTypes- the types of the feature that must match the number of features in length.- Returns:
- a default decision tree with all features beeing set to what has been configured in the given array.
-
createCompiledTree
public static DecisionTree createCompiledTree()
- Returns:
- a default compiled decision tree with all features beeing nominal.
-
createCompiledTree
public static DecisionTree createCompiledTree(FeatureType[] featureTypes)
Creates a new compiled decision tree with the given feature types.- Parameters:
featureTypes- the types of the feature that must match the number of features in length.- Returns:
- a default decision tree with all features beeing set to what has been configured in the given array.
-
-