Gini Impurity

Easy
~12 min
code completion

Gini Impurity

Gini impurity measures how often a randomly chosen element would be misclassified if randomly labeled according to the class distribution. It's used by CART decision trees to choose split points.

where is the proportion of class in the node.

Properties:

  • : perfectly pure node (all one class)
  • : maximally impure binary node (50/50 split)
  • Your task:

    Implement gini_impurity(counts) where counts is an array of class counts (not probabilities). Convert to proportions first.

    Example Tests

    Pure node: Gini = 0

    Input: {"counts":[10,0]}

    Expected: 0

    50/50 binary split: Gini = 0.5

    Input: {"counts":[5,5]}

    Expected: 0.5

    3/1 split: Gini = 0.375

    Input: {"counts":[3,1]}

    Expected: 0.375

    Sign in to solve this problem

    You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.