# Decision trees
A decision tree classifies an observation through a sequence of tests. Training derives those decision rules from the examples.
Trees are fast to evaluate, and you can follow the tests to see which attributes mattered. Splitting on individual attributes can overlook relationships between them. An uneven class distribution can also affect performance.
# Building a tree
The central question is which attribute to test at each node.
Entropy measures uncertainty in the class distribution. In a dataset with classes, let be the proportion in class . Its entropy is
.
A pure node contains observations from a single class. One way to describe how close a node is to that state is the fraction covered by its majority class.
- Choose a split. ID3 uses information gain, C4.5 uses gain ratio, and classification trees in CART use Gini impurity.
- Information gain is the reduction in entropy after splitting on attribute : . Here counts distinct values and is the subset with value . The ratio weights each child by its size.
- Information gain tends to favour attributes with many values, which can lead to overfitting. Gain ratio divides it by the entropy of the split: , where .
- Gini impurity is the probability that two independent draws from the class distribution differ. With class proportions , .
- Partition the samples. The selected test assigns the observations to child nodes.
- Repeat recursively. At each child choose a new split until a stopping condition applies, for example a pure node, no remaining attributes, or a maximum depth.
- Assign leaves. Limits on leaf size, depth or purity can leave several classes in a leaf. Assign the most frequent class. In these notes, a tie is broken by choosing at random among the tied classes.
- Prune. Removing overly complex branches can improve generalisation. Methods include reduced-error pruning (REP), pessimistic-error pruning (PEP), error-based pruning (EBP), cost-complexity pruning (CCP) and minimum-error pruning (MEP).
- Predict. Follow the tests from the root to a leaf and return its class, or its numerical prediction for a regression tree.
# ID3
Compute the information gain for each candidate attribute and choose the largest. Create branches for its possible values. If no further split is possible, label the leaf with the most frequent class among its samples.
# C4.5
- Split selection. A low split entropy can inflate gain ratio. The selection heuristic first keeps attributes with above-average information gain and then chooses the highest gain ratio among them.
- Continuous attributes. ID3 handles discrete attributes. C4.5 can also split a continuous attribute by finding a threshold. Sort the distinct observed values. With values, there are midpoints between neighbours. Evaluate the information gain for each binary test (above the midpoint versus at or below it) and choose the best threshold.
- Missing values. Calculate gain among observations with known values and scale it by the proportion of observations with known values. If this attribute is used, distribute the weight of a sample with a missing value across children in proportion to the known samples in those branches.
# CART
CART builds binary trees for classification or regression. In the categorical classification example here, test whether attribute equals value . The test divides into and : samples with that value and all remaining samples. For each candidate split, weight each child’s Gini impurity by its share of the samples: . Choose the split with the lowest weighted impurity. Every internal node has exactly two children.
# Anomaly detection
Anomaly detection looks for observations that don’t fit the expected pattern. One approach flags points with a low estimated probability density.
For a continuous variable, describes density near . The probability over an interval is , and the integral over the entire real line is 1.
Gaussian distribution
The mean locates the centre. A larger standard deviation spreads the distribution out. For observations the estimates used here are and .

Gaussian anomaly detection
For one-dimensional observations , estimate the mean and standard deviation, calculate , and flag when .
For samples with features, arrange the values by feature and observation . Each row contains the observations for one feature:
Fit the Gaussian parameters separately for each feature:
Under the independent-feature model, multiply the densities:
Compare this joint density with the threshold .
# Principal component analysis
PCA projects high-dimensional data into fewer dimensions. The first principal direction captures the most variance. Each subsequent direction captures as much of the remaining variance as possible while staying orthogonal to the directions already chosen. PCA does not use class labels.
- Preprocess the features. For standardisation, centre them at zero and scale to unit standard deviation.
- Find the eigenvectors of the covariance matrix and the variance along each direction.
- Choose the target dimension based on the task or a desired fraction of explained variance.
- Project the observations onto the space spanned by the leading eigenvectors.
# Model optimisation checks
These notes consolidate an earlier post on model optimisation. They’re questions to investigate; the experiment determines which changes help.
# Data
Data quality limits what the model can achieve.
- What does each feature mean? Removing irrelevant features can save computation and reduce overfitting.
- Do the features have very different scales? Normalisation or standardisation can balance their influence and speed up convergence.
- Are there outliers? Decide whether keeping or filtering them makes the model more robust.
- Are there enough examples? Consider increasing the sample.
- Does the collection method produce representative data?
- Are labels assigned consistently under the same rules?
# Compare models
Try different models on comparable data and compare their performance to find one that better suits the task.
# Key parameters
Try every combination of the key parameter values under consideration and compare the model’s performance.

