Decision TreesMedium
Weighted Gini After Split
~15 mincode completion
Implement weighted_gini(left_counts, right_counts) that returns the weighted Gini of the two children (not the information gain).
Examples
Both pure: weighted Gini = 0
- Input
- weighted_gini([5, 0], [0, 5])
- Output
- 0
Both 50/50: weighted Gini = 0.5
- Input
- weighted_gini([2, 2], [3, 3])
- Output
- 0.5
Larger pure child dominates
- Input
- weighted_gini([8, 0], [1, 1])
- Output
- 0.1
Hints
Hint 1
Sum with , and check which axis you are summing over.
Hint 2
Do not forget to weighting by size. That step is easy to skip.
Requirements
left_counts: Class counts for the left childright_counts: Class counts for the right childReturn Weighted Gini = (n_L G_L + n_R G_R) / (n_L + n_R)
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Try similar problems(1)
Where this shows up
~15 min
••••••••••••••••
8 employers weight this skill
2 health and bio companies, 2 quant funds, 2 enterprise vendors, 1 AI product company, 1 frontier lab. Top match scores 85.
Python
import numpy as np
def weighted_gini(left_counts: np.ndarray, right_counts: np.ndarray) -> float:
"""
Compute the weighted Gini impurity after a binary split.
Args:
left_counts: Class counts for the left child
right_counts: Class counts for the right child
Returns:
Weighted Gini = (n_L * G_L + n_R * G_R) / (n_L + n_R)
"""
# YOUR CODE HERE
pass