Class 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 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.
      • compileTree

        public void compileTree()
                         throws java.lang.Exception
        Compiles 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.