Gini Impurity Calculator
Compute the Gini impurity of a decision-tree node from its class counts or proportions, with the full 1 − Σ pₖ² working, a Shannon-entropy comparison, and the Gini gain of a candidate split. Matches scikit-learn. No signup, runs in your browser.
How it works
Gini impurity measures how mixed the class labels are at a node in a classification tree. It is the default splitting criterion in scikit-learn's DecisionTreeClassifier and the diversity index introduced by Breiman, Friedman, Olshen and Stone in Classification and Regression Trees (CART, 1984). Everything below is the same arithmetic the calculator above runs, so you can check a homework answer line by line or reproduce a scikit-learn tree_.impurity value by hand.
Let a node hold counts n₁, …, n_K over K classes, with total N = Σ nₖ. The calculation is four steps:
- Class proportions. For each class, pₖ = nₖ / N. These are the fractions of samples in the node belonging to each class.
- Square each proportion to get pₖ², then add them: Σ pₖ². This sum is the probability that two samples drawn at random from the node share a class.
- Gini impurity. G = 1 − Σ pₖ². Equivalently G = Σ pₖ(1 − pₖ) — the probability that two random draws differ. The calculator computes both forms and shows they agree, as a built-in cross-check.
- Range. G lies in [0, 1 − 1/K]. It is 0 for a pure node (one class only) and reaches its maximum 1 − 1/K when all classes are equally frequent.
The second form is the one printed in the scikit-learn documentation: H(Q_m) = Σ p_mk(1 − p_mk) for the samples Q_m at node m. Expanding it gives Σ pₖ − Σ pₖ², and since the proportions sum to one, Σ pₖ = 1 — which is why 1 − Σ pₖ² and Σ pₖ(1 − pₖ) are the same number. Seeing both agree on screen is a real check on your arithmetic, because a mis-typed count usually breaks one path before the other.
Why the formula is a probability
Gini impurity has a plain-English reading that makes it easier to sanity-check than entropy. Draw one sample from the node at random, then draw a second one, independently and with replacement. The chance both land in class k is pₖ², so the chance they match on any class is Σ pₖ². The chance they differ is one minus that, which is G. A Gini of 0.48 therefore means: pick two rows from this node at random and 48% of the time they carry different labels. That framing also explains the ceiling — the most confusable a K-class node can get is when every class is equally likely, giving Σ pₖ² = K·(1/K)² = 1/K and so G = 1 − 1/K.
From one node to a split
A single node's impurity is not a decision — a tree needs to compare candidate splits. In split mode, the tool evaluates a parent node partitioned into two children. It computes each child's Gini Gⱼ, the sample weights wⱼ = Nⱼ / N, the weighted child impurity Σ wⱼ·Gⱼ, and the Gini gain ΔG = G_parent − Σ wⱼ·Gⱼ. The weighting matters: a child holding three samples cannot be allowed to count as much as a sibling holding three hundred, which is exactly the mistake that produces trees that chase tiny pure leaves.
CART evaluates every feature and every threshold this way and keeps the split with the largest ΔG, then recurses on each child until a stopping rule fires — maximum depth, minimum samples per leaf, or a node that is already pure. Gini gain is never negative for a valid partition (the weighted average of children can only be less than or equal to the parent), so a gain of exactly zero is the signal that the candidate feature carries no information about the label at this node.
Entropy, and why the tool shows both
Alongside Gini, the tool reports Shannon entropy, H = −Σ pₖ log₂ pₖ bits, using the convention 0·log₂0 = 0 so pure classes never produce NaN. Entropy is scikit-learn's criterion="entropy" and the basis of information gain in ID3 and C4.5. The two curves have the same shape — zero at purity, maximum at a uniform node — so in practice they pick the same split the large majority of the time. Entropy punishes rare classes harder because of the logarithm, which shows up on very imbalanced nodes such as [999, 1] in the examples below. If you want the entropy figure on its own, with its own working table, use the Shannon entropy calculator; for the training-loss cousin of the same quantity, see the cross-entropy loss calculator.
Edge cases the calculator handles
- Pure nodes. Counts like [10, 0] give G = 0 and H = 0 bits rather than a division-by-zero or NaN.
- Scale invariance. [6, 4], [60, 40] and [600 000 000, 400 000 000] all return 0.48. Gini reads proportions, not sample volume, which is why the weights in a split have to carry the volume information separately.
- Proportions input. Switch the input mode and enter 0.6, 0.4 directly. The tool requires the values to sum to 1 within ±0.001 and tells you the actual sum when they do not, so a typo surfaces immediately instead of silently rescaling.
- Empty and negative input. All-zero counts and any negative value are rejected with a message naming the offending token; an empty node has no impurity to report.
- Mismatched split classes. If a child lists a different number of classes from the parent, the tool refuses to compute rather than pad with zeros, because silently padding would change the gain.
- Parent counts that disagree with the children. The gain is measured against the impurity of the combined child population, which is the quantity CART actually decreases. If your typed parent does not equal the sum of the children, the headline gain still describes the real partition.
Gini vs entropy vs misclassification error
Three impurity measures show up in textbooks and exam papers. They agree at the extremes and differ in the middle, which is where the choice occasionally changes a split.
| Measure | Formula | Value at [50, 50] | Value at [75, 25] |
|---|---|---|---|
| Gini impurity | 1 − Σ pₖ² | 0.5 | 0.375 |
| Shannon entropy | −Σ pₖ log₂ pₖ | 1 bit | 0.8113 bits |
| Misclassification error | 1 − max pₖ | 0.5 | 0.25 |
Entropy is on a different scale (bits, maxing at log₂K), so never compare a Gini number against an entropy number directly — compare each against its own maximum. Misclassification error is the one to avoid for growing a tree: it is piecewise linear, so it often reports zero gain for a split that genuinely improves the node, and CART uses it for pruning rather than for splitting. Gini and entropy are both strictly concave, which is what guarantees a non-negative gain for every real partition.
Worked examples
Checking your answer in Python
The arithmetic is short enough to re-derive in code, which is the quickest way to be certain a marked answer or a hand-drawn tree is right. The version below needs nothing beyond the Python standard library, so you can paste it straight into the browser-based online Python compiler and run it without installing anything.
def gini(counts):
n = sum(counts)
return 1 - sum((c / n) ** 2 for c in counts)
def gini_gain(parent, children):
n = sum(sum(c) for c in children)
weighted = sum(sum(c) / n * gini(c) for c in children)
return gini(parent) - weighted
print(gini([6, 4])) # 0.48
print(gini_gain([6, 4], [[4, 0], [2, 4]])) # 0.21333333333333332Two details in those seven lines are the ones people get wrong on paper. First, gini_gain takes its total N from the children, not from the typed parent, so the gain always describes the partition that actually happened. Second, each child is weighted by sum(c) / n before averaging — dropping that term is the unweighted-average mistake listed further down. The trailing digits on 0.21333333333333332 are ordinary binary floating point, not an error; the exact value is 16/75.
If scikit-learn is installed locally, the same numbers can be read back out of the library that defines them. Fit a depth-1 tree on a single binary feature and inspect the impurity array:
import numpy as np from sklearn.tree import DecisionTreeClassifier # 4 samples with feature 0 -> all class 0 (left child [4, 0]) # 6 samples with feature 1 -> 2 of class 0, 4 of class 1 (right child [2, 4]) X = np.array([[0]] * 4 + [[1]] * 6) y = np.array([0, 0, 0, 0] + [0, 0, 1, 1, 1, 1]) clf = DecisionTreeClassifier(criterion="gini", max_depth=1).fit(X, y) print(clf.tree_.impurity) # parent, left, right
tree_.impurity holds one Gini value per node in depth-first order, so the three entries are the parent, the left child and the right child: 0.48, 0.0 and 0.4444, matching the split example above exactly. If your own numbers disagree with scikit-learn, the cause is almost always the counts rather than the formula — a sample assigned to the wrong side of the threshold, or a class silently dropped because it had zero members in one child. Re-enter both children in split mode above and compare the per-class working table row by row.
Reading your result
The headline number means nothing without its ceiling, which is why the tool prints 1 − 1/K next to it. Against that ceiling:
- G = 0 — pure node. Every sample carries the same label; a tree turns this into a leaf.
- G below about a quarter of the maximum — dominated by one class. Useful as a leaf, but on imbalanced data check the raw counts before trusting it, as the [999, 1] example shows.
- G near the maximum — the node is close to uniform and carries almost no decision information. This is the node a split should be targeting.
- ΔG = 0 — the candidate split reproduces the parent in both children. Try another feature or threshold.
One habit worth keeping: impurity describes the training data at a node, not the model's accuracy. A tree of pure leaves has zero impurity everywhere and can still be badly overfit. Judge the finished model on held-out predictions instead — a confusion matrix with precision, recall and F1 answers the question impurity cannot.
Common mistakes
- Averaging the children without weights. Plain (G_left + G_right)/2 overstates the gain whenever the children are different sizes. Always weight by Nⱼ/N.
- Comparing Gini against entropy.0.48 Gini is not "worse" than 1.0 bit of entropy — different scales, different maxima.
- Listing classes in different orders across nodes. Gini of a single node is order-independent, but a split is not readable if the parent says [cats, dogs] and a child says [dogs, cats]. Fix the order once and keep it.
- Assuming the maximum is always 0.5. That holds only for two classes. With five classes the ceiling is 0.8.
- Confusing it with the Gini coefficient. Same surname, different statistic — impurity here, inequality there.
If you are working through a machine-learning module at a Sri Lankan university, the by-hand format in the working table above is the one most marking schemes expect: counts, proportions, squares, sum, then 1 minus the sum. Show the weighted child line separately when the question asks for information gain.
Frequently asked questions
Sources & references
- scikit-learn — Decision Trees, Mathematical formulation (Gini impurity & impurity decrease)
- Wikipedia — Decision tree learning → Gini impurity (formula, range, entropy comparison)
- Breiman, Friedman, Olshen & Stone — Classification and Regression Trees (CART, 1984)
The Gini impurity and Gini-gain formulas were last cross-checked against the scikit-learn documentation on 2026-09-01. These are standard, uncontested textbook formulas with no rates or policy that drift.
Related tools
Comments & feedback
Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.
Found a bug, edge case, or want to suggest an improvement?
Email me at [email protected] — most fixes ship within 24 hours.