# What is the difference between Gini index and Gini coefficient?

I am building a decision tree from scratch. I have been using entropy so far (calculated this way):

def calculate_entropy(data):

label_column = data[:,-1]
_,counts = np.unique(label_column,return_counts=True)

probabilities = counts/counts.sum()
entropy = sum(probabilities*(-np.log2(probabilities)))

return entropy


Now I want to implement a function that calculates the Gini index of an array. From here I get the following function that calculates:

def gini_coeff(x):
# requires all values in x to be zero or positive numbers,
# otherwise results are undefined
n = len(x)
s = x.sum()
r = argsort(argsort(-x)) # calculates zero-based ranks
return 1 - (2.0 * (r*x).sum() + s)/(n*s)


That for

x = [1,1,2]
gini_coeff(np.array(x))


0.16666666666666663

But from this other source I see that the Gini impurity can be calculated

$$G = \sum_i^n \cdot p(i) \cdot (1-p(i))$$

That I implemented this way

def calculate_gini(data):
label_column = data
_,counts = np.unique(label_column,return_counts=True)

probabilities = counts/counts.sum()
return sum(probabilities*(1-probabilities))

calculate_gini(x)


0.4444444444444444

I believe that I am making here a conceptual problem. What is it? What are the differences? Which one should I use for a decision tree?

The Gini impurity for a single label $$i$$ is the probability of that item occurring time the probability of some other label $$\ne i$$ occurring, which gives $$p_{i}(1-p_{i})$$. However, the Gini impurity for a set of labels ($$J$$ many labels) is the sum of this $$p_{i}(1-p_{i})$$ term across all labels, which simplifies to $$1-\sum_{i=1}^{J}{p_{i}^{2}}$$
• Note that the complement $\sum p_i^2$ is also often used. – Nick Cox Jan 22 at 17:31