Skip to content

Commit

Permalink
Add ability to add DensityEstimator as a Clusterer
Browse files Browse the repository at this point in the history
  • Loading branch information
lwgray committed Jul 29, 2023
1 parent f7a8e95 commit e38d0e7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
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: 1 addition & 1 deletion yellowbrick/cluster/silhouette.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ 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
else:
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
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

0 comments on commit e38d0e7

Please sign in to comment.