Registry Mirror
Skip to content

Conversation

@bardrop

@bardrop bardrop commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

silhouette_score gives wrong values in two cases.

A sample alone in its cluster. Its a(x) is 0, so it scores (b - 0) / b = 1. Rousseeuw's definition (and scikit-learn) gives such a sample s(x) = 0. So a clustering scores higher just by having one-point clusters.

Example: points [0], [1], [10] with labels 0, 0, 1:

  • [0] scores 0.9, [1] scores 8/9, [10] is alone → 0
  • expected mean 0.5963, linfa gave 0.9296

Identical samples. When a(x) = b(x) = 0, the score is 0 / 0 = NaN, and the whole mean becomes NaN. scikit-learn gives 0 there (nan_to_num).

Example: four equal points [1, 1] with labels 0, 0, 1, 1 → linfa gave NaN, expected 0.

Change

  • remember the size of the sample's own cluster; if it is 1, the sample scores 0
  • divide by max(a, b) only when it is not 0, else the sample scores 0

Tests

Two new tests in metrics_clustering.rs, both fail before the change (0.9296 vs 0.5963, NaN vs 0). cargo test --lib in linfa: 78 pass.

The single-label case still returns 1, as before (scikit-learn raises an error there); I did not change it.

…samples is 0

A sample alone in its cluster got a(x) = 0, so it scored 1 instead of 0
(Rousseeuw's definition, same as scikit-learn). When a(x) = b(x) = 0 the
score was 0 / 0 = NaN; scikit-learn gives 0.
@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.98%. Comparing base (9a8f637) to head (332405f).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #481      +/-   ##
==========================================
+ Coverage   76.95%   76.98%   +0.02%     
==========================================
  Files         105      105              
  Lines        7612     7617       +5     
==========================================
+ Hits         5858     5864       +6     
+ Misses       1754     1753       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant