Calculate the Gini coefficient (Gini index) from a list of incomes or other values, or Gini impurity from class counts for a decision tree node.

Incomes or other values, separated by commas, spaces or new lines. Donโ€™t use thousands separators.

Optional settings


Related Calculators

Calculator Functions

FunctionWhat it doesWhat it requires
Gini coefficient from valuesFinds how unequally a list of incomes or other values is sharedRequires a list of non-negative values
Gini impurity from class countsFinds how mixed the classes are in one group of itemsRequires the number of items in each class
Weighted Gini of a decision-tree splitFinds the impurity left after a split, with the parent impurity and the Gini gainRequires the class counts for each child node, one node per line

Entries can be separated by commas, spaces or new lines, without thousands separators. The first function takes up to 10,000 values and gives every value equal weight. The coefficient is shown from 0 to 1 and again as an index from 0 to 100.

Gini Index Formulas

G = ฮฃ(2i โˆ’ n โˆ’ 1)yi รท
(n ร— ฮฃyi) [1]
GGini coefficient. Multiply by 100 for the Gini index
nNumber of values
yiThe i-th value after sorting from smallest to largest
iRank of the value, from 1 to n

Gini coefficient from values

The main formula applies after the values are sorted in ascending order. For 1, 2, 3, 4, 5 the weights (2i โˆ’ n โˆ’ 1) are โˆ’4, โˆ’2, 0, 2, 4, so the top sum is โˆ’4 โˆ’ 4 + 0 + 8 + 20 = 20. The bottom is 5 ร— 15 = 75, and G = 20 รท 75 = 0.266667, a Gini index of 26.6667.

The same number comes from adding the absolute difference between every pair of values and dividing by 2n ร— ฮฃy [1]. Equal values give 0. With n values the largest possible result is 1 โˆ’ 1/n, reached when one value holds everything, so 5 values cannot score above 0.8. Multiplying by n รท (n โˆ’ 1) turns the result into an unbiased estimate for a larger population [1]. The calculator does not apply that correction.

Gini impurity from class counts

G = 1 โˆ’ ฮฃpkยฒ [2]

pk is the share of items in class k, and the formula is the same as ฮฃpk(1 โˆ’ pk) [2]. A node with 40 items of one class and 10 of another has shares 0.8 and 0.2, so G = 1 โˆ’ 0.64 โˆ’ 0.04 = 0.32. A single-class node scores 0, and k equally filled classes score the maximum, 1 โˆ’ 1/k. This measures class mixing in a classification tree. It is a different quantity from the income-inequality coefficient, although both carry the Gini name.

Weighted Gini of a decision-tree split

Gsplit = ฮฃ(nj รท N) ร— Gj [2]

Each child node j has nj items and its own impurity Gj, and N is the total number of items. Children are weighted by size, not equally [2]. For a child with counts 40 and 10 (G = 0.32) and a child with counts 5 and 45 (G = 0.18), each holding half of the 100 items, the weighted Gini is 0.5 ร— 0.32 + 0.5 ร— 0.18 = 0.25.

The parent node is the column totals, 45 and 55, with impurity 1 โˆ’ 0.2025 โˆ’ 0.3025 = 0.495. Gini gain is the parent impurity minus the weighted Gini, 0.495 โˆ’ 0.25 = 0.245. A larger gain means the split separates the classes better. Every line must list the classes in the same order, with a zero for a class that is absent.

Limits of This Estimate

  • The coefficient treats every value as one equally weighted observation. Survey weights, grouped income brackets and negative values such as net debt are not handled.
  • No small-sample correction is applied, so short lists give a lower coefficient than the population they were drawn from would.
  • The split function scores the child nodes that are entered. It does not search for the best feature or threshold, and it takes up to 100 children and 100 classes.

Sources

  1. Wolfram MathWorld, Gini Coefficient: the sorted-values formula, the pairwise-difference form and the n รท (n โˆ’ 1) correction
  2. scikit-learn documentation, 1.10 Decision Trees, Mathematical formulation: Gini impurity of a node and the sample-weighted impurity of a split