Skip to content
induwara.lk
Premium
induwara.lkMachine Learning · Decision Trees

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.

By Induwara AshinsanaUpdated Sep 2, 2026
Gini impuritynode & split
Matches scikit-learn

Comma- or space-separated, e.g. 6, 4 for 6 of class A and 4 of class B.

Examples
Gini impurity
0.4800
Shannon entropy
0.9710 bits
Max impurity (1 − 1/2)
0.5000
2 classes
Σ pₖ²
0.5200
N = 10

G = 1 − (0.6000² + 0.4000²) = 1 − 0.5200 = 0.4800

Cross-checked against the sum form Σ pₖ(1 − pₖ) = 0.4800.

Per-class working

ClassCount nₖProportion pₖpₖ²
A60.60000.3600
B40.40000.1600
Σ pₖ²0.5200
G = 1 − Σ pₖ²0.4800

Gini vs entropy: the two criteria nearly always pick the same split. Entropy is shown for comparison — see the ML metrics tools for the Shannon entropy and cross-entropy calculators.

Formula G = 1 − Σ pₖ²and the weighted impurity-decrease (Gini gain) follow scikit-learn's Decision Trees mathematical formulation. Last verified 2026-06-10. Full sources are listed below the calculator.

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:

  1. Class proportions. For each class, pₖ = nₖ / N. These are the fractions of samples in the node belonging to each class.
  2. 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.
  3. 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.
  4. 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.

MeasureFormulaValue at [50, 50]Value at [75, 25]
Gini impurity1 − Σ pₖ²0.50.375
Shannon entropy−Σ pₖ log₂ pₖ1 bit0.8113 bits
Misclassification error1 − max pₖ0.50.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

Maximally mixed binary node — counts [50, 50]

  1. N = 100, proportions p = [0.5, 0.5]
  2. Squares: 0.5² = 0.25 and 0.5² = 0.25
  3. Σ pₖ² = 0.25 + 0.25 = 0.50
  4. G = 1 − 0.50 = 0.5 (the maximum 1 − 1/2 for two classes)
  5. Entropy = −(0.5·log₂0.5 + 0.5·log₂0.5) = 1 bit

Skewed node — counts [3, 1]

  1. N = 4, proportions p = [0.75, 0.25]
  2. Squares: 0.75² = 0.5625 and 0.25² = 0.0625
  3. Σ pₖ² = 0.5625 + 0.0625 = 0.625
  4. G = 1 − 0.625 = 0.375
  5. Entropy = −(0.75·log₂0.75 + 0.25·log₂0.25) ≈ 0.8113 bits

Three classes, uneven — proportions [0.2, 0.3, 0.5]

  1. Input mode: proportions. They sum to 1.0, so the node is valid
  2. Squares: 0.04, 0.09, 0.25
  3. Σ pₖ² = 0.04 + 0.09 + 0.25 = 0.38
  4. G = 1 − 0.38 = 0.62
  5. Max for K = 3 is 1 − 1/3 = 0.6667, so this node is close to worst-case
  6. Entropy ≈ 1.4855 bits (max here is log₂3 ≈ 1.5850)

Split mode (Gini gain) — parent [6, 4] → [4, 0] | [2, 4]

  1. Parent: G = 1 − (0.6² + 0.4²) = 1 − 0.52 = 0.48
  2. Left child [4, 0]: pure, G = 0, weight = 4/10 = 0.4
  3. Right child [2, 4]: p = [1/3, 2/3], G = 1 − (0.1111 + 0.4444) = 0.4444, weight = 6/10 = 0.6
  4. Weighted child impurity = 0.4·0 + 0.6·0.4444 = 0.2667
  5. Gini gain ΔG = 0.48 − 0.2667 = 0.2133

Edge case — a split worth nothing: [6, 4] → [3, 2] | [3, 2]

  1. Parent: G = 1 − (0.6² + 0.4²) = 0.48
  2. Left child [3, 2]: p = [0.6, 0.4], G = 0.48, weight = 5/10 = 0.5
  3. Right child [3, 2]: identical, G = 0.48, weight = 0.5
  4. Weighted child impurity = 0.5·0.48 + 0.5·0.48 = 0.48
  5. Gini gain ΔG = 0.48 − 0.48 = 0
  6. Reading: both children mirror the parent, so this feature separates nothing. CART discards it and tries the next candidate.

Edge case — severe imbalance: counts [999, 1]

  1. N = 1000, proportions p = [0.999, 0.001]
  2. Squares: 0.998001 and 0.000001
  3. Σ pₖ² = 0.998002
  4. G = 1 − 0.998002 = 0.001998 (near-pure, but not pure)
  5. Entropy ≈ 0.0114 bits — relative to its own maximum of 1 bit, entropy flags the rare class harder than Gini does
  6. Reading: a near-zero Gini on an imbalanced node is not evidence of a good split; check the class counts, not just G.

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.21333333333333332

Two 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

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

Rate this tool
Be the first to rate

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.