Skip to content

Selecting Lasso via cross-validation data leakage #26359

Open

Description

@nielsbijl

Describe the issue linked to the documentation

model = make_pipeline(StandardScaler(), LassoCV(cv=20)).fit(X, y)

Optimizing Alpha for lasso involves data leakage because standard scaling is applied on X and y in one go, instant for each fold.

It is used in the example Selecting Lasso via cross-validation
https://scikit-learn.org/stable/auto_examples/linear_model/plot_lasso_model_selection.html

Suggest a potential alternative/fix

No response

Activity

  1. adrinjalali commented on May 12, 2023

    @adrinjalali
    Member

    Thanks for bringing this up, I've been wondering about our *CV classes too. Unless certain conditions are met, we always have leakage in those pipelines, and that's true for all of them (except *SearchCV taking the whole pipeline).

    We should probably make that very clear, and make sure none of our examples have this issue. And I think if the user has enough CPU cycles at their hand, it's almost always safer to use a *SearchCV.

    I wonder what @agramfort thinks here.

  2. agramfort commented on May 12, 2023

    @agramfort
    Member

    do you have empirical evidence that the alpha obtained is one option or the other is very different and that the test performance degrades with the approach model = make_pipeline(StandardScaler(), LassoCV(cv=20)).fit(X, y) ?

  3. adrinjalali commented on Jun 1, 2023

    @adrinjalali
    Member

    @agramfort as soon as there are outliers, they're not the same anymore, here's an example:

    from sklearn.datasets import make_regression
    from sklearn.linear_model import Lasso, LassoCV
    from sklearn.pipeline import make_pipeline
    from sklearn.preprocessing import StandardScaler
    from sklearn.model_selection import GridSearchCV
    
    X, y = make_regression(n_samples=200, n_features=500, n_informative=5, random_state=0)
    X[:2, ] = X[:2, ] +20
    
    est = make_pipeline(StandardScaler(), LassoCV(cv=5)).fit(X, y)
    
    est_gs = GridSearchCV(
        make_pipeline(StandardScaler(), Lasso(random_state=0)),
        param_grid={"lasso__alpha": est[-1].alphas_},
        cv=5,
    ).fit(X, y)
    print(est[-1].alpha_)
    print(est_gs.best_estimator_[-1].alpha)

    Gives these two alphas:

    44.79962005897805
    51.508676553970744
    
  4. agramfort commented on Jul 29, 2023

    @agramfort
    Member

    two remarks here:

    • Using a GridSearchCV for a Lasso is a terrible waste of time as you start from scratch for each alpha. I suspect a 100x difference in running time unless you accept to have your screened filled with "ConvergenceWarning: Objective did not converge."
    • if you have outliers maybe use a RobustScaler? Using a RobustScaler with the snippet above I get as alphas:
      66.89463732039701
      66.89463732039701
      So it seems to do the job.

    my 2c

  5. adrinjalali commented on Aug 1, 2023

    @adrinjalali
    Member

    I think my point is that people need to really know what they're doing if they use something like LassoCV. We should have very clear warnings and describe why using is might be okay, and when it can be used.

  6. jrasero commented on Aug 29, 2023

    @jrasero

    Hi,

    Sorry for jumping in here, but this is an issue I have been revisiting a lot lately.

    The problem with the current implementation of LassoCV (and other *CV estimators like RidgeCV) is that it is quite restrictive since it is only appropriate when features in the input data are all in similar units. When features have different units (e.g. weight vs height), then you need to scale them so that some features (those with small units) do not get more penalized than others (those with large units). But you can't create a pipeline like @nielsbijl showed, because that would cause data leakage. So the only alternative to do this properly would be through GridSearchCV, but this is less efficient compared to LassoCV.

    How hard would it be to implement a scale step within LassoCV? In fact, common libraries in R (https://glmnet.stanford.edu/articles/glmnet.html) and MATLAB (https://www.mathworks.com/help/stats/lasso.html) implementing a similar function perform a scaling operation by default (it can be turned off though).

  7. agramfort commented on Aug 30, 2023

    @agramfort
    Member

    at this point if you really want to do this I would suggest you use an older version of sklearn that had the "normalize" parameter for LassoCV.

  8. Barath-kalidass commented on Sep 16, 2026

    @Barath-kalidass

    Hi, I would like to work on this issue as part of my open-source contribution. I can update the documentation/example to address the cross-validation data leakage concern. Please let me know if I can proceed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions