Skip to content

Instantly share code, notes, and snippets.

@sschnug
Last active October 3, 2018 06:38
Show Gist options
  • Save sschnug/a32fd0facf55e168aa58aa505893f238 to your computer and use it in GitHub Desktop.
Save sschnug/a32fd0facf55e168aa58aa505893f238 to your computer and use it in GitHub Desktop.
lasso init in sklearn
import numpy as np
from sklearn.datasets.samples_generator import make_regression
from sklearn.linear_model import Lasso
X, y = make_regression(n_samples=200, n_features=5000, random_state=0)
dense_lasso = Lasso(alpha=1, fit_intercept=False, max_iter=1000)
dense_lasso.fit(X, y)
print('iterations needed: ', dense_lasso.n_iter_)
init_lasso = Lasso(alpha=1, fit_intercept=False, max_iter=1000, warm_start=True)
init_lasso.coef_ = dense_lasso.coef_ + np.random.uniform(size=dense_lasso.coef_.shape[0])
init_lasso.fit(X, y)
print('iterations needed using init: ', init_lasso.n_iter_)
init_lasso_no_noise = Lasso(alpha=1, fit_intercept=False, max_iter=1000, warm_start=True)
init_lasso_no_noise.coef_ = dense_lasso.coef_
init_lasso_no_noise.fit(X, y)
print('iterations needed using init: ', init_lasso_no_noise.n_iter_)
# Output:
# iterations needed: 61
# iterations needed using init: 11
# iterations needed using init: 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment