Question: Implement the LASSO algorithr. The algorithm loops itercnt times. During each iteration, the algorithm updates each coordinate w d in the weight vector w (
Implement the LASSO algorithr.
The algorithm loops itercnt times. During each iteration, the algorithm updates each coordinate in the
weight vector see the slides to the update formula The coordinates should be updated in order. The
first coordinate should not be regularized, that is the update for the first feature is see the
slides as this feature corresponds to the bias term.
The algorithm should return the updated weights There are codes. One to fill the blank as the answer. But for the easyness I will give the answer checking python code since it automatically corrects the answer. Write the answer accordingly. Answer: "import sys
import numpy as np
def lassoX y s reg, itercnt:
Subgradient optimization for Lasso
Parameters
X : an array of size n k
training input data for the regressor
y : an array of size n
training output for the regressor
s : an array of size k
initial weights
reg : real
weight for the regularization term
itercnt : int
number of iterations
Returns
w : an array of size k
weights after itercnt iterations
cnt k Xshape
# make a copy of the initial vector
# we can now change single elements of w without changing s
w scopy
# place your code here
return w
def mainargv:
D nploadtxtargv
y D:copy # copy is needed, otherwise next line will mess up the splice
D: # replace the output column of D with constant, now the first feature gives us the bias term
reg floatargv
itercnt intargv
w npzerosDshape # starting point for weights
printlassoD y w reg, itercnt
# This allows the script to be used as a module and as a standalone program
if namemain:
if lensysargv:
printusage: python s filename' sysargv
else:
mainsysargv Answer correcting python code: #usrbinenv python
import sys
import unittest
from tmc import points
from tmcutils import load, getout
import numpy as np
from utils import errormsg, ApproxTest
@points
class LassoTesterApproxTest:
def testlassoself:
lasso loadsrclasso', 'lasso'
D nploadtxttesttoytxt
y D:copy # copy is needed, otherwise next line will mess up the splice
D: # replace the label column of D with constant, now the first feature gives us the bias term
w npzerosDshape
w lassoD y w
w npzeros
w
w
w
self.assertApproxw w errormsgIncorrect weights input toy.txt lam itercnt w w
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
