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 wd in the
weight vector w(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 wd=cdad(see the
slides), as this feature corresponds to the bias term.
The algorithm should return the updated weights w. There are 2 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 lasso(X, 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 = X.shape
# make a copy of the initial vector
# we can now change single elements of w without changing s
w = s.copy()
# place your code here
return w
def main(argv):
D = np.loadtxt(argv[1])
y = D[:,0].copy() # copy is needed, otherwise next line will mess up the splice
D[:,0]=1 # replace the output column of D with constant, now the first feature gives us the bias term
reg = float(argv[2])
itercnt = int(argv[3])
w = np.zeros(D.shape[1]) # starting point for weights
print(lasso(D, y, w, reg, itercnt))
# This allows the script to be used as a module and as a standalone program
if __name__=="__main__":
if len(sys.argv)!=4:
print('usage: python %s filename' % sys.argv[0])
else:
main(sys.argv)" Answer correcting python code: "#!/usr/bin/env python3
import sys
import unittest
from tmc import points
from tmc.utils import load, get_out
import numpy as np
from .utils import errormsg, ApproxTest
@points('2.3.1','2.3.2','2.3.3','2.3.4','2.3.5')
class LassoTester(ApproxTest):
def test_lasso(self):
lasso = load('src.lasso', 'lasso')
D = np.loadtxt('test/toy.txt')
y = D[:,0].copy() # copy is needed, otherwise next line will mess up the splice
D[:,0]=1 # replace the label column of D with constant, now the first feature gives us the bias term
w = np.zeros(D.shape[1])
w = lasso(D, y, w,10,100)
w1= np.zeros(11)
w1[0]=3.01421845
w1[3]=0.97828604
w1[5]=0.9834849
self.assertApprox(w1, w, errormsg("Incorrect weights (input toy.txt, lam =10, itercnt =100)", w1, w))"
Implement the LASSO algorithr. The algorithm

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!