Skip to content
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

WIP: Allows Silhouette Visualizer to accept DensityEstimator #1304

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 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,29 @@ 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
4 changes: 2 additions & 2 deletions yellowbrick/cluster/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
## Imports
##########################################################################

from yellowbrick.utils import isclusterer
from yellowbrick.utils import isclusterer, isdensity
from yellowbrick.base import ScoreVisualizer
from yellowbrick.exceptions import YellowbrickTypeError

Expand All @@ -41,7 +41,7 @@ class ClusteringScoreVisualizer(ScoreVisualizer):
"""

def __init__(self, estimator, ax=None, fig=None, force_model=False, **kwargs):
if not force_model and not isclusterer(estimator):
if not force_model and not isclusterer(estimator) and not isdensity(estimator):
raise YellowbrickTypeError(
"The supplied model is not a clustering estimator; try a "
"classifier or regression score visualizer instead!"
Expand Down
2 changes: 2 additions & 0 deletions yellowbrick/cluster/silhouette.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ 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:
unique_labels = set(labels)
n_noise_clusters = 1 if -1 in unique_labels else 0
Expand Down
19 changes: 19 additions & 0 deletions yellowbrick/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,25 @@ def is_clusterer(estimator):
isclusterer = is_clusterer


def is_density(estimator):
"""
Returns True if the given estimator is a Density Estimator.

Parameters
----------
estimator : class or instance
The object to test if it is a Scikit-Learn clusterer, especially a
Scikit-Learn estimator or Yellowbrick visualizer
"""

# Test the _estimator_type property
return getattr(estimator, "_estimator_type", None) == "DensityEstimator"


# Alias for closer name to isinstance and issubclass
isdensity = is_density


def is_gridsearch(estimator):
"""
Returns True if the given estimator is a clusterer.
Expand Down