Implement the group lasso penalty and its block soft-thresholding prox in NumPy

Quick Overview

Implement the group lasso penalty and its proximal operator, block soft-thresholding, in NumPy for unsorted and interleaved group labels. Tests how a separable prox splits into per-group problems, how the threshold scales with group size, exact zeroing of whole groups, and input validation.

Implement the group lasso penalty and its block soft-thresholding prox in NumPy

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement two NumPy functions for the group lasso. `group_lasso_penalty(beta, groups, lam)` returns the penalty as a Python `float`: $$ \lambda \sum_g \sqrt{p_g}\, \lVert \beta_g \rVert_2 $$ Here $p_g$ is the group size and $\beta_g$ is the sub-vector of `beta` whose entries carry label $g$ in `groups`. `group_soft_threshold(v, groups, lam, step=1.0)` is the proximal operator of `step * group_lasso_penalty`. For each group, with $t = \text{step} \cdot \lambda \sqrt{p_g}$, the output block is $(1 - t / \lVert v_g \rVert_2)\, v_g$ if $\lVert v_g \rVert_2 > t$, and exactly zeros otherwise. Example: ```python v = [-3, 2, 2, 2, 2] groups = [0, 1, 1, 1, 1] group_soft_threshold(v, groups, lam=0.5, step=2.0) # -> array([-2., 1., 1., 1., 1.]) group_lasso_penalty(v, groups, lam=0.5) # -> 5.5, which is 0.5 * (1 * 3 + 2 * 4) ``` ```hint Selecting a group Labels can be unsorted and interleaved, as in `[3, 0, 3, 7, 0]`. Think about how to pick out one group's entries without assuming they sit next to each other. ``` ```hint The zero block The shrink formula divides by the block norm. Check which comparison against $t$ keeps that division away from a zero norm, and what a block with norm exactly $t$ should become. ``` ### Constraints & Assumptions - `v` (or `beta`) and `groups` must be 1-D and of equal length, otherwise raise `ValueError`. - `lam < 0` raises `ValueError` in both functions, and `step <= 0` raises `ValueError` in `group_soft_threshold`. - Group labels are integers that may be non-contiguous and interleaved. Groups do not overlap: each coefficient has exactly one label. - `group_soft_threshold` returns a new float `np.ndarray` of the same shape and does not mutate its input. ### What a Strong Answer Covers - Selecting each group by its label, so unsorted, non-contiguous and interleaved labels all work. - A threshold that scales with `step`, `lam` and the square root of the group size, and a block that is exactly zero when its norm is at most the threshold. - Why the proximal operator splits into one independent problem per group, and why keeping the direction while shrinking the norm solves each one. - Input validation, a fresh float output, and the time and space cost of the grouping strategy. ### Follow-up Questions - How would you use this operator inside a proximal gradient solver for group-lasso regression? - How would you extend this to the sparse group lasso? - What changes if groups overlap?

Overview: Implement the group lasso penalty and its proximal operator, block soft-thresholding, in NumPy for unsorted and interleaved group labels. Tests how a separable prox splits into per-group problems, how the threshold scales with group size, exact zeroing of whole groups, and input validation.

PracHub logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement two NumPy functions for the group lasso.

group_lasso_penalty(beta, groups, lam) returns the penalty as a Python float:

λ∑gpg ∥βg∥2\lambda \sum_g \sqrt{p_g}\, \lVert \beta_g \rVert_2

Here pgp_g is the group size and βg\beta_g is the sub-vector of beta whose entries carry label gg in groups.

group_soft_threshold(v, groups, lam, step=1.0) is the proximal operator of step * group_lasso_penalty. For each group, with t=step⋅λpgt = \text{step} \cdot \lambda \sqrt{p_g}, the output block is (1−t/∥vg∥2) vg(1 - t / \lVert v_g \rVert_2)\, v_g if ∥vg∥2>t\lVert v_g \rVert_2 > t, and exactly zeros otherwise.

Example:

v = [-3, 2, 2, 2, 2]
groups = [0, 1, 1, 1, 1]

group_soft_threshold(v, groups, lam=0.5, step=2.0)
# -> array([-2., 1., 1., 1., 1.])

group_lasso_penalty(v, groups, lam=0.5)
# -> 5.5, which is 0.5 * (1 * 3 + 2 * 4)

Constraints & Assumptions

  • v (or beta ) and groups must be 1-D and of equal length, otherwise raise ValueError .
  • lam < 0 raises ValueError in both functions, and step <= 0 raises ValueError in group_soft_threshold .
  • Group labels are integers that may be non-contiguous and interleaved. Groups do not overlap: each coefficient has exactly one label.
  • group_soft_threshold returns a new float np.ndarray of the same shape and does not mutate its input.

What a Strong Answer Covers Guidance

  • Selecting each group by its label, so unsorted, non-contiguous and interleaved labels all work.
  • A threshold that scales with step , lam and the square root of the group size, and a block that is exactly zero when its norm is at most the threshold.
  • Why the proximal operator splits into one independent problem per group, and why keeping the direction while shrinking the norm solves each one.
  • Input validation, a fresh float output, and the time and space cost of the grouping strategy.

Follow-up Questions Guidance

  • How would you use this operator inside a proximal gradient solver for group-lasso regression?
  • How would you extend this to the sparse group lasso?
  • What changes if groups overlap?