Supervised Learning
Decision Trees
Each question divides the training observations reaching one node; leaf votes turn those questions into predictions.
Training feature space
80 training observations. Inspected node 0 contains 80, A 40, B 40. Depth 0; 1 leaves. Training accuracy 0.500. Exploratory validation accuracy 0.500 on 80 independently generated observations.
Circle = A; square = B. Large class glyphs are observations; small neutral glyphs mark leaf votes. Rectangles are visible path intersections, not a sampled grid. Leaves extend beyond the frame. Dashed = manual preview; solid = best supported candidate after reveal.
- Class A
- Class B
- Manual preview (dashed)
- Best supported split (solid)
Observation 1: x 3.894027888261112, y 5.747910787242908, class A.
Which observations share a label?
Circles are class A and squares are class B. Inspect an observation to see its raw coordinates and label.
Check this stage
A decision tree chooses a question using only the training observations reaching one node. Move a manual threshold, compare its weighted impurity with the greedy choice, then inspect the resulting leaf votes.
What does a question change?
In Feature space, circles identify class A and squares class B. Inspect a training observation to see its exact coordinates. The original sample has 40 observations of each class, generated around two overlapping Gaussian centres. Coordinates vary; the generator does not flip labels. New data changes the training draw while validation stays fixed.
In Root node, all training observations reach one node. Its majority class is the prediction before any question is asked. Equal votes choose A, the lowest class index. Select Imbalanced clusters: 64 A and 16 B show why a root need not have maximum impurity. Accuracy alone can hide poor minority recall.
How is a split scored?
For a nonempty node, is its fraction of training observations in class . Gini impurity and class entropy are:
Entropy is in bits; a zero-frequency term contributes zero. Either criterion is zero for a pure node, which may contain many observations. A leaf can also be impure if growth stops early.
In First split, drag the neutral diamond or use Manual threshold. Choose x or y. Coordinates less than or equal to the threshold go left; the rest go right. The preview uses only observations reaching the inspected node. It does not replace the fitted tree.
With parent size and child sizes , the reduction is:
Each weight is that child's share of its parent, not half by default. The drawer exposes child memberships, class counts, impurities and weighted result. An empty or undersized child makes the split invalid.
For a hand example, take x = 0, 1, 2, 3 with labels A, A, B, B and constant y. Parent Gini is . At x = 0.5 the children are [A] and [A,B,B]. Weighted Gini is , giving gain . At x = 1.5 both children are pure, giving gain . Candidate midpoints are 0.5, 1.5 and 2.5; constant y supplies none.
Predict whether your valid manual gain is lower, equal or higher than the best supported candidate, then reveal. Both computations use identical node membership, criterion and minimum leaf size. The automatic search checks sorted distinct-value midpoints on both features. Gains within retain the earlier candidate: x before y, then ascending threshold. A different manual threshold can create the same training partition.
Why is greedy a limitation?
The search chooses one locally useful question without planning later branches. It does not find a globally optimal tree. This lab grows a branch only when gain exceeds . On exact XOR a first question can have zero gain even though two questions together separate the classes. That stopping policy can miss a useful interaction.
Positive affine rescaling, such as metres to centimetres, preserves candidate training partitions in exact arithmetic. A nonlinear monotonic transform preserves training order but need not preserve midpoint boundaries for new queries. This differs from nearest-neighbour distances changing with feature units.
What stops growth?
In Grow tree, compare three constraints:
- Max depth limits questions along a path. The root is at depth zero.
- Minimum samples to split constrains the parent before another question is tried.
- Minimum samples per leaf constrains both proposed children.
Pure nodes, constant features, missing admissible thresholds and nonpositive gain also stop this implementation. Inspection reports the actual reason. Post-pruning grows a tree and then chooses a smaller subtree under a complexity penalty; these controls restrict growth beforehand. Post-pruning is not simulated.
Choose Conflicting duplicates: opposite labels at identical coordinates cannot be separated by these features. Depth alone cannot force perfect training accuracy. This fixed fixture is not an independent generalisation experiment and has no active random seed.
How do the tree and rectangles correspond?
Click a numbered node, press Enter or Space on it, or use Inspect node. Observations outside it dim. The diagram shows its parent and immediate children, with the left branch representing ≤. The complete fitted outline is in Under the hood, beside all training coordinates, class labels, node memberships and fitted leaf predictions.
Every leaf rectangle is the exact intersection of its path tests with the viewport. A small neutral circle or square at its centre identifies its vote. Large class glyphs identify training observations. The fitted rule extends beyond the frame: cropping the display does not clamp queries or alter fitting. Raw thresholds and bounds remain inspectable. These rectangles do not depend on a finite query grid.
Does more depth help on new observations?
Training accuracy scores the observations used to fit. Exploratory validation accuracy scores a separate generated draw which never chooses splits. Depth-two and depth-six comparisons reuse the same draws and sample limits. Validation class recalls and a training-majority baseline help interpret imbalance.
Try the deep-tree experiment, then reduce depth. Measure what changes: perfect training accuracy alone proves neither generalisation nor overfitting. A worse independent score for the deeper fit supports a comparison on these observations, not a guarantee for every seed. Overlap does not mean the generator injected incorrect labels.
Repeatedly choosing settings using validation makes its score part of exploration. An untouched final test belongs after choices are fixed; none is shown here. See train/test split for the protocol and ensembles for combining trees.
Back to working restores the complete experiment saved before failure, including stage, seed, controls, inspection, free exploration and prediction. Reset retains stage and seed but clears recovery. Share reproduces guided display stage, seed, observation choice, controls and inspection after recomputing. Guesses and recovery snapshots are excluded.
Sources and limits
The scikit-learn tree guide supplies the partitioning and weighted-impurity formulation; its classifier reference distinguishes parent and leaf constraints. The cross-validation guide explains final testing after selection. This lab uses two numeric features, double-precision conventions and small synthetic samples. It does not implement missing values, categorical subset search, regression, calibration or pruning.
Stage recap
Feature space
Circles are class A and squares are class B. Inspect an observation to see its raw coordinates and label.
Root node
Count the classes before naming impurity. The root is maximally mixed only when the two class counts are equal.
First split
Move the dashed threshold. Compare its actual child membership and weighted impurity with the best midpoint candidate on this same node.
Grow tree
Depth and sample limits control growth. The validation draw is reused for exploration and never chooses a split. It is not an untouched final test.
Reference
- Gini Impurity
- 1 - Σ(p_i)²
- Weighted reduction
- Parent impurity minus (n_left × H_left + n_right × H_right) / n_parent
- Entropy
- −Σ p_k log₂(p_k), in bits; zero-count terms contribute zero.
- Stopping
- Depth, parent size, child size, purity, available thresholds and positive gain all matter.
- Validation
- An independent draw reused for exploration; reserve a separate final test after selection.
Break it on purpose
Load the deep-tree experiment and compare depth six with depth two on the same training and validation draws. A better training fit need not give better validation accuracy. Conflicting labels at identical coordinates cannot be separated at any depth.
Explore next
- Train/Test Split & CVA held-out score is only as honest as the split and fitting procedure behind it.
- Ridge & Lasso RegularisationA coefficient penalty changes the fitted curve; its benefit on new observations must be measured.
- Supervised Ensembles: Random Forests & BoostingAn ensemble combines actual learner predictions; bootstrap sampling, feature restrictions and sequential weighting create different voting rules.