Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Scikit learn Scikit learn Lasso

From Leeroopedia


Knowledge Sources
Domains Machine Learning, Sparse Regression
Last Updated 2026-02-08 15:00 GMT

Overview

Concrete tool for linear regression with L1 regularization (Lasso) enabling sparse feature selection provided by scikit-learn.

Description

Lasso (Least Absolute Shrinkage and Selection Operator) implements a linear model trained with L1 prior as regularizer. It extends ElasticNet with l1_ratio fixed at 1.0. The optimization objective minimizes (1 / (2 * n_samples)) * ||y - Xw||^2_2 + alpha * ||w||_1. The L1 penalty encourages sparsity in the learned coefficients, effectively performing feature selection by driving some coefficients exactly to zero. The implementation uses coordinate descent optimization.

Usage

Use Lasso when you need a linear regression model with automatic feature selection, when you suspect many features are irrelevant, or when you want an interpretable sparse model. It is especially useful in high-dimensional settings where the number of features may exceed the number of samples.

Code Reference

Source Location

Signature

class Lasso(ElasticNet):
    def __init__(
        self,
        alpha=1.0,
        *,
        fit_intercept=True,
        precompute=False,
        copy_X=True,
        max_iter=1000,
        tol=1e-4,
        warm_start=False,
        positive=False,
        random_state=None,
        selection="cyclic",
    ):

Import

from sklearn.linear_model import Lasso

I/O Contract

Inputs

Name Type Required Description
alpha float No Constant multiplying the L1 penalty term (default=1.0)
fit_intercept bool No Whether to calculate the intercept (default=True)
precompute bool or array-like No Whether to use a precomputed Gram matrix (default=False)
copy_X bool No Whether to copy X or overwrite (default=True)
max_iter int No Maximum number of iterations (default=1000)
tol float No Tolerance for the optimization convergence (default=1e-4)
warm_start bool No Reuse solution of previous call as initialization (default=False)
positive bool No Restrict coefficients to be positive (default=False)
random_state int or RandomState No Random number generator seed for selection='random'
selection str No Coefficient update order: 'cyclic' or 'random' (default='cyclic')

Outputs

Name Type Description
coef_ ndarray of shape (n_features,) Estimated coefficients for the linear model
intercept_ float Estimated independent term in the linear model
n_iter_ int Number of iterations run by the coordinate descent solver
dual_gap_ float The dual gap at the end of optimization

Usage Examples

Basic Usage

from sklearn.linear_model import Lasso
from sklearn.datasets import make_regression

X, y = make_regression(n_samples=100, n_features=20, n_informative=5, noise=10, random_state=42)
model = Lasso(alpha=0.1)
model.fit(X, y)
print("Non-zero coefficients:", (model.coef_ != 0).sum())
print("Score:", model.score(X, y))

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment