XGBoost Leaf Weights and Split Gain

~20 mincode completion

Implement split_gain(g, h, left_mask, lam, gamma) returning [w_left, w_right, gain] as floats. left_mask has a 1 for rows that go left and a 0 for rows that go right.

Examples

Squared error, the worked example

Input
split_gain([-1, -2, -6, -7], [1, 1, 1, 1], [1, 1, 0, 0], 1, 0)
Output
[1, 4.333333, 4.066667]

No regularization gives plain leaf means

Input
split_gain([-1, -2, -6, -7], [1, 1, 1, 1], [1, 1, 0, 0], 0, 0)
Output
[1.5, 6.5, 12.5]

Log loss gradients and hessians

Input
split_gain([-0.5, 0.5, -0.5, 0.5, -0.3], [0.25, 0.25, 0.25, 0.25, 0.21], [1, 0, 1, 0, 1], 1, 0)
Output
[0.760234, -0.666667, 0.807123]

Hints

Hint 1

Convert the input with before doing elementwise work.

Hint 2

Do not forget to lambda in denominator. That step is easy to skip.

Requirements

  • Return [w_left, w_right, gain]

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~20 min

8 employers weight this skill

2 big tech firms, 2 health and bio companies, 2 quant funds, 1 AI product company, 1 enterprise vendor. Top match scores 92.

Python
import numpy as np

def split_gain(g, h, left_mask, lam: float, gamma: float) -> list:
    """
    XGBoost leaf weights and split gain from per-row gradients and hessians.

    Returns:
        [w_left, w_right, gain]
    """
    g = np.asarray(g, dtype=float)
    h = np.asarray(h, dtype=float)
    left = np.asarray(left_mask).astype(bool)
    # YOUR CODE HERE
    pass

Run your code to see results

⌘↵ runs against the visible tests

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.