# Daviesbouldinscore

> Compute the DaviesBouldinScore metric — provided by torchmetrics. Use when the user has predictions and ground-truth and needs to compute DaviesBouldinScore, or asks how to score with DaviesBouldinScore.

- Skill: `qhjqhj00/daviesbouldinscore` (Agent Skill)
- Install (CLI): `npx skillmds add qhjqhj00/daviesbouldinscore`
- Raw SKILL.md: https://api.skillmd.com/api/skills/qhjqhj00/daviesbouldinscore/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: qhjqhj00 (https://skillmd.com/u/qhjqhj00)
- Updated: 2026-09-08
- Page: https://skillmd.com/skills/qhjqhj00/daviesbouldinscore

---


# daviesbouldinscore

> Metric `DaviesBouldinScore` from `torchmetrics` (torchmetrics.clustering.DaviesBouldinScore)

## When to invoke this skill

The user has predictions + ground truth and asks to evaluate with DaviesBouldinScore, or
mentions `torchmetrics.clustering.DaviesBouldinScore` directly, or wants the standard torchmetrics implementation.

## Reference signature

```python
from torchmetrics.clustering import DaviesBouldinScore

# DaviesBouldinScore(**kwargs: Any) -> None
```

## Library docstring

```
Compute `Davies-Bouldin Score`_ for clustering algorithms.

Given the following quantities:

.. math::
    S_i = \left( \frac{1}{T_i} \sum_{j=1}^{T_i} ||X_j - A_i||^2_2 \right)^{1/2}

where :math:`T_i` is the number of samples in cluster :math:`i`, :math:`X_j` is the :math:`j`-th sample in cluster
:math:`i`, and :math:`A_i` is the centroid of cluster :math:`i`. This quantity is the average distance between all
the samples in cluster :math:`i` and its centroid. Let

.. math::
    M_{i,j} = ||A_i - A_j||_2

e.g. the distance between the centroids of cluster :math:`i` and cluster :math:`j`. Then the Davies-Bouldin score
is defined as:

.. math::
    DB = \frac{1}{n_{clusters}} \sum_{i=1}^{n_{clusters}} \max_{j \neq i} \left( \frac{S_i + S_j}{M_{i,j}} \right)

This clustering metric is an intrinsic measure, because it does not rely on ground truth labels for the evaluation.
Instead it examines how well the clusters are separated from each other. The score is higher when clusters are dense
and well separated, which relates to a standard concept of a cluster.

As input to ``forward`` and ``update`` the metric accepts the following input:

- ``data`` (:class:`~torch.Tensor`): float tensor with shape ``(N,d)`` with the embedded data. ``d`` is the
  dimensionality of the embedding space.
- ``labels`` (:class:`~torch.Tensor`): single integer tensor with shape ``(N,)`` with cluster labels

As output of ``forward`` and ``compute`` the metric returns the following output:

- ``chs`` (:class:`~torch.Tensor`): A tensor with the Calinski Harabasz Score

Args:
    kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

Example::
    >>> from torch import randn, randint
    >>> from torchmetrics.clustering import DaviesBouldinScore
    >>> data = randn(10, 3)
    >>> labels = randint(3, (10,))
    >>> metric = DaviesBouldinScore()
    >>> metric(data, labels)
    tensor(1.2540)
```

## Quick recipe

```python
import torchmetrics.clustering as _m
score = _m.DaviesBouldinScore(y_true, y_pred)
```

## Don'ts

- Don't reimplement when the library version handles edge cases (NaN, ties, empty inputs) better than a hand-rolled formula.
- Always check the library version's argument order — sklearn is `(y_true, y_pred)` while torchmetrics is `(preds, target)`.

