Skip to content

fix: complete sklearn 1.9 support — drop self.alphas overwrite (closes #1032)#1042

Open
immu4989 wants to merge 1 commit into
py-why:mainfrom
immu4989:fix-1032-sklearn-1.9-n_alphas
Open

fix: complete sklearn 1.9 support — drop self.alphas overwrite (closes #1032)#1042
immu4989 wants to merge 1 commit into
py-why:mainfrom
immu4989:fix-1032-sklearn-1.9-n_alphas

Conversation

@immu4989

Copy link
Copy Markdown
Contributor

Closes #1032.

What's happening

#1032 (filed by @jakevdp) reports that import econml.orf errors on scikit-learn 1.9 because n_alphas was removed from LassoCV.__init__.

The recent commit e546416 added a >= 1.7 version dispatch in WeightedLassoCV.__init__ / WeightedMultiTaskLassoCV.__init__ that translates n_alphas=<int> into alphas=<int> on the super().__init__() call. That fixes the import-time TypeError jakevdp reported.

But e546416 also added self.alphas = alphas at the end of the dispatch, which clobbers the value sklearn's __init__ had correctly recorded back to the constructor's alphas kwarg (None by default):

if parse(sklearn.__version__) >= parse("1.7"):
    super().__init__(
        eps=eps, alphas=alphas if alphas is not None else n_alphas,  # alphas=100 ✓
        ...)
    self.n_alphas = n_alphas
    self.alphas = alphas    # ← overwrites sklearn's alphas=100 back to None

On sklearn 1.7–1.8 the loose param-validation tolerated the resulting self.alphas = None. On sklearn 1.9 the stricter _param_validation rejects it, so SparseLinearDML.fit (via _DebiasedLasso.fitWeightedLassoCV) and DebiasedLasso.fit raise:

sklearn.utils._param_validation.InvalidParameterError:
The 'alphas' parameter of WeightedLassoCV must be an int in the range [1, inf)
or an array-like. Got None instead.

Fix

Drop the overwrite. super().__init__(...) already records self.alphas from the translated value. self.n_alphas is still set so callers can introspect the original wrapper kwarg.

Same edit applied to both WeightedLassoCV.__init__ and WeightedMultiTaskLassoCV.__init__.

Also bumps pyproject.toml scikit-learn >= 1.0, < 1.9< 1.10 so users can actually install sklearn 1.9.

Verification (locally, sklearn 1.9.0 + narwhals)

  • import econml.orf ✓ (jakevdp's exact repro)
  • Smoke fits all succeed on sklearn 1.9: LinearDML, SparseLinearDML, CausalForestDML, DMLOrthoForest, LinearDRLearner, SparseLinearDRLearner
  • New test_default_alphas_fits_on_strict_sklearn regression test in TestLassoExtensions:
    • Asserts WeightedLassoCV().alphas is not None and WeightedMultiTaskLassoCV().alphas is not None (the dispatched value must survive)
    • Calls .fit(...) on each, verifying sklearn 1.9 strict validation accepts the result
    • Pre-fix on sklearn 1.9: fails with AssertionError: WeightedLassoCV.alphas was clobbered to None by __init__ (#1032)
    • Post-fix: passes
  • Broader sweep: 48 passed, 0 failed across test_linear_model.py + test_dml.py + test_treatment_featurization.py on sklearn 1.9.

…y#1032)

py-why#1032 reports econml errors on import with scikit-learn 1.9. e546416
("Fix scikit-learn 1.7+ FutureWarnings ...") added a >=1.7 dispatch in
WeightedLassoCV / WeightedMultiTaskLassoCV that translates
n_alphas=<int> into alphas=<int> on the super().__init__() call, which
fixes the import-time TypeError. But it then ran self.alphas = alphas
at the end of the dispatch, overwriting the value sklearn's __init__
had correctly recorded back to the constructor's alphas kwarg (None by
default).

On sklearn 1.7-1.8 the loose param-validation tolerated the resulting
self.alphas = None. sklearn 1.9's stricter _param_validation rejects it,
so SparseLinearDML.fit (via _DebiasedLasso.fit -> WeightedLassoCV) and
DebiasedLasso.fit raise InvalidParameterError.

Drop the overwrite. super().__init__(...) already records self.alphas
from the translated value. self.n_alphas is still set so callers can
introspect the original wrapper kwarg.

Also bump scikit-learn pin from < 1.9 to < 1.10 so 1.9 installs.
Adds test_default_alphas_fits_on_strict_sklearn covering both
WeightedLassoCV and WeightedMultiTaskLassoCV; verifies on sklearn 1.9
locally that 48 tests pass across test_linear_model, test_dml, and
test_treatment_featurization (no regressions).

Signed-off-by: Imran Ahamed <immu4989@gmail.com>
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