Skip to content
DeepForgeby svx
All problems
EasyInformation Theoryinfo-319

Gini Impurity of Labels

Problem

Compute the Gini impurity of a list of labels, defined as 1 - sum(p_i^2) where p_i is the empirical frequency of each distinct label. Return 0.0 for an empty list.

Hint

Count each label, square the frequencies, and subtract from one.

Test cases

case 1
in: [["a","b"]]
expected: 0.5
case 2
in: [["a","a","b","b"]]
expected: 0.5
case 3
in: [["a","a","a"]]
expected: 0
case 4
in: [[]]
expected: 0
case 5
in: [["a","b","c","c"]]
expected: 0.625
Starter codePython
def gini_impurity(labels):
    # Your code here
    pass

5 test cases · run your code against them in the editor