Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsml-082

L1 Soft Thresholding

Problem

Apply the L1 soft-thresholding operator element-wise: sign(v) * max(|v| - lam, 0).

Return the transformed list; empty input returns [].

Hint

Shrink values toward zero by lam and clamp anything inside [-lam, lam] to zero.

Test cases

case 1
in: [[3,-2,0.5],1]
expected: [2,-1,0]
case 2
in: [[1],2]
expected: [0]
case 3
in: [[-5,5],0]
expected: [-5,5]
case 4
in: [[],1]
expected: []
Starter codePython
def soft_threshold(values, lam):
    # Your code here
    pass

4 test cases · run your code against them in the editor