From d05db36366f0db3a1b07d8999ad9fe49d0bb0ce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20Simon?= Date: Sun, 29 Aug 2021 00:07:45 +0200 Subject: [PATCH 01/19] Adding informative error message for the negative weight case (linear regression) --- sklearn/linear_model/_base.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sklearn/linear_model/_base.py b/sklearn/linear_model/_base.py index 79d6f321cb124..4c9651db2d3bf 100644 --- a/sklearn/linear_model/_base.py +++ b/sklearn/linear_model/_base.py @@ -664,6 +664,8 @@ def fit(self, X, y, sample_weight=None): if sample_weight is not None: sample_weight = _check_sample_weight(sample_weight, X, dtype=X.dtype) + if np.any(sample_weight < 0): + raise ValueError("The weights of the samples cannot be negative") X, y, X_offset, y_offset, X_scale = self._preprocess_data( X, From 7370c51f3726e4021e850b1e00efbe1e2a325c4c Mon Sep 17 00:00:00 2001 From: simonandras Date: Wed, 1 Sep 2021 20:03:50 +0200 Subject: [PATCH 02/19] The negativity check is now in _check_sample_weight which can be turned on with the only_positive bool parameter (beckward compatible). For the check the alredy written check_non_negative is used. --- sklearn/linear_model/_base.py | 6 ++---- sklearn/utils/validation.py | 5 ++++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/sklearn/linear_model/_base.py b/sklearn/linear_model/_base.py index 4c9651db2d3bf..5e707d5e9eba4 100644 --- a/sklearn/linear_model/_base.py +++ b/sklearn/linear_model/_base.py @@ -662,10 +662,8 @@ def fit(self, X, y, sample_weight=None): X, y, accept_sparse=accept_sparse, y_numeric=True, multi_output=True ) - if sample_weight is not None: - sample_weight = _check_sample_weight(sample_weight, X, dtype=X.dtype) - if np.any(sample_weight < 0): - raise ValueError("The weights of the samples cannot be negative") + if sample_weight is not None: # the weights should be non-negative + sample_weight = _check_sample_weight(sample_weight, X, only_positive=True, dtype=X.dtype) X, y, X_offset, y_offset, X_scale = self._preprocess_data( X, diff --git a/sklearn/utils/validation.py b/sklearn/utils/validation.py index 871f4a191b2a9..104458c6d39a7 100644 --- a/sklearn/utils/validation.py +++ b/sklearn/utils/validation.py @@ -1479,7 +1479,7 @@ def _check_psd_eigenvalues(lambdas, enable_warnings=False): return lambdas -def _check_sample_weight(sample_weight, X, dtype=None, copy=False): +def _check_sample_weight(sample_weight, X, only_positive=False, dtype=None, copy=False): """Validate sample weights. Note that passing sample_weight=None will output an array of ones. @@ -1540,6 +1540,9 @@ def _check_sample_weight(sample_weight, X, dtype=None, copy=False): ) ) + if only_positive: + check_non_negative(sample_weight, "sample weight") + return sample_weight From 8c07156de3a5b87c5bd8379287106ae725d3c46a Mon Sep 17 00:00:00 2001 From: simonandras Date: Wed, 1 Sep 2021 20:25:50 +0200 Subject: [PATCH 03/19] Adding parameter documentation to the changed _check_sample_weight and rename the only_positive parameter to only_non_negative, because zeros are accepted. --- sklearn/linear_model/_base.py | 2 +- sklearn/utils/validation.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/sklearn/linear_model/_base.py b/sklearn/linear_model/_base.py index 5e707d5e9eba4..93fd2cb3e617c 100644 --- a/sklearn/linear_model/_base.py +++ b/sklearn/linear_model/_base.py @@ -663,7 +663,7 @@ def fit(self, X, y, sample_weight=None): ) if sample_weight is not None: # the weights should be non-negative - sample_weight = _check_sample_weight(sample_weight, X, only_positive=True, dtype=X.dtype) + sample_weight = _check_sample_weight(sample_weight, X, only_non_negative=True, dtype=X.dtype) X, y, X_offset, y_offset, X_scale = self._preprocess_data( X, diff --git a/sklearn/utils/validation.py b/sklearn/utils/validation.py index 104458c6d39a7..5f1b8b407631a 100644 --- a/sklearn/utils/validation.py +++ b/sklearn/utils/validation.py @@ -1479,7 +1479,7 @@ def _check_psd_eigenvalues(lambdas, enable_warnings=False): return lambdas -def _check_sample_weight(sample_weight, X, only_positive=False, dtype=None, copy=False): +def _check_sample_weight(sample_weight, X, only_non_negative=False, dtype=None, copy=False): """Validate sample weights. Note that passing sample_weight=None will output an array of ones. @@ -1494,6 +1494,8 @@ def _check_sample_weight(sample_weight, X, only_positive=False, dtype=None, copy X : {ndarray, list, sparse matrix} Input data. + + only_non_negative : if True then a non negativity check for the sample_weight will be done dtype : dtype, default=None dtype of the validated `sample_weight`. @@ -1540,7 +1542,7 @@ def _check_sample_weight(sample_weight, X, only_positive=False, dtype=None, copy ) ) - if only_positive: + if only_non_negative: check_non_negative(sample_weight, "sample weight") return sample_weight From f0583d96ead24fa6d305f26fec20fe9de196ce8b Mon Sep 17 00:00:00 2001 From: simonandras Date: Thu, 2 Sep 2021 02:00:33 +0200 Subject: [PATCH 04/19] Making some lines shorter than 88 char to pass tests --- sklearn/linear_model/_base.py | 4 +++- sklearn/utils/validation.py | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/sklearn/linear_model/_base.py b/sklearn/linear_model/_base.py index 93fd2cb3e617c..6e1d6acb0cc91 100644 --- a/sklearn/linear_model/_base.py +++ b/sklearn/linear_model/_base.py @@ -663,7 +663,9 @@ def fit(self, X, y, sample_weight=None): ) if sample_weight is not None: # the weights should be non-negative - sample_weight = _check_sample_weight(sample_weight, X, only_non_negative=True, dtype=X.dtype) + sample_weight = _check_sample_weight( + sample_weight, X, only_non_negative=True, dtype=X.dtype + ) X, y, X_offset, y_offset, X_scale = self._preprocess_data( X, diff --git a/sklearn/utils/validation.py b/sklearn/utils/validation.py index 5f1b8b407631a..3d912e2fb24ce 100644 --- a/sklearn/utils/validation.py +++ b/sklearn/utils/validation.py @@ -1479,7 +1479,8 @@ def _check_psd_eigenvalues(lambdas, enable_warnings=False): return lambdas -def _check_sample_weight(sample_weight, X, only_non_negative=False, dtype=None, copy=False): +def _check_sample_weight(sample_weight, X, only_non_negative=False, dtype=None, + copy=False): """Validate sample weights. Note that passing sample_weight=None will output an array of ones. From b3216f895c856ab9e9fa74301764f0ca59a8c16d Mon Sep 17 00:00:00 2001 From: simonandras Date: Thu, 2 Sep 2021 02:07:18 +0200 Subject: [PATCH 05/19] Make one line documentation in two lines to pass test (it was too long) --- sklearn/utils/validation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sklearn/utils/validation.py b/sklearn/utils/validation.py index 3d912e2fb24ce..6984722ceacaa 100644 --- a/sklearn/utils/validation.py +++ b/sklearn/utils/validation.py @@ -1496,7 +1496,8 @@ def _check_sample_weight(sample_weight, X, only_non_negative=False, dtype=None, X : {ndarray, list, sparse matrix} Input data. - only_non_negative : if True then a non negativity check for the sample_weight will be done + only_non_negative : if True then a non negativity check for the sample_weight + will be done dtype : dtype, default=None dtype of the validated `sample_weight`. From bc914dc72611efacdea21969d99ecbb09b49a33f Mon Sep 17 00:00:00 2001 From: simonandras Date: Thu, 2 Sep 2021 02:11:43 +0200 Subject: [PATCH 06/19] Delete whitespace in blank line --- sklearn/utils/validation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn/utils/validation.py b/sklearn/utils/validation.py index 6984722ceacaa..71a29d036faf2 100644 --- a/sklearn/utils/validation.py +++ b/sklearn/utils/validation.py @@ -1495,7 +1495,7 @@ def _check_sample_weight(sample_weight, X, only_non_negative=False, dtype=None, X : {ndarray, list, sparse matrix} Input data. - + only_non_negative : if True then a non negativity check for the sample_weight will be done From 1cbdcb4fca819484f74e5d6e7f1f163354eccb44 Mon Sep 17 00:00:00 2001 From: simonandras Date: Thu, 2 Sep 2021 02:15:45 +0200 Subject: [PATCH 07/19] Add a dot to end of sentence (in modified function documentation) --- sklearn/utils/validation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn/utils/validation.py b/sklearn/utils/validation.py index 71a29d036faf2..75d5ca0b9f9ab 100644 --- a/sklearn/utils/validation.py +++ b/sklearn/utils/validation.py @@ -1497,7 +1497,7 @@ def _check_sample_weight(sample_weight, X, only_non_negative=False, dtype=None, Input data. only_non_negative : if True then a non negativity check for the sample_weight - will be done + will be done. dtype : dtype, default=None dtype of the validated `sample_weight`. From a0d6a70c90416b63cd2ec65823558aaa23a964df Mon Sep 17 00:00:00 2001 From: simonandras Date: Thu, 2 Sep 2021 02:23:01 +0200 Subject: [PATCH 08/19] formatting with black . --- sklearn/utils/validation.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sklearn/utils/validation.py b/sklearn/utils/validation.py index 75d5ca0b9f9ab..554f821539dad 100644 --- a/sklearn/utils/validation.py +++ b/sklearn/utils/validation.py @@ -1479,8 +1479,9 @@ def _check_psd_eigenvalues(lambdas, enable_warnings=False): return lambdas -def _check_sample_weight(sample_weight, X, only_non_negative=False, dtype=None, - copy=False): +def _check_sample_weight( + sample_weight, X, only_non_negative=False, dtype=None, copy=False +): """Validate sample weights. Note that passing sample_weight=None will output an array of ones. From 622a1c7b05699feec65f78d6a040d0a58301ca3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20Simon?= <36902722+simonandras@users.noreply.github.com> Date: Thu, 2 Sep 2021 20:19:46 +0200 Subject: [PATCH 09/19] Update sklearn/utils/validation.py update documentation Co-authored-by: Guillaume Lemaitre --- sklearn/utils/validation.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sklearn/utils/validation.py b/sklearn/utils/validation.py index 554f821539dad..d241693d43b54 100644 --- a/sklearn/utils/validation.py +++ b/sklearn/utils/validation.py @@ -1497,8 +1497,10 @@ def _check_sample_weight( X : {ndarray, list, sparse matrix} Input data. - only_non_negative : if True then a non negativity check for the sample_weight - will be done. + only_non_negative : bool, default=False, + Whether or not the weights are expected to be non-negative. + + .. versionadded:: 1.0 dtype : dtype, default=None dtype of the validated `sample_weight`. From 72c05f98cb63a99e873fec65e5999a6fac9455f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20Simon?= <36902722+simonandras@users.noreply.github.com> Date: Thu, 2 Sep 2021 20:24:27 +0200 Subject: [PATCH 10/19] Update sklearn/linear_model/_base.py delete unnecessary comment Co-authored-by: Guillaume Lemaitre --- sklearn/linear_model/_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn/linear_model/_base.py b/sklearn/linear_model/_base.py index 6e1d6acb0cc91..514525a14a012 100644 --- a/sklearn/linear_model/_base.py +++ b/sklearn/linear_model/_base.py @@ -662,7 +662,7 @@ def fit(self, X, y, sample_weight=None): X, y, accept_sparse=accept_sparse, y_numeric=True, multi_output=True ) - if sample_weight is not None: # the weights should be non-negative + if sample_weight is not None: sample_weight = _check_sample_weight( sample_weight, X, only_non_negative=True, dtype=X.dtype ) From fb01e063d1e52f426dde3af5ca266ba24c8605c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20Simon?= <36902722+simonandras@users.noreply.github.com> Date: Thu, 2 Sep 2021 20:25:30 +0200 Subject: [PATCH 11/19] Update sklearn/utils/validation.py Add `...` to function name in the error message Co-authored-by: Guillaume Lemaitre --- sklearn/utils/validation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn/utils/validation.py b/sklearn/utils/validation.py index d241693d43b54..d8e9c87df4414 100644 --- a/sklearn/utils/validation.py +++ b/sklearn/utils/validation.py @@ -1548,7 +1548,7 @@ def _check_sample_weight( ) if only_non_negative: - check_non_negative(sample_weight, "sample weight") + check_non_negative(sample_weight, "`sample_weight`") return sample_weight From da6c6fdce27d12aff5b56e19821efe9fcc5485c3 Mon Sep 17 00:00:00 2001 From: simonandras Date: Thu, 2 Sep 2021 20:52:03 +0200 Subject: [PATCH 12/19] Solve the previous problems --- sklearn/ensemble/_weight_boosting.py | 8 ++++---- sklearn/ensemble/tests/test_weight_boosting.py | 2 +- sklearn/neighbors/_kde.py | 4 +--- sklearn/neighbors/tests/test_kde.py | 2 +- sklearn/utils/validation.py | 8 ++++++-- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/sklearn/ensemble/_weight_boosting.py b/sklearn/ensemble/_weight_boosting.py index 77ef449ba1933..a47937880d91c 100644 --- a/sklearn/ensemble/_weight_boosting.py +++ b/sklearn/ensemble/_weight_boosting.py @@ -123,10 +123,10 @@ def fit(self, X, y, sample_weight=None): y_numeric=is_regressor(self), ) - sample_weight = _check_sample_weight(sample_weight, X, np.float64, copy=True) + sample_weight = _check_sample_weight( + sample_weight, X, np.float64, copy=True, only_non_negative=True + ) sample_weight /= sample_weight.sum() - if np.any(sample_weight < 0): - raise ValueError("sample_weight cannot contain negative weights") # Check parameters self._validate_estimator() @@ -136,7 +136,7 @@ def fit(self, X, y, sample_weight=None): self.estimator_weights_ = np.zeros(self.n_estimators, dtype=np.float64) self.estimator_errors_ = np.ones(self.n_estimators, dtype=np.float64) - # Initializion of the random number instance that will be used to + # Initialization of the random number instance that will be used to # generate a seed at each iteration random_state = check_random_state(self.random_state) diff --git a/sklearn/ensemble/tests/test_weight_boosting.py b/sklearn/ensemble/tests/test_weight_boosting.py index 6927d47c11cfe..159f83abf24c4 100755 --- a/sklearn/ensemble/tests/test_weight_boosting.py +++ b/sklearn/ensemble/tests/test_weight_boosting.py @@ -576,6 +576,6 @@ def test_adaboost_negative_weight_error(model, X, y): sample_weight = np.ones_like(y) sample_weight[-1] = -10 - err_msg = "sample_weight cannot contain negative weight" + err_msg = "Negative values in data passed to `sample_weight`" with pytest.raises(ValueError, match=err_msg): model.fit(X, y, sample_weight=sample_weight) diff --git a/sklearn/neighbors/_kde.py b/sklearn/neighbors/_kde.py index 328a13371bafd..dcfc7b51f421a 100644 --- a/sklearn/neighbors/_kde.py +++ b/sklearn/neighbors/_kde.py @@ -191,9 +191,7 @@ def fit(self, X, y=None, sample_weight=None): X = self._validate_data(X, order="C", dtype=DTYPE) if sample_weight is not None: - sample_weight = _check_sample_weight(sample_weight, X, DTYPE) - if sample_weight.min() <= 0: - raise ValueError("sample_weight must have positive values") + sample_weight = _check_sample_weight(sample_weight, X, DTYPE, only_non_negative=True) kwargs = self.metric_params if kwargs is None: diff --git a/sklearn/neighbors/tests/test_kde.py b/sklearn/neighbors/tests/test_kde.py index 84f7623c8dbf1..d4fb775c44826 100644 --- a/sklearn/neighbors/tests/test_kde.py +++ b/sklearn/neighbors/tests/test_kde.py @@ -209,7 +209,7 @@ def test_sample_weight_invalid(): data = np.reshape([1.0, 2.0, 3.0], (-1, 1)) sample_weight = [0.1, -0.2, 0.3] - expected_err = "sample_weight must have positive values" + expected_err = "Negative values in data passed to `sample_weight`" with pytest.raises(ValueError, match=expected_err): kde.fit(data, sample_weight=sample_weight) diff --git a/sklearn/utils/validation.py b/sklearn/utils/validation.py index d8e9c87df4414..fbe58a3997d4b 100644 --- a/sklearn/utils/validation.py +++ b/sklearn/utils/validation.py @@ -1480,7 +1480,11 @@ def _check_psd_eigenvalues(lambdas, enable_warnings=False): def _check_sample_weight( - sample_weight, X, only_non_negative=False, dtype=None, copy=False + sample_weight, + X, + dtype=None, + copy=False, + only_non_negative=False ): """Validate sample weights. @@ -1499,7 +1503,7 @@ def _check_sample_weight( only_non_negative : bool, default=False, Whether or not the weights are expected to be non-negative. - + .. versionadded:: 1.0 dtype : dtype, default=None From a7d5f2d4feb7883aee2460e85140c07d25977fc2 Mon Sep 17 00:00:00 2001 From: simonandras Date: Thu, 2 Sep 2021 20:55:43 +0200 Subject: [PATCH 13/19] reformatting with black . --- sklearn/neighbors/_kde.py | 4 +++- sklearn/utils/validation.py | 6 +----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/sklearn/neighbors/_kde.py b/sklearn/neighbors/_kde.py index dcfc7b51f421a..0ac0ea7226b90 100644 --- a/sklearn/neighbors/_kde.py +++ b/sklearn/neighbors/_kde.py @@ -191,7 +191,9 @@ def fit(self, X, y=None, sample_weight=None): X = self._validate_data(X, order="C", dtype=DTYPE) if sample_weight is not None: - sample_weight = _check_sample_weight(sample_weight, X, DTYPE, only_non_negative=True) + sample_weight = _check_sample_weight( + sample_weight, X, DTYPE, only_non_negative=True + ) kwargs = self.metric_params if kwargs is None: diff --git a/sklearn/utils/validation.py b/sklearn/utils/validation.py index fbe58a3997d4b..4c75695419e17 100644 --- a/sklearn/utils/validation.py +++ b/sklearn/utils/validation.py @@ -1480,11 +1480,7 @@ def _check_psd_eigenvalues(lambdas, enable_warnings=False): def _check_sample_weight( - sample_weight, - X, - dtype=None, - copy=False, - only_non_negative=False + sample_weight, X, dtype=None, copy=False, only_non_negative=False ): """Validate sample weights. From b8741dbf1a71c1be8ce2f84fcc285573ebc28614 Mon Sep 17 00:00:00 2001 From: simonandras Date: Thu, 2 Sep 2021 21:16:07 +0200 Subject: [PATCH 14/19] In the documentation of _check_sample_weight changing the 3 space indents to 4 space indents --- sklearn/utils/validation.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/sklearn/utils/validation.py b/sklearn/utils/validation.py index 4c75695419e17..ab2faa373a034 100644 --- a/sklearn/utils/validation.py +++ b/sklearn/utils/validation.py @@ -1492,7 +1492,7 @@ def _check_sample_weight( Parameters ---------- sample_weight : {ndarray, Number or None}, shape (n_samples,) - Input sample weights. + Input sample weights. X : {ndarray, list, sparse matrix} Input data. @@ -1503,11 +1503,11 @@ def _check_sample_weight( .. versionadded:: 1.0 dtype : dtype, default=None - dtype of the validated `sample_weight`. - If None, and the input `sample_weight` is an array, the dtype of the - input is preserved; otherwise an array with the default numpy dtype - is be allocated. If `dtype` is not one of `float32`, `float64`, - `None`, the output will be of dtype `float64`. + dtype of the validated `sample_weight`. + If None, and the input `sample_weight` is an array, the dtype of the + input is preserved; otherwise an array with the default numpy dtype + is be allocated. If `dtype` is not one of `float32`, `float64`, + `None`, the output will be of dtype `float64`. copy : bool, default=False If True, a copy of sample_weight will be created. @@ -1515,7 +1515,7 @@ def _check_sample_weight( Returns ------- sample_weight : ndarray of shape (n_samples,) - Validated sample weight. It is guaranteed to be "C" contiguous. + Validated sample weight. It is guaranteed to be "C" contiguous. """ n_samples = _num_samples(X) From ed20f5a186de33485282fc903476267e28730ac9 Mon Sep 17 00:00:00 2001 From: simonandras Date: Thu, 2 Sep 2021 23:37:16 +0200 Subject: [PATCH 15/19] Adding test to test_validation.py to test the negative weight error in the _check_sample_weigth function --- sklearn/utils/tests/test_validation.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/sklearn/utils/tests/test_validation.py b/sklearn/utils/tests/test_validation.py index 45c24d696cd40..6ce2696920028 100644 --- a/sklearn/utils/tests/test_validation.py +++ b/sklearn/utils/tests/test_validation.py @@ -51,8 +51,8 @@ _num_features, FLOAT_DTYPES, _get_feature_names, + _check_fit_params ) -from sklearn.utils.validation import _check_fit_params from sklearn.base import BaseEstimator import sklearn @@ -1240,6 +1240,14 @@ def test_check_sample_weight(): sample_weight = _check_sample_weight(None, X, dtype=X.dtype) assert sample_weight.dtype == np.float64 + # check negative weight when only_non_negative=True + X = np.ones((5, 2)) + sample_weight = np.ones(_num_samples(X)) + sample_weight[-1] = -10 + err_msg = "Negative values in data passed to `sample_weight`" + with pytest.raises(ValueError, match=err_msg): + _check_sample_weight(sample_weight, X, only_non_negative=True) + @pytest.mark.parametrize("toarray", [np.array, sp.csr_matrix, sp.csc_matrix]) def test_allclose_dense_sparse_equals(toarray): From 9c24e51445fd96da78e46d3b9a67a11357ea0c5f Mon Sep 17 00:00:00 2001 From: simonandras Date: Thu, 2 Sep 2021 23:42:40 +0200 Subject: [PATCH 16/19] black . --- sklearn/utils/tests/test_validation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn/utils/tests/test_validation.py b/sklearn/utils/tests/test_validation.py index 6ce2696920028..69a102f58acd7 100644 --- a/sklearn/utils/tests/test_validation.py +++ b/sklearn/utils/tests/test_validation.py @@ -51,7 +51,7 @@ _num_features, FLOAT_DTYPES, _get_feature_names, - _check_fit_params + _check_fit_params, ) from sklearn.base import BaseEstimator import sklearn From 3acb9b36e805488267d91936d6cdf91baf46db91 Mon Sep 17 00:00:00 2001 From: simonandras Date: Fri, 3 Sep 2021 00:35:08 +0200 Subject: [PATCH 17/19] In LinearRegression in fit method in _check_sample_weight function call make the parameter order like in the function definition --- sklearn/linear_model/_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn/linear_model/_base.py b/sklearn/linear_model/_base.py index 514525a14a012..8b5102ecdd403 100644 --- a/sklearn/linear_model/_base.py +++ b/sklearn/linear_model/_base.py @@ -664,7 +664,7 @@ def fit(self, X, y, sample_weight=None): if sample_weight is not None: sample_weight = _check_sample_weight( - sample_weight, X, only_non_negative=True, dtype=X.dtype + sample_weight, X, dtype=X.dtype, only_non_negative=True ) X, y, X_offset, y_offset, X_scale = self._preprocess_data( From 95ac8bfe5b67098b90fe5990247f8e522195f2a6 Mon Sep 17 00:00:00 2001 From: simonandras Date: Fri, 3 Sep 2021 13:47:46 +0200 Subject: [PATCH 18/19] Added entry to the changelog (sklearn.utils). Also corrected 2 typo in the changelog. --- doc/whats_new/v1.0.rst | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/doc/whats_new/v1.0.rst b/doc/whats_new/v1.0.rst index 79f68c80df0ec..b2002d75728a0 100644 --- a/doc/whats_new/v1.0.rst +++ b/doc/whats_new/v1.0.rst @@ -821,7 +821,7 @@ Changelog unavailable on the basis of state, in a more readable way. :pr:`19948` by `Joel Nothman`_. -_ |Enhancement| :func:`utils.validation.check_is_fitted` now uses +- |Enhancement| :func:`utils.validation.check_is_fitted` now uses ``__sklearn_is_fitted__`` if available, instead of checking for attributes ending with an underscore. This also makes :class:`Pipeline` and :class:`preprocessing.FunctionTransformer` pass @@ -856,7 +856,7 @@ _ |Enhancement| :func:`utils.validation.check_is_fitted` now uses warning was previously raised in resampling utilities and functions using those utilities (e.g. :func:`model_selection.train_test_split`, :func:`model_selection.cross_validate`, - :func:`model_seleection.cross_val_score`, + :func:`model_selection.cross_val_score`, :func:`model_selection.cross_val_predict`). :pr:`20673` by :user:`Joris Van den Bossche `. @@ -865,6 +865,18 @@ _ |Enhancement| :func:`utils.validation.check_is_fitted` now uses `np.int64`). :pr:`20727` by `Guillaume Lemaitre`_. +- |Enhancement| :func:`utils.validation._check_sample_weight` can perform a + non-negativity check on the sample weights. It can be turned on + using the only_non_negative bool parameter. + Estimators that check for non-negative weights are updated: + :func:`linear_model.LinearRegression` (here the previous + error message was misleading), + :func:`ensemble.AdaBoostClassifier`, + :func:`ensemble.AdaBoostRegressor`, + :func:`neighbors.KernelDensity`. + :pr:`20880` by :user:`Guillaume Lemaitre ` + and :user:`András Simon `. + :mod:`sklearn.validation` ......................... From 6864e9bfbbc5daaaf43dd23e137c2d3beac54486 Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Wed, 8 Sep 2021 11:14:22 +0200 Subject: [PATCH 19/19] move whats new version --- doc/whats_new/v1.0.rst | 14 +------------- doc/whats_new/v1.1.rst | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/doc/whats_new/v1.0.rst b/doc/whats_new/v1.0.rst index 38407888dbc47..e15c8ec5ff832 100644 --- a/doc/whats_new/v1.0.rst +++ b/doc/whats_new/v1.0.rst @@ -901,7 +901,7 @@ Changelog warning was previously raised in resampling utilities and functions using those utilities (e.g. :func:`model_selection.train_test_split`, :func:`model_selection.cross_validate`, - :func:`model_selection.cross_val_score`, + :func:`model_seleection.cross_val_score`, :func:`model_selection.cross_val_predict`). :pr:`20673` by :user:`Joris Van den Bossche `. @@ -910,18 +910,6 @@ Changelog `np.int64`). :pr:`20727` by `Guillaume Lemaitre`_. -- |Enhancement| :func:`utils.validation._check_sample_weight` can perform a - non-negativity check on the sample weights. It can be turned on - using the only_non_negative bool parameter. - Estimators that check for non-negative weights are updated: - :func:`linear_model.LinearRegression` (here the previous - error message was misleading), - :func:`ensemble.AdaBoostClassifier`, - :func:`ensemble.AdaBoostRegressor`, - :func:`neighbors.KernelDensity`. - :pr:`20880` by :user:`Guillaume Lemaitre ` - and :user:`András Simon `. - - |API| :func:`utils._testing.assert_warns` and :func:`utils._testing.assert_warns_message` are deprecated in 1.0 and will be removed in 1.2. Used `pytest.warns` context manager instead. Note that diff --git a/doc/whats_new/v1.1.rst b/doc/whats_new/v1.1.rst index 1639b4b691c65..fba40e25a9e7e 100644 --- a/doc/whats_new/v1.1.rst +++ b/doc/whats_new/v1.1.rst @@ -38,9 +38,20 @@ Changelog :pr:`123456` by :user:`Joe Bloggs `. where 123456 is the *pull request* number, not the issue number. - -:mod:`sklearn.decomposition` -............................ +:mod:`sklearn.utils` +.................... + +- |Enhancement| :func:`utils.validation._check_sample_weight` can perform a + non-negativity check on the sample weights. It can be turned on + using the only_non_negative bool parameter. + Estimators that check for non-negative weights are updated: + :func:`linear_model.LinearRegression` (here the previous + error message was misleading), + :func:`ensemble.AdaBoostClassifier`, + :func:`ensemble.AdaBoostRegressor`, + :func:`neighbors.KernelDensity`. + :pr:`20880` by :user:`Guillaume Lemaitre ` + and :user:`András Simon `. Code and Documentation Contributors