Weighted Gini After Split

Medium
~15 min
code completion

Weighted Gini After a Split

Decision trees choose the split that most reduces impurity. After splitting a node into left and right children, the weighted Gini is:

where , are the child sizes and .

The information gain (impurity reduction) is then:

Higher information gain → better split.

Your task:

Implement weighted_gini(left_counts, right_counts) that returns the weighted Gini of the two children (not the information gain).

Example Tests

Both pure: weighted Gini = 0

Input: {"left_counts":[5,0],"right_counts":[0,5]}

Expected: 0

Both 50/50: weighted Gini = 0.5

Input: {"left_counts":[2,2],"right_counts":[3,3]}

Expected: 0.5

Larger pure child dominates

Input: {"left_counts":[8,0],"right_counts":[1,1]}

Expected: 0.1

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.