Lasso Regression - Nori12/Machine-Learning-Tutorial GitHub Wiki

Machine Learning Tutorial

Overview

Lasso (Least Absolute Shrinkage and Selection Operator) is an alternative to [Ridge Regression]]. As with ridge regression, using the lasso also restricts coefficients to be close to zero, but in a slightly different way, called [L1 regularization. The consequence of L1 regularization is that when using the lasso, some coefficients are exactly zero. This means some features are entirely ignored by the model. This can be seen as a form of automatic feature selection. Having some coefficients be exactly zero often makes a model easier to interpret, and can reveal the most important features of your model.

Similarly to Ridge, the Lasso also has a regularization parameter, alpha, that controls how strongly coefficients are pushed toward zero. A lower alpha allow us to fit a more complex model. So, To reduce underfitting, decrease alpha. When we do this, we also need to increase the default setting of max_iter.

In practice, Ridge Regression is usually the first choice between these two models. However, if you have a large amount of features and expect only a few of them to be important, Lasso might be a better choice.

from sklearn.linear_model import Lasso

lasso = Lasso(alpha=0.01, max_iter=100000).fit(X_train, y_train)

Check out the Linear Regression page to see how to get the coefficients.