Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

FIX ElasticNet.fit does not modify sample_weight in place #19055

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 6 doc/whats_new/v1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ Changelog
in multicore settings. :pr:`19052` by
:user:`Yusuke Nagasaka <YusukeNagasaka>`.

:mod:`sklearn.linear_model`
...........................

- |Fix| :meth:`ElasticNet.fit` no longer modifies `sample_weight` in place.
:pr:`19055` by `Thomas Fan`_.

Code and Documentation Contributors
-----------------------------------

Expand Down
2 changes: 1 addition & 1 deletion 2 sklearn/linear_model/_coordinate_descent.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ def fit(self, X, y, sample_weight=None, check_input=True):
dtype=X.dtype)
# simplify things by rescaling sw to sum up to n_samples
# => np.average(x, weights=sw) = np.mean(sw * x)
sample_weight *= (n_samples / np.sum(sample_weight))
sample_weight = sample_weight * (n_samples / np.sum(sample_weight))
# Objective function is:
# 1/2 * np.average(squared error, weights=sw) + alpha * penalty
# but coordinate descent minimizes:
Expand Down
19 changes: 19 additions & 0 deletions 19 sklearn/linear_model/tests/test_coordinate_descent.py
Original file line number Diff line number Diff line change
Expand Up @@ -1214,3 +1214,22 @@ def test_linear_models_cv_fit_for_all_backends(backend, estimator):

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


@pytest.mark.parametrize("check_input", [True, False])
def test_enet_sample_weight_does_not_overwrite_sample_weight(check_input):
"""Check that ElasticNet does not overwrite sample_weights."""

rng = np.random.RandomState(0)
n_samples, n_features = 10, 5

X = rng.rand(n_samples, n_features)
y = rng.rand(n_samples)

sample_weight_1_25 = 1.25 * np.ones_like(y)
sample_weight = sample_weight_1_25.copy()

reg = ElasticNet()
reg.fit(X, y, sample_weight=sample_weight, check_input=check_input)

assert_array_equal(sample_weight, sample_weight_1_25)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.