Skip to content

Commit bd55522

Browse files
glevvjjerphan
andauthored
FIX deprecate sample_weight in predict method of BaseKMeans (scikit-learn#25251)
Co-authored-by: Julien Jerphanion <git@jjerphan.xyz> Fixes scikit-learn#25066
1 parent 8679db4 commit bd55522

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎doc/whats_new/v1.3.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ Changelog
5252
:pr:`123456` by :user:`Joe Bloggs <joeongithub>`.
5353
where 123456 is the *pull request* number, not the issue number.
5454
55+
:mod:`sklearn.cluster`
56+
......................
57+
58+
- |API| The `sample_weight` parameter in `predict` for
59+
:meth:`cluster.KMeans.predict` and :meth:`cluster.MiniBatchKMeans.predict`
60+
is now deprecated and will be removed in v1.5.
61+
:pr:`25251` by :user:`Gleb Levitski <glevv>`.
62+
5563
:mod:`sklearn.decomposition`
5664
............................
5765
- |Enhancement| :class:`decomposition.DictionaryLearning` now accepts the parameter

‎sklearn/cluster/_kmeans.py‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@ def fit_predict(self, X, y=None, sample_weight=None):
10321032
"""
10331033
return self.fit(X, sample_weight=sample_weight).labels_
10341034

1035-
def predict(self, X, sample_weight=None):
1035+
def predict(self, X, sample_weight="deprecated"):
10361036
"""Predict the closest cluster each sample in X belongs to.
10371037
10381038
In the vector quantization literature, `cluster_centers_` is called
@@ -1048,6 +1048,10 @@ def predict(self, X, sample_weight=None):
10481048
The weights for each observation in X. If None, all observations
10491049
are assigned equal weight.
10501050
1051+
.. deprecated:: 1.3
1052+
The parameter `sample_weight` is deprecated in version 1.3
1053+
and will be removed in 1.5.
1054+
10511055
Returns
10521056
-------
10531057
labels : ndarray of shape (n_samples,)
@@ -1056,7 +1060,15 @@ def predict(self, X, sample_weight=None):
10561060
check_is_fitted(self)
10571061

10581062
X = self._check_test_data(X)
1059-
sample_weight = _check_sample_weight(sample_weight, X, dtype=X.dtype)
1063+
if sample_weight != "deprecated":
1064+
warnings.warn(
1065+
"'sample_weight' was deprecated in version 1.3 and "
1066+
"will be removed in 1.5.",
1067+
FutureWarning,
1068+
)
1069+
sample_weight = _check_sample_weight(sample_weight, X, dtype=X.dtype)
1070+
else:
1071+
sample_weight = _check_sample_weight(None, X, dtype=X.dtype)
10601072

10611073
labels = _labels_inertia_threadpool_limit(
10621074
X,

‎sklearn/cluster/tests/test_k_means.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,19 @@ def test_algorithm_auto_full_deprecation_warning(algorithm):
225225
assert kmeans._algorithm == "lloyd"
226226

227227

228+
@pytest.mark.parametrize("Estimator", [KMeans, MiniBatchKMeans])
229+
def test_predict_sample_weight_deprecation_warning(Estimator):
230+
X = np.random.rand(100, 2)
231+
sample_weight = np.random.uniform(size=100)
232+
kmeans = Estimator()
233+
kmeans.fit(X, sample_weight=sample_weight)
234+
warn_msg = (
235+
"'sample_weight' was deprecated in version 1.3 and will be removed in 1.5."
236+
)
237+
with pytest.warns(FutureWarning, match=warn_msg):
238+
kmeans.predict(X, sample_weight=sample_weight)
239+
240+
228241
def test_minibatch_update_consistency(global_random_seed):
229242
# Check that dense and sparse minibatch update give the same results
230243
rng = np.random.RandomState(global_random_seed)

0 commit comments

Comments
 (0)