Skip to content

FIX: resolve sklearn 1.9 compatibility for WeightedLassoCV n_alphas deprecation (#1032 )#1046

Open
genrichez wants to merge 1 commit into
py-why:mainfrom
genrichez:fix/sklearn-1.9-n-alphas-compat
Open

FIX: resolve sklearn 1.9 compatibility for WeightedLassoCV n_alphas deprecation (#1032 )#1046
genrichez wants to merge 1 commit into
py-why:mainfrom
genrichez:fix/sklearn-1.9-n-alphas-compat

Conversation

@genrichez

Copy link
Copy Markdown

Summary

Fixes InvalidParameterError and FutureWarning when using EconML with scikit-learn >= 1.7 (where n_alphas is deprecated) and >= 1.9 (where it will be removed).

Closes #1032

Problem

In sklearn 1.7+, LassoCV and MultiTaskLassoCV deprecated the n_alphas parameter in favor of passing an int directly to alphas. The existing version check in WeightedLassoCV.__init__ correctly passes alphas=n_alphas to super().__init__(), but two issues remained:

  1. self.alphas was set to None (the default alphas kwarg value) instead of the effective int value, causing InvalidParameterError: The 'alphas' parameter must be an int or array-like. Got None instead at fit time.

  2. n_alphas leaked into get_params() output. When sklearn's LinearModelCV.fit() calls self.get_params() to build path_params, n_alphas was passed to lasso_path(), triggering FutureWarning: 'n_alphas' was deprecated in 1.9 and will be removed in 1.11.

Fix

  • Set self.alphas to the effective value (alphas if provided, else n_alphas as int) in the >= 1.7 branch
  • Override get_params() to exclude n_alphas from the returned dict on sklearn >= 1.7, preventing it from leaking into lasso_path(**path_params)
  • Apply same fix to WeightedMultiTaskLassoCV

Testing

Verified with sklearn 1.9.0:

Test Result
Import econml.orf (triggers WeightedLassoCV init) PASS
WeightedLassoCV.fit() with default n_alphas=100 PASS
WeightedLassoCV.fit() with n_alphas=1 (edge) PASS
WeightedLassoCV.fit() with n_alphas=1000 (large) PASS
WeightedLassoCV.fit() with explicit alphas array PASS
Both n_alphas and alphas provided (alphas takes precedence) PASS
WeightedMultiTaskLassoCV.fit() PASS
WeightedLassoCVWrapper single/multi-target PASS
WeightedLassoCVWrapper with sample_weight PASS
DebiasedLasso (uses WeightedLassoCV internally) PASS
LinearDML end-to-end pipeline PASS
sklearn.clone() compatibility PASS
set_params() round-trip PASS
Pickle serialization round-trip PASS
Zero FutureWarning about n_alphas PASS
Alpha grid size matches n_alphas (10, 50, 100, 200) PASS
WeightedLassoCV == sklearn LassoCV when no weights (coef diff=0) PASS
Sample weights correctly affect coefficients PASS
DebiasedLasso CI coverage ~95% PASS

Backward Compatibility

  • WeightedLassoCV(n_alphas=50) still works on all sklearn versions
  • WeightedLassoCV(alphas=[0.1, 0.01]) still works
  • get_params() / set_params() / clone() all work correctly
  • No behavior change on sklearn < 1.7

@genrichez genrichez force-pushed the fix/sklearn-1.9-n-alphas-compat branch from a4c2796 to 41596d9 Compare July 7, 2026 21:28
…eprecation

On sklearn >= 1.7, the n_alphas parameter in LassoCV/MultiTaskLassoCV is
deprecated (removed in 1.11) in favor of passing an int to alphas.

The existing version check correctly passes alphas=n_alphas to super().__init__(),
but the n_alphas attribute leaked into get_params() output. When sklearn's
LinearModelCV.fit() calls self.get_params() to build path_params, n_alphas was
passed to lasso_path(), triggering FutureWarnings. Additionally, self.alphas was
being set to None (the default) instead of the effective int value, causing
InvalidParameterError at fit time.

Fix:
- Set self.alphas to the effective value (alphas if provided, else n_alphas)
  in the >= 1.7 branch
- Override get_params() to exclude n_alphas from the returned dict on
  sklearn >= 1.7, preventing it from leaking into lasso_path()
- Apply same fix to WeightedMultiTaskLassoCV

Closes py-why#1032

Signed-off-by: genrichez <2.2434764e+07+genrichez@users.noreply.github.com>
@genrichez genrichez force-pushed the fix/sklearn-1.9-n-alphas-compat branch from 41596d9 to 79d580a Compare July 8, 2026 09:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scikit-learn v1.9 drops n_alphas from LassoCV and related estimators, causing econml to error on import

1 participant