TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Compute Silhouette Score

Metrics & Evaluation
Medium

Compute the mean Silhouette Score for clustered points using Euclidean distance. For point iii, let a(i)a(i)a(i) be its average distance to the other points in its own cluster, and let b(i)b(i)b(i) be the smallest average distance from the point to any other cluster.

The point score is

s(i)=b(i)−a(i)max⁡(a(i),b(i))s(i) = \frac{b(i)-a(i)}{\max(a(i),b(i))}s(i)=max(a(i),b(i))b(i)−a(i)​

Return the mean of s(i)s(i)s(i) over all points as a Python float. Every cluster contains at least two points.

Loading visualization...

Examples

Input: X = [[0, 0], [0, 1], [1, 0], [5, 5], [5, 6], [6, 5]], labels = [0, 0, 0, 1, 1, 1]

Output: 0.8398

Explanation: Each point is much closer on average to its own cluster than to the other cluster.

Hint 1

X[:, None, :] - X[None, :, :] forms every pairwise displacement.

Hint 2

A boolean matrix from labels[:, None] == labels[None, :] identifies same-cluster pairs.

Hint 3

Set each point's own-cluster column to infinity before taking the nearest other-cluster mean.

Requirements

  • Compute pairwise Euclidean distances with NumPy broadcasting
  • Support two or more clusters
  • Avoid nested Python loops over pairs of samples
  • Return the mean score as a Python float

Constraints

  • X has shape (N,D)(N,D)(N,D) and labels has length NNN
  • 2≤N≤5002 \leq N \leq 5002≤N≤500
  • Every cluster contains at least two points
  • Use NumPy only
Try Similar Problems
K Means Centroid UpdateK Means AssignmentKnn DistanceEuclidean DistanceClassification Metrics

Sign in to take notes on this problem

Case 1

Accepts: array

Accepts: array

You must run your code first.
PrevNext

Compute Silhouette Score

Metrics & Evaluation
Medium

Compute the mean Silhouette Score for clustered points using Euclidean distance. For point iii, let a(i)a(i)a(i) be its average distance to the other points in its own cluster, and let b(i)b(i)b(i) be the smallest average distance from the point to any other cluster.

The point score is

s(i)=b(i)−a(i)max⁡(a(i),b(i))s(i) = \frac{b(i)-a(i)}{\max(a(i),b(i))}s(i)=max(a(i),b(i))b(i)−a(i)​

Return the mean of s(i)s(i)s(i) over all points as a Python float. Every cluster contains at least two points.

Loading visualization...

Examples

Input: X = [[0, 0], [0, 1], [1, 0], [5, 5], [5, 6], [6, 5]], labels = [0, 0, 0, 1, 1, 1]

Output: 0.8398

Explanation: Each point is much closer on average to its own cluster than to the other cluster.

Hint 1

X[:, None, :] - X[None, :, :] forms every pairwise displacement.

Hint 2

A boolean matrix from labels[:, None] == labels[None, :] identifies same-cluster pairs.

Hint 3

Set each point's own-cluster column to infinity before taking the nearest other-cluster mean.

Requirements

  • Compute pairwise Euclidean distances with NumPy broadcasting
  • Support two or more clusters
  • Avoid nested Python loops over pairs of samples
  • Return the mean score as a Python float

Constraints

  • X has shape (N,D)(N,D)(N,D) and labels has length NNN
  • 2≤N≤5002 \leq N \leq 5002≤N≤500
  • Every cluster contains at least two points
  • Use NumPy only
Try Similar Problems
K Means Centroid UpdateK Means AssignmentKnn DistanceEuclidean DistanceClassification Metrics

Sign in to take notes on this problem

Case 1

Accepts: array

Accepts: array

You must run your code first.