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 child

  • right_counts: Class counts for the right child

  • Return 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

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
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.