Decision Trees (DTs) are a non-parametric supervised learning method used
for classification and regression. The goal is to create a model that predicts the value of a
target variable by learning simple decision rules inferred from the data
features.

For instance, in the example below, decision trees learn from data to
approximate a sine curve with a set of if-then-else decision rules. The deeper
the tree, the more complex the decision rules and the fitter the model.

Some advantages of decision trees are:

Simple to understand and to interpret. Trees can be visualised.

Requires little data preparation. Other techniques often require data
normalisation, dummy variables need to be created and blank values to
be removed. Note however that this module does not support missing
values.

The cost of using the tree (i.e., predicting data) is logarithmic in the
number of data points used to train the tree.

Able to handle both numerical and categorical data. Other techniques
are usually specialised in analysing datasets that have only one type
of variable. See algorithms for more
information.

Able to handle multi-output problems.

Uses a white box model. If a given situation is observable in a model,
the explanation for the condition is easily explained by boolean logic.
By contrast, in a black box model (e.g., in an artificial neural
network), results may be more difficult to interpret.

Possible to validate a model using statistical tests. That makes it
possible to account for the reliability of the model.

Performs well even if its assumptions are somewhat violated by
the true model from which the data were generated.

The disadvantages of decision trees include:

Decision-tree learners can create over-complex trees that do not
generalise the data well. This is called overfitting. Mechanisms
such as pruning (not currently supported), setting the minimum
number of samples required at a leaf node or setting the maximum
depth of the tree are necessary to avoid this problem.

Decision trees can be unstable because small variations in the
data might result in a completely different tree being generated.
This problem is mitigated by using decision trees within an
ensemble.

The problem of learning an optimal decision tree is known to be
NP-complete under several aspects of optimality and even for simple
concepts. Consequently, practical decision-tree learning algorithms
are based on heuristic algorithms such as the greedy algorithm where
locally optimal decisions are made at each node. Such algorithms
cannot guarantee to return the globally optimal decision tree. This
can be mitigated by training multiple trees in an ensemble learner,
where the features and samples are randomly sampled with replacement.

There are concepts that are hard to learn because decision trees
do not express them easily, such as XOR, parity or multiplexer problems.

Decision tree learners create biased trees if some classes dominate.
It is therefore recommended to balance the dataset prior to fitting
with the decision tree.

As other classifiers, DecisionTreeClassifier take as input two arrays:
an array X, sparse or dense, of size [n_samples,n_features] holding the
training samples, and an array Y of integer values, size [n_samples],
holding the class labels for the training samples:

A multi-output problem is a supervised learning problem with several outputs
to predict, that is when Y is a 2d array of size [n_samples,n_outputs].

When there is no correlation between the outputs, a very simple way to solve
this kind of problem is to build n independent models, i.e. one for each
output, and then to use those models to independently predict each one of the n
outputs. However, because it is likely that the output values related to the
same input are themselves correlated, an often better way is to build a single
model capable of predicting simultaneously all n outputs. First, it requires
lower training time since only a single estimator is built. Second, the
generalization accuracy of the resulting estimator may often be increased.

With regard to decision trees, this strategy can readily be used to support
multi-output problems. This requires the following changes:

Store n output values in leaves, instead of 1;

Use splitting criteria that compute the average reduction across all
n outputs.

This module offers support for multi-output problems by implementing this
strategy in both DecisionTreeClassifier and
DecisionTreeRegressor. If a decision tree is fit on an output array Y
of size [n_samples,n_outputs] then the resulting estimator will:

Output n_output values upon predict;

Output a list of n_output arrays of class probabilities upon
predict_proba.

The use of multi-output trees for regression is demonstrated in
Multi-output Decision Tree Regression. In this example, the input
X is a single real value and the outputs Y are the sine and cosine of X.

The use of multi-output trees for classification is demonstrated in
Face completion with a multi-output estimators. In this example, the inputs
X are the pixels of the upper half of faces and the outputs Y are the pixels of
the lower half of those faces.

In general, the run time cost to construct a balanced binary tree is
and query time
. Although the tree construction algorithm attempts
to generate balanced trees, they will not always be balanced. Assuming that the
subtrees remain approximately balanced, the cost at each node consists of
searching through to find the feature that offers the
largest reduction in entropy. This has a cost of
at each node, leading to a
total cost over the entire trees (by summing the cost at each node) of
.

Scikit-learn offers a more efficient implementation for the construction of
decision trees. A naive implementation (as above) would recompute the class
label histograms (for classification) or the means (for regression) at for each
new split point along a given feature. By presorting the feature over all
relevant samples, and retaining a running label count, we reduce the complexity
at each node to , which results in a
total cost of .

Decision trees tend to overfit on data with a large number of features.
Getting the right ratio of samples to number of features is important, since
a tree with few samples in high dimensional space is very likely to overfit.

Consider performing dimensionality reduction (PCA,
ICA, or Feature selection) beforehand to
give your tree a better chance of finding features that are discriminative.

Visualise your tree as you are training by using the export
function. Use max_depth=3 as an initial tree depth to get a feel for
how the tree is fitting to your data, and then increase the depth.

Remember that the number of samples required to populate the tree doubles
for each additional level the tree grows to. Use max_depth to control
the size of the tree to prevent overfitting.

Use min_samples_split or min_samples_leaf to control the number of
samples at a leaf node. A very small number will usually mean the tree
will overfit, whereas a large number will prevent the tree from learning
the data. Try min_samples_leaf=5 as an initial value.
The main difference between the two is that min_samples_leaf guarantees
a minimum number of samples in a leaf, while min_samples_split can
create arbitrary small leaves, though min_samples_split is more common
in the literature.

Balance your dataset before training to prevent the tree from being biased
toward the classes that are dominant. Class balancing can be done by
sampling an equal number of samples from each class, or preferably by
normalizing the sum of the sample weights (sample_weight) for each
class to the same value. Also note that weight-based pre-pruning criteria,
such as min_weight_fraction_leaf, will then be less biased toward
dominant classes than criteria that are not aware of the sample weights,
like min_samples_leaf.

If the samples are weighted, it will be easier to optimize the tree
structure using weight-based pre-pruning criterion such as
min_weight_fraction_leaf, which ensure that leaf nodes contain at least
a fraction of the overall sum of the sample weights.

All decision trees use np.float32 arrays internally.
If training data is not in this format, a copy of the dataset will be made.

If the input matrix X is very sparse, it is recommended to convert to sparse
csc_matrix`beforecallingfitandsparse``csr_matrix before calling
predict. Training time can be orders of magnitude faster for a sparse
matrix input compared to a dense matrix when features have zero values in
most of the samples.

What are all the various decision tree algorithms and how do they differ
from each other? Which one is implemented in scikit-learn?

ID3 (Iterative Dichotomiser 3) was developed in 1986 by Ross Quinlan.
The algorithm creates a multiway tree, finding for each node (i.e. in
a greedy manner) the categorical feature that will yield the largest
information gain for categorical targets. Trees are grown to their
maximum size and then a pruning step is usually applied to improve the
ability of the tree to generalise to unseen data.

C4.5 is the successor to ID3 and removed the restriction that features
must be categorical by dynamically defining a discrete attribute (based
on numerical variables) that partitions the continuous attribute value
into a discrete set of intervals. C4.5 converts the trained trees
(i.e. the output of the ID3 algorithm) into sets of if-then rules.
These accuracy of each rule is then evaluated to determine the order
in which they should be applied. Pruning is done by removing a rule’s
precondition if the accuracy of the rule improves without it.

C5.0 is Quinlan’s latest version release under a proprietary license.
It uses less memory and builds smaller rulesets than C4.5 while being
more accurate.

CART (Classification and Regression Trees) is very similar to C4.5, but
it differs in that it supports numerical target variables (regression) and
does not compute rule sets. CART constructs binary trees using the feature
and threshold that yield the largest information gain at each node.