DaviesBouldinScore raises ValueError when number of clusters < 2
🐛 Bug description
The DaviesBouldinScore metric in ignite.metrics.clustering raises a ValueError when the number of unique clusters in the input labels is less than 2.
The Davies-Bouldin Index is calculated based on the ratio of within-cluster distances to between-cluster distances. When only one cluster exists, the between-cluster distance is undefined, causing the underlying sklearn.metrics.davies_bouldin_score to crash. This is particularly problematic in early training phases where a clustering model might not have separated the data yet.
Steps to reproduce
import torch
from ignite.metrics.clustering import DaviesBouldinScore
metric = DaviesBouldinScore()
features = torch.randn(10, 5)
# All samples assigned to the same cluster (0)
labels = torch.zeros(10, dtype=torch.long)
metric.update((features, labels))
# Raises ValueError: Number of labels is 1. Valid values are 2 to n_samples (inclusive).
print(metric.compute())
Closing this in favor of the consolidated tracking issue #3655. Let's continue the discussion and track PRs there.
Hi! I've submitted a fix for this issue in #3671.
The fix adds a validation check in _davies_bouldin_score using
torch.unique(labels) before calling sklearn, raising a clear
ValueError with a helpful message when the number of unique
clusters is less than 2. A test case was also added to cover this scenario.
Would appreciate any feedback!
Hi! I'm fixing this alongside #3641 since the root cause is identical — both metrics pass data to sklearn without validating the cluster count first.
Will include the fix for DaviesBouldinScore in the same PR with a
dedicated test case. Opening the PR shortly.
@HimanshuSingh-966 there are already pr opened for these 2 metric issue, you can check if other clustering metrics suffer from these issue and open pr from them. All the clustering metrics are in 'ignite/metrics/clustering`
I would recommend opening a issue first before submitting a pr.
Hi, I would like to work on this issue. Is it still available?
Hi, I have submitted a PR for this issue. Please review when you get time. Thank you!
@Karanrajpurohit855 there are already PRs addressing this issue, please work on something. I have to close your PR to prevent burden. Thanks for your understanding.
Thank you for the update. I understand 👍
I will look for another issue to contribute.
Done in https://github.com/pytorch/ignite/pull/3699.