8000 [MRG] Fix LinearModelsCV for loky backend. by jeremiedbb · Pull Request #14264 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

[MRG] Fix LinearModelsCV for loky backend. #14264

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

Merged
Merged
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
6 changes: 6 additions & 0 deletions doc/whats_new/v0.23.rst
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ Changelog
random noise to the target. This might help with stability in some edge
cases. :pr:`15179` by :user:`angelaambroz`.

- |Fix| Fixed a bug in :class:`linear_model.ElasticNetCV`,
:class:`linear_model.MultitaskElasticNetCV`, :class:`linear_model.LassoCV`
and :class:`linear_model.MultitaskLassoCV` where fitting would fail when
using joblib loky backend. :pr:`14264` by
:user:`Jérémie du Boisberranger <jeremiedbb>`.

:mod:`sklearn.metrics`
......................

Expand Down
9 changes: 9 additions & 0 deletions sklearn/linear_model/_coordinate_descent.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,15 @@ def _path_residuals(X, y, train, test, path, path_params, alphas=None,
y_train = y[train]
X_test = X[test]
y_test = y[test]

if not sparse.issparse(X):
for array, array_input in ((X_train, X), (y_train, y),
F099 (X_test, X), (y_test, y)):
if array.base is not array_input and not array.flags['WRITEABLE']:
# fancy indexing should create a writable copy but it doesn't
# for read-only memmaps (cf. numpy#14132).
array.setflags(write=True)

fit_intercept = path_params['fit_intercept']
normalize = path_params['normalize']

Expand Down
23 changes: 23 additions & 0 deletions sklearn/linear_model/tests/test_coordinate_descent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import pytest
from scipy import interpolate, sparse
from copy import deepcopy
import joblib
from distutils.version import LooseVersion

from sklearn.datasets import load_boston
from sklearn.datasets import make_regression
from sklearn.exceptions import ConvergenceWarning
from sklearn.utils._testing import assert_array_almost_equal
from sklearn.utils._testing import assert_almost_equal
Expand Down Expand Up @@ -1020,3 +1023,23 @@ def test_enet_sample_weight_sparse():
with pytest.raises(ValueError, match="Sample weights do not.*support "
"sparse matrices"):
reg.fit(X, y, sample_weight=sw, check_input=True)


@pytest.mark.parametrize("backend", ["loky", "threading"])
@pytest.mark.parametrize("estimator",
[ElasticNetCV, MultiTaskElasticNetCV,
LassoCV, MultiTaskLassoCV])
def test_linear_models_cv_fit_for_all_backends(backend, estimator):
# LinearModelsCV.fit performs inplace operations on input data which is
# memmapped when using loky backend, causing an error due to unexpected
# behavior of fancy indexing of read-only memmaps (cf. numpy#14132).

if joblib.__version__ < LooseVersion('0.12') and backend == 'loky':
pytest.skip('loky backend does not exist in joblib <0.12')

# Create a problem sufficiently large to cause memmapping (1MB).
n_targets = 1 + (estimator in (MultiTaskElasticNetCV, MultiTaskLassoCV))
X, y = make_regression(20000, 10, n_targets=n_targets)

with joblib.parallel_backend(backend=backend):
estimator(n_jobs=2, cv=3).fit(X, y)
0