Skip to main content

Playground · Decision Trees

Decision trees & Gini impurity

A decision tree learns by asking one question at a time: "is this feature above some threshold?" Each split carves the data into two groups, and the tree greedily picks the split that makes those groups as pure as possible. Pick a feature, drag the threshold, and watch real Gini impurity and information gain recompute — then let the tree find the best split itself.

Cyan and magenta are the two classes. The amber line is the current split; points on each side form the two child nodes.

Split on feature

Points where the chosen feature is below the threshold go to one child; the rest go to the other.

How a decision tree decides

Gini impurity

For a group, Gini = 1 − Σ pc2, where pc is the fraction of class c. It's 0 when a node is one pure class and peaks at 0.5 for a 50/50 two-class mix — a measure of how "mixed up" the labels are.

Information gain

A split scores as the parent's impurity minus the size-weighted average impurity of its two children. The tree is greedy: it tries every threshold on every feature and keeps the split with the highest gain (the biggest drop in impurity).

Recursion & depth

Each child becomes a new parent and is split again, recursively, until nodes are pure or a depth limit is hit. Grown too deep, a tree memorises noise and overfits — so depth, minimum node size and pruning are the usual guard rails.

Caveat: this dataset is illustrative and only two-dimensional, and a single axis-aligned split is just one node. Real trees stack many splits over many features (and ensembles like random forests average whole forests of them). The Gini and information-gain maths here are exactly what libraries such as scikit-learn compute. More on the Learn shelf.