Decision Trees
Easy
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:
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