Skip to content

Commit b001ea3

Browse files
committed
ENH Lasso path plot
1 parent 045d3a1 commit b001ea3

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

ch07/lasso_path_plot.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This code is supporting material for the book
2+
# Building Machine Learning Systems with Python
3+
# by Willi Richert and Luis Pedro Coelho
4+
# published by PACKT Publishing
5+
#
6+
# It is made available under the MIT License
7+
8+
from sklearn.linear_model import Lasso
9+
from sklearn.datasets import load_boston
10+
from matplotlib import pyplot as plt
11+
import numpy as np
12+
13+
boston = load_boston()
14+
x = boston.data
15+
y = boston.target
16+
17+
las = Lasso(normalize=1)
18+
alphas = np.logspace(-5, 2, 1000)
19+
_, coefs, _= las.path(x, y, alphas=alphas)
20+
21+
fig,ax = plt.subplots()
22+
ax.plot(alphas, coefs.T)
23+
ax.set_xscale('log')
24+
ax.set_xlim(alphas.max(), alphas.min())
25+
ax.set_xlabel('Alpha')
26+
ax.set_ylabel('Coefficient weight')
27+
fig.savefig('Figure_LassoPath.png', dpi=150)
28+

0 commit comments

Comments
 (0)