Skip to content

Commit

Permalink
Silhouette Score make_monotonic for non-monotonic label set (#3619)
Browse files Browse the repository at this point in the history
closes #3584

Authors:
  - Divye Gala (@divyegala)

Approvers:
  - Dante Gama Dessavre (@dantegd)

URL: #3619
  • Loading branch information
divyegala authored Mar 16, 2021
1 parent 452f92d commit 96eaf62
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
15 changes: 13 additions & 2 deletions python/cuml/metrics/cluster/silhouette_score.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ from cuml.metrics.pairwise_distances import _determine_metric
from cuml.raft.common.handle cimport handle_t
from cuml.raft.common.handle import Handle
from cuml.metrics.distance_type cimport DistanceType
from cuml.prims.label.classlabels import make_monotonic, check_labels

cdef extern from "cuml/metrics/metrics.hpp" namespace "ML::Metrics::Batched":
float silhouette_score(
Expand Down Expand Up @@ -105,6 +106,16 @@ def _silhouette_coeff(
labels.to_output(output_type='cupy', output_dtype='int')
).shape[0]

if not check_labels(labels, cp.arange(n_labels, dtype=np.int32)):
mono_labels, _ = make_monotonic(labels, copy=True)
mono_labels, _, _, _ = input_to_cuml_array(
mono_labels,
order='C',
convert_to_dtype=np.int32
)
else:
mono_labels = labels

cdef uintptr_t scores_ptr
if sil_scores is None:
scores_ptr = <uintptr_t> NULL
Expand All @@ -122,7 +133,7 @@ def _silhouette_coeff(
<float*> <uintptr_t> data.ptr,
<int> n_rows,
<int> n_cols,
<int*> <uintptr_t> labels.ptr,
<int*> <uintptr_t> mono_labels.ptr,
<int> n_labels,
<float*> scores_ptr,
<int> chunksize,
Expand All @@ -132,7 +143,7 @@ def _silhouette_coeff(
<double*> <uintptr_t> data.ptr,
<int> n_rows,
<int> n_cols,
<int*> <uintptr_t> labels.ptr,
<int*> <uintptr_t> mono_labels.ptr,
<int> n_labels,
<double*> scores_ptr,
<int> chunksize,
Expand Down
17 changes: 17 additions & 0 deletions python/cuml/test/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,23 @@ def test_silhouette_samples_batched(metric, chunk_divider, labeled_clusters):
assert False


def test_silhouette_score_batched_non_monotonic():
vecs = np.array([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0],
[2.0, 2.0, 2.0], [10.0, 10.0, 10.0]])
labels = np.array([0, 0, 1, 3])

cuml_score = cu_silhouette_score(X=vecs, labels=labels)
sk_score = sk_silhouette_score(X=vecs, labels=labels)
assert_almost_equal(cuml_score, sk_score, decimal=2)

vecs = np.array([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0], [10.0, 10.0, 10.0]])
labels = np.array([1, 1, 3])

cuml_score = cu_silhouette_score(X=vecs, labels=labels)
sk_score = sk_silhouette_score(X=vecs, labels=labels)
assert_almost_equal(cuml_score, sk_score, decimal=2)


def score_homogeneity(ground_truth, predictions, use_handle):
return score_labeling_with_handle(cuml.metrics.homogeneity_score,
ground_truth,
Expand Down

0 comments on commit 96eaf62

Please sign in to comment.