Skip to content

Commit

Permalink
Merge 6700791 into 515400d
Browse files Browse the repository at this point in the history
  • Loading branch information
gclen committed Mar 18, 2020
2 parents 515400d + 6700791 commit e9e8420
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions hdbscan/hdbscan_.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,10 @@ def weighted_cluster_centroid(self, cluster_id):
if not hasattr(self, 'labels_'):
raise AttributeError('Model has not been fit to data')

if cluster_id == -1:
raise ValueError('Cannot calculate weighted centroid for -1 cluster '
'since it is a noise cluster')

mask = self.labels_ == cluster_id
cluster_data = self._raw_data[mask]
cluster_membership_strengths = self.probabilities_[mask]
Expand All @@ -1015,6 +1019,10 @@ def weighted_cluster_medoid(self, cluster_id):
if not hasattr(self, 'labels_'):
raise AttributeError('Model has not been fit to data')

if cluster_id == -1:
raise ValueError('Cannot calculate weighted centroid for -1 cluster '
'since it is a noise cluster')

mask = self.labels_ == cluster_id
cluster_data = self._raw_data[mask]
cluster_membership_strengths = self.probabilities_[mask]
Expand Down
7 changes: 7 additions & 0 deletions hdbscan/tests/test_hdbscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,13 @@ def test_hdbscan_centroids_medoids():
medoid = clusterer.weighted_cluster_medoid(idx)
assert_array_almost_equal(medoid, center, decimal=1)


def test_hdbscan_no_centroid_medoid_for_noise():
clusterer = HDBSCAN().fit(X)
assert_raises(ValueError, clusterer.weighted_cluster_centroid, -1)
assert_raises(ValueError, clusterer.weighted_cluster_medoid, -1)


# Disable for now -- need to refactor to meet newer standards
@SkipTest
def test_hdbscan_is_sklearn_estimator():
Expand Down

0 comments on commit e9e8420

Please sign in to comment.