Skip to content

Commit

Permalink
FIX convergence criterion of MeanShift (#28951)
Browse files Browse the repository at this point in the history
Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
Co-authored-by: Jérémie du Boisberranger <jeremie@probabl.ai>
  • Loading branch information
3 people committed May 17, 2024
1 parent 87ceec2 commit e796d0a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/whats_new/v1.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ Changelog
:mod:`sklearn.cluster`
......................

- |Fix| The :class:`cluster.MeanShift` class now properly converges for constant data.
:pr:`28951` by :user:`Akihiro Kuno <akikuno>`.

- |FIX| Create copy of precomputed sparse matrix within the `fit` method of
:class:`~cluster.OPTICS` to avoid in-place modification of the sparse matrix.
:pr:`28491` by :user:`Thanh Lam Dang <lamdang2k>`.
Expand Down
2 changes: 1 addition & 1 deletion sklearn/cluster/_mean_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def _mean_shift_single_seed(my_mean, X, nbrs, max_iter):
my_mean = np.mean(points_within, axis=0)
# If converged or at max_iter, adds the cluster
if (
np.linalg.norm(my_mean - my_old_mean) < stop_thresh
np.linalg.norm(my_mean - my_old_mean) <= stop_thresh
or completed_iterations == max_iter
):
break
Expand Down
9 changes: 9 additions & 0 deletions sklearn/cluster/tests/test_mean_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@
)


def test_convergence_of_1d_constant_data():
# Test convergence using 1D constant data
# Non-regression test for:
# https://github.com/scikit-learn/scikit-learn/issues/28926
model = MeanShift()
n_iter = model.fit(np.ones(10).reshape(-1, 1)).n_iter_
assert n_iter < model.max_iter


def test_estimate_bandwidth():
# Test estimate_bandwidth
bandwidth = estimate_bandwidth(X, n_samples=200)
Expand Down

0 comments on commit e796d0a

Please sign in to comment.