Skip to content

Commit

Permalink
Modify the distance calculating function in Silhouette coefficient (r…
Browse files Browse the repository at this point in the history
…elated to an issue raised in #1468).
  • Loading branch information
hoanganhngo610 committed Dec 6, 2023
1 parent 4660aea commit 3ea1918
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions river/metrics/silhouette.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Silhouette(metrics.base.ClusteringMetric):
... metric.update(x, y_pred, k_means.centers)
>>> metric
Silhouette: 0.568058
Silhouette: 0.32145
References
----------
Expand All @@ -65,18 +65,18 @@ def __init__(self):

@staticmethod
def _find_distance_second_closest_center(centers, x):
distances = {i: math.sqrt(utils.math.minkowski_distance(centers[i], x, 2)) for i in centers}
distances = {i: utils.math.minkowski_distance(centers[i], x, 2) for i in centers}
return sorted(distances.values())[-2]

def update(self, x, y_pred, centers, w=1.0):
distance_closest_centroid = math.sqrt(utils.math.minkowski_distance(centers[y_pred], x, 2))
distance_closest_centroid = utils.math.minkowski_distance(centers[y_pred], x, 2)
self._sum_distance_closest_centroid += distance_closest_centroid

distance_second_closest_centroid = self._find_distance_second_closest_center(centers, x)
self._sum_distance_second_closest_centroid += distance_second_closest_centroid

def revert(self, x, y_pred, centers, w=1.0):
distance_closest_centroid = math.sqrt(utils.math.minkowski_distance(centers[y_pred], x, 2))
distance_closest_centroid = utils.math.minkowski_distance(centers[y_pred], x, 2)
self._sum_distance_closest_centroid -= distance_closest_centroid

distance_second_closest_centroid = self._find_distance_second_closest_center(centers, x)
Expand Down

0 comments on commit 3ea1918

Please sign in to comment.