Skip to content

[MRG] Fix regression in silhouette_score for clusters of size 1 #6089

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions sklearn/metrics/cluster/tests/test_unsupervised.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from sklearn import datasets
from sklearn.metrics.cluster.unsupervised import silhouette_score
from sklearn.metrics.cluster.unsupervised import silhouette_samples
from sklearn.metrics import pairwise_distances
from sklearn.utils.testing import assert_false
from sklearn.utils.testing import assert_almost_equal
Expand Down Expand Up @@ -50,6 +51,8 @@ def test_no_nan():
D = np.random.RandomState(0).rand(len(labels), len(labels))
silhouette = silhouette_score(D, labels, metric='precomputed')
assert_false(np.isnan(silhouette))
ss = silhouette_samples(D, labels, metric='precomputed')
assert_false(np.isnan(ss).any())


def test_correct_labelsize():
Expand Down
4 changes: 3 additions & 1 deletion sklearn/metrics/cluster/unsupervised.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,6 @@ def silhouette_samples(X, labels, metric='euclidean', **kwds):

sil_samples = inter_clust_dists - intra_clust_dists
sil_samples /= np.maximum(intra_clust_dists, inter_clust_dists)
return sil_samples

# nan values are for clusters of size 1, and should be 0
Copy link
Member

Choose a reason for hiding this comment

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

@MechCoder suggested testing it differently right? Could you look into that

(Pulled from the hidden diff)

This test is not a regression test. It will pass in master.
Can you remove this and add a new test testing something like this

distances = np.random.RandomState(0).rand(5)
A = _intra_cluster_distance(distance, np.array([0, 1, 1, 1, 1]), 0)
assert_true(np.isnan(A))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rvraghav93 , would something like this work,

D = np.random.RandomState(0).rand(len(labels), len(labels))
labels = np.array([1, 0, 1, 1, 1])
ss = silhouette_samples(D, labels, metric='precomputed')
assert_false(np.isnan(ss).any())

I need to use any()/all() as we're checking an array here. And, the method that @MechCoder suggested has been deprecated..
Also, sorry about the travis. I made a slight mistake, am fixing it

return np.nan_to_num(sil_samples)