Skip to content

Loosened to dist <= stop_thresh to converge in on 1D constant data #28951

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 12 commits into from
May 17, 2024
Merged
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
Loading