In [1]:
import sys
sys.version
sys.version_info


Out[1]:
sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)

In [2]:
import sys
sys.path.append('../')

In [3]:
import scipy.sparse as sp
import numpy as np

In [4]:
from lasso import SparseLasso

In [5]:
# try the code used in the scikit-learn example. 
skl_ex_X = sp.csc_matrix([[0.,0], [1, 1], [2, 2]])
skl_ex_Y = np.array([0, 1, 2])

In [6]:
sp.csc_matrix(np.array([[0, 1, 2]]))


Out[6]:
<1x3 sparse matrix of type '<class 'numpy.int64'>'
	with 2 stored elements in Compressed Sparse Column format>

In [7]:
lasso_toy = SparseLasso(X = skl_ex_X, 
            y = skl_ex_Y,
            lam = 0.1,
            #w = np.array([0., 0.]),
            verbose = True
           )
lasso_toy.run()
lasso_toy.w.toarray()


> /Users/janet/Machine_Learning_CSE_546/HW1/Q7_lasso/lasso.py(28)__init__()
-> if w is None:
(Pdb) c
0.098750036122
  (0, 0)	0.0
  (1, 0)	0.975104098956
Out[7]:
array([[ 0.       ],
       [ 0.9751041]])

In [8]:
print(lasso_toy.w.toarray())
print("")
print(lasso_toy.w0)
print("")
print(lasso_toy.objective())
print("")
print(lasso_toy.calc_yhat())


[[ 0.       ]
 [ 0.9751041]]

0.0248265017407

0.098750036122

[[ 0.0248265]
 [ 0.9999306]
 [ 1.9750347]]

Compare to sklearn


In [9]:
from sklearn import linear_model
lam = 0.1
alpha = lam/(2*3)  # hard coded for 3 sample points. 
clf = linear_model.Lasso(alpha)  # 3 samples  http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Lasso.html
clf.fit([[0.,0], [1, 1], [2, 2]], [0., 1, 2])

print(clf.coef_)
print(clf.intercept_)
print(clf.predict([[0.,0], [1, 1], [2, 2]]))


[  9.75000000e-01   4.16333634e-17]
0.025
[ 0.025  1.     1.975]

High-printing test case


In [10]:
# try the code used in the scikit-learn example. 
toy_X = sp.csc_matrix([[0.,1], [1, 2], [2, 3]])
toy_Y = np.array([2., 5, 8])

In [11]:
toy = SparseLasso(X = toy_X, 
            y = toy_Y,
            lam = 0.1,
            #w = np.array([0., 0.]),
            verbose = True
           )


> /Users/janet/Machine_Learning_CSE_546/HW1/Q7_lasso/lasso.py(28)__init__()
-> if w is None:
(Pdb) c

In [12]:
toy.run()


0.298750064354
  (0, 0)	2.2406977257
  (1, 0)	0.734136201636

In [13]:
print(toy.w.toarray())
print(toy.w0)


[[ 2.24069773]
 [ 0.7341362 ]]
1.29108522858

In [14]:
from sklearn import linear_model
lam = 0.1
alpha = lam/(2*3)  # hard coded for 3 sample points. 
clf = linear_model.Lasso(alpha)  # 3 samples  http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Lasso.html
clf.fit([[0.,1], [1, 2], [2, 3]], [2., 5, 8])

print(clf.coef_)
print(clf.intercept_)
print(clf.predict([[0.,1], [1, 2], [2, 3]]))


[ 2.975  0.   ]
2.025
[ 2.025  5.     7.975]

In [ ]: