Vectorized Soft-Thresholding Operator

Quick Overview

Implement the soft-thresholding operator elementwise in NumPy, with broadcasting between values and thresholds, input validation, and a float return for scalar results. It tests vectorized NumPy, careful edge-case handling, and how the operator connects to L1 regularization and the Lasso.

Vectorized Soft-Thresholding Operator

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `soft_threshold(x, lam)`, which applies `sign(x) * max(|x| - lam, 0)` elementwise. `x` and `lam` may each be a scalar or an array and are broadcast together. For example: ```python soft_threshold(np.array([-3.0, -1.0, 0.5, 2.5]), 1.0) # array([-2., -0., 0., 1.5]) soft_threshold(-4.0, 1.5) # -2.5 (Python float) soft_threshold(np.array([[3.0, -3.0, 0.5]]), np.array([1.0, 2.5, 0.0])) # array([[2., -0.5, 0.5]]) ``` In the first call the `-1.0` entry comes out as negative zero, which compares equal to `0.0`. ```hint Validate before computing A comparison such as `lam < 0` is False for NaN, so watch how you test the thresholds. Converting both inputs to float arrays first lets scalars, lists and arrays go through the same code. ``` ### Constraints & Assumptions - A 0-d result is returned as a Python `float`, otherwise a `float64` `np.ndarray` with the broadcast shape. - Support per-element or per-column thresholds via NumPy broadcasting. - Any negative or `NaN` value in `lam` raises `ValueError`. - Do not modify `x` in place. No Python loops. ### What a Strong Answer Covers - A vectorized implementation that broadcasts scalar, per-element and per-column thresholds - Validation that rejects negative and NaN thresholds - The return-type rule for 0-d results, and leaving `x` unmodified - A justification of the closed form from the one-dimensional L1-penalized problem - Edge cases such as NaN or infinite values in `x`, negative zero, and shapes that do not broadcast ### Follow-up Questions - How does this operator relate to the Lasso? - How would you implement hard thresholding, and how does it differ? - How would you shrink a whole group of coefficients at once, as in the group lasso?

Overview: Implement the soft-thresholding operator elementwise in NumPy, with broadcasting between values and thresholds, input validation, and a float return for scalar results. It tests vectorized NumPy, careful edge-case handling, and how the operator connects to L1 regularization and the Lasso.

PracHub logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement soft_threshold(x, lam), which applies sign(x) * max(|x| - lam, 0) elementwise. x and lam may each be a scalar or an array and are broadcast together.

For example:

soft_threshold(np.array([-3.0, -1.0, 0.5, 2.5]), 1.0)
# array([-2., -0., 0., 1.5])

soft_threshold(-4.0, 1.5)   # -2.5 (Python float)

soft_threshold(np.array([[3.0, -3.0, 0.5]]), np.array([1.0, 2.5, 0.0]))
# array([[2., -0.5, 0.5]])

In the first call the -1.0 entry comes out as negative zero, which compares equal to 0.0.

Constraints & Assumptions

  • A 0-d result is returned as a Python float , otherwise a float64 np.ndarray with the broadcast shape.
  • Support per-element or per-column thresholds via NumPy broadcasting.
  • Any negative or NaN value in lam raises ValueError .
  • Do not modify x in place. No Python loops.

What a Strong Answer Covers Guidance

  • A vectorized implementation that broadcasts scalar, per-element and per-column thresholds
  • Validation that rejects negative and NaN thresholds
  • The return-type rule for 0-d results, and leaving x unmodified
  • A justification of the closed form from the one-dimensional L1-penalized problem
  • Edge cases such as NaN or infinite values in x , negative zero, and shapes that do not broadcast

Follow-up Questions Guidance

  • How does this operator relate to the Lasso?
  • How would you implement hard thresholding, and how does it differ?
  • How would you shrink a whole group of coefficients at once, as in the group lasso?