Skip to content

FIX LinearRegression sample weight bug (numpy solver) #30030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/whats_new/v1.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ Changelog
:mod:`sklearn.linear_model`
...........................

- |Fix| :class:`linear_model.LinearRegression` replaces the `scipy.linalg.lstsq`
solver by `numpy.linalg.lstsq` and sets the `rcond` parameter.
:pr:`30030` by :user:`Antoine Baker <antoinebaker>`.

- |Fix| :class:`linear_model.LogisticRegressionCV` corrects sample weight handling
for the calculation of test scores.
:pr:`29419` by :user:`Shruti Nath <snath-xoc>`.
Expand Down
10 changes: 7 additions & 3 deletions sklearn/linear_model/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import numpy as np
import scipy.sparse as sp
from scipy import linalg, optimize, sparse
from scipy import optimize, sparse
from scipy.sparse.linalg import lsqr
from scipy.special import expit

Expand Down Expand Up @@ -525,7 +525,7 @@ class LinearRegression(MultiOutputMixin, RegressorMixin, LinearModel):
Notes
-----
From the implementation point of view, this is just plain Ordinary
Least Squares (scipy.linalg.lstsq) or Non Negative Least Squares
Least Squares (numpy.linalg.lstsq) or Non Negative Least Squares
(scipy.optimize.nnls) wrapped as a predictor object.

Examples
Expand Down Expand Up @@ -673,7 +673,11 @@ def rmatvec(b):
)
self.coef_ = np.vstack([out[0] for out in outs])
else:
self.coef_, _, self.rank_, self.singular_ = linalg.lstsq(X, y)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case that we keep scipy lstsq, it might be safe here to pass check_finite=False since we validate X and y or do we consider that the centering could trigger inf and nan for some reason?

Copy link
Contributor Author

@antoinebaker antoinebaker Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a good idea to improve the performance, I'll add the option in the benchmark. After validating X,y and checking sample_weight I think we are safe, except that _check_sample_weight with ensure_non_negative=True still allows all zero weights, so we should probably raise an error in that case.

# cut-off ratio for small singular values (numpy 2.0 default value)
rcond = max(X.shape) * np.finfo(X.dtype).eps
self.coef_, _, self.rank_, self.singular_ = np.linalg.lstsq(
X, y, rcond=rcond
)
self.coef_ = self.coef_.T

if y.ndim == 1:
Expand Down
14 changes: 3 additions & 11 deletions sklearn/linear_model/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,17 +754,9 @@ def test_linear_regression_sample_weight_consistency(
if fit_intercept:
intercept_0 = reg.intercept_
reg.fit(X[:-5], y[:-5], sample_weight=sample_weight[:-5])
if fit_intercept and sparse_container is None:
# FIXME: https://github.com/scikit-learn/scikit-learn/issues/26164
# This often fails, e.g. when calling
# SKLEARN_TESTS_GLOBAL_RANDOM_SEED="all" pytest \
# sklearn/linear_model/tests/test_base.py\
# ::test_linear_regression_sample_weight_consistency
pass
else:
assert_allclose(reg.coef_, coef_0, rtol=1e-5)
if fit_intercept:
assert_allclose(reg.intercept_, intercept_0)
assert_allclose(reg.coef_, coef_0, rtol=1e-5)
if fit_intercept:
assert_allclose(reg.intercept_, intercept_0)

# 5) check that multiplying sample_weight by 2 is equivalent to repeating
# corresponding samples twice
Expand Down