Skip to content

MNT Don't normalize sample weights in KMeans#17848

Merged
glemaitre merged 5 commits into
scikit-learn:masterfrom
jeremiedbb:kmeans-sample-weight
Jul 8, 2020
Merged

MNT Don't normalize sample weights in KMeans#17848
glemaitre merged 5 commits into
scikit-learn:masterfrom
jeremiedbb:kmeans-sample-weight

Conversation

@jeremiedbb

Copy link
Copy Markdown
Member

sample_weights are normalized such that their sum = n_samples. Doing this normalization has absolutely no impact on the clustering. Moreover it's currently bugged since we don't invert the normalization to report the inertia (Fixes #16594).

This is extracted from #17622 to facilitate the reviews.

@rth @ogrisel @glemaitre

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is also more consistent with how other clusterers deal with sample weight

@jnothman

jnothman commented Jul 6, 2020

Copy link
Copy Markdown
Member

I wonder if it's worth updating the docstring of fit to indicate that sample_weight is equivalent to replication?

@jeremiedbb

Copy link
Copy Markdown
Member Author

I wonder if it's worth updating the docstring of fit to indicate that sample_weight is equivalent to replication?

@jnothman, well it's not in many situations. In KMeans with the k-means++ or random init. In MinibatchKMeans no matter the init. Because sampling X will be different with duplicated points. So I'm not sure it's worth to advertise that when it only works in rare situations

@jnothman

jnothman commented Jul 6, 2020 via email

Copy link
Copy Markdown
Member

Comment thread doc/whats_new/v0.24.rst
:mod:`sklearn.cluster`
.........................

- |Fix| Fixed a bug in :class:`cluster.KMeans` and

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since the inertia will change, we should add this change in the Changed Model section.

@glemaitre glemaitre left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@glemaitre glemaitre merged commit 7514a05 into scikit-learn:master Jul 8, 2020
@glemaitre

Copy link
Copy Markdown
Member

Thanks @jeremiedbb

glemaitre pushed a commit to glemaitre/scikit-learn that referenced this pull request Aug 3, 2020
`sample_weight` should not be normalized in KMeans. The weight magnitude
should have an influence on the `inertia_`, larger the weights, larger should be
the inertia.
jayzed82 pushed a commit to jayzed82/scikit-learn that referenced this pull request Oct 22, 2020
`sample_weight` should not be normalized in KMeans. The weight magnitude
should have an influence on the `inertia_`, larger the weights, larger should be
the inertia.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Documentation is wrong about KMeans.inertia_

3 participants