Skip to content
DeepForgeby svx
All problems
EasyML Fundamentalsml-081

Elastic Net Objective

Problem

Compute the elastic net objective for linear regression without an intercept: (1/(2n)) * ||Xw - y||^2 + alpha * l1_ratio * sum(|w_j|) + alpha * (1 - l1_ratio) / 2 * sum(w_j^2).

Empty X returns 0.0.

Hint

l1_ratio blends the L1 and L2 penalties; the first term is the mean squared error over 2.

Test cases

case 1
in: [[[1],[2]],[2,4],[2],0.1,0.5]
expected: 0.2
case 2
in: [[[1],[2]],[1,2],[0],0.1,0.5]
expected: 1.25
case 3
in: [[[1,0],[0,1]],[1,1],[1,-1],0.2,0.5]
expected: 1.3
case 4
in: [[],[],[1],0.1,0.5]
expected: 0
Starter codePython
def elastic_net_objective(X, y, w, alpha, l1_ratio):
    # Your code here
    pass

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