Skip to content

More stable n_init runs for KMeans #20200

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 4 commits into from
Jun 22, 2021
Merged
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
4 changes: 3 additions & 1 deletion sklearn/cluster/_kmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,9 @@ def fit(self, X, y=None, sample_weight=None):
x_squared_norms=x_squared_norms, n_threads=self._n_threads)

# determine if these results are the best so far
if best_inertia is None or inertia < best_inertia:
# allow small tolerance on the inertia to accommodate for
# non-deterministic rounding errors due to parallel computation
if best_inertia is None or inertia < best_inertia * (1 - 1e-6):
Comment on lines +1060 to +1062
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Precising that this is just above the float32 eps as you mentioned might be informative here.

best_labels = labels
best_centers = centers
best_inertia = inertia
Expand Down