ignite icon indicating copy to clipboard operation
ignite copied to clipboard

DaviesBouldinScore raises ValueError when number of clusters < 2

Open Prathamesh8989 opened this issue 8 months ago • 9 comments

🐛 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())

Prathamesh8989 avatar Mar 05 '26 18:03 Prathamesh8989

Closing this in favor of the consolidated tracking issue #3655. Let's continue the discussion and track PRs there.

TahaZahid05 avatar Mar 07 '26 15:03 TahaZahid05

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!

mohamedhiba avatar Mar 15 '26 03:03 mohamedhiba

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 avatar Mar 17 '26 07:03 HimanshuSingh-966

@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.

aaishwarymishra avatar Mar 17 '26 07:03 aaishwarymishra

Hi, I would like to work on this issue. Is it still available?

Karanrajpurohit855 avatar Mar 22 '26 16:03 Karanrajpurohit855

Hi, I have submitted a PR for this issue. Please review when you get time. Thank you!

Karanrajpurohit855 avatar Mar 25 '26 05:03 Karanrajpurohit855

@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.

vfdev-5 avatar Mar 25 '26 10:03 vfdev-5

Thank you for the update. I understand 👍
I will look for another issue to contribute.

Karanrajpurohit855 avatar Mar 25 '26 17:03 Karanrajpurohit855

Done in https://github.com/pytorch/ignite/pull/3699.

vfdev-5 avatar Mar 26 '26 11:03 vfdev-5