Skip to content

Instantly share code, notes, and snippets.

@larsbratholm
Created March 26, 2019 20:20
Show Gist options
  • Save larsbratholm/6ed4101809c926a7d4695b59234f585e to your computer and use it in GitHub Desktop.
Save larsbratholm/6ed4101809c926a7d4695b59234f585e to your computer and use it in GitHub Desktop.
Lasso example
import numpy as np
from sklearn.linear_model import Lasso, LinearRegression
# Generate 100 samples of y = x**2 plus some small random error
n = 100
x = np.random.random(100)
y = x**2 + 0.1*np.random.random(n)
# Create [x,x**2] as different features
f = np.asarray([x,x**2]).T
# Train a linear regression model as comparison
m = LinearRegression()
# Fit the model
m.fit(f,y)
# Get cooefficients
print(m.coef_) # [0.1, 0.1, 0.1, 0.1, 0.1]
# Try to add more and more l1-regularization
# to make the solution more sparse.
# A linear model can then be trained on the remaining features
for alpha in 10**np.arange(-9,0):
# Initialize the lasso class
m = Lasso(alpha=alpha)
m.fit(f,y)
print(alpha, m.coef_)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment