Skip to content

Commit

Permalink
Test usage of Density Estimator in Silhouette visualizer
Browse files Browse the repository at this point in the history
  • Loading branch information
lwgray committed Jul 29, 2023
1 parent 9737dba commit 1b4e351
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions tests/test_cluster/test_silhouette.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans, MiniBatchKMeans
from sklearn.cluster import SpectralClustering, AgglomerativeClustering
from sklearn.mixture import GaussianMixture

from unittest import mock
from tests.base import VisualTestCase
Expand Down Expand Up @@ -84,6 +85,27 @@ def test_integrated_mini_batch_kmeans_silhouette(self):

self.assert_images_similar(visualizer, remove_legend=True)

@pytest.mark.xfail(sys.platform == "win32", reason="images not close on windows")
def test_integrated_gaussian_mixture_silhouette(self):
"""
Test Density Estimator works with silhouette visualizer
"""
# NOTE see #182: cannot use occupancy dataset because of memory usage

# Generate a blobs data set
X, y = make_blobs(
n_samples=1000, n_features=12, centers=8, shuffle=False, random_state=0
)

fig = plt.figure()
ax = fig.add_subplot()

visualizer = SilhouetteVisualizer(GaussianMixture(n_components=5, random_state=0), ax=ax)
visualizer.fit(X)
visualizer.finalize()

self.assert_images_similar(visualizer, remove_legend=True)

@pytest.mark.skip(reason="no negative silhouette example available yet")
def test_negative_silhouette_score(self):
"""
Expand Down
6 changes: 3 additions & 3 deletions yellowbrick/cluster/silhouette.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ def fit(self, X, y=None, **kwargs):
# Compute the number of available clusters from the estimator
if hasattr(self.estimator, "n_clusters"):
self.n_clusters_ = self.estimator.n_clusters
elif hasattr(self.estimator, "n_components"):
self.n_clusters_ = self.estimator.n_components
else:
elif hasattr(self.estimator, "n_components"):
self.n_clusters_ = self.estimator.n_components
else:
unique_labels = set(labels)
n_noise_clusters = 1 if -1 in unique_labels else 0
self.n_clusters_ = len(unique_labels) - n_noise_clusters
Expand Down

0 comments on commit 1b4e351

Please sign in to comment.