---
title: Davies Bouldin Score
description: Compute Davies-Bouldin Score for clustering algorithms.
canonical: 'https://lightning.ai/docs/torchmetrics/stable/clustering/davies_bouldin_score'
---
# Davies Bouldin Score {#davies-bouldin-score}

## Module Interface {#module-interface}

### `class torchmetrics.clustering.DaviesBouldinScore(**kwargs)` {#torchmetrics.clustering.DaviesBouldinScore}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/clustering/davies_bouldin_score.py#L29-L138)

Compute [Davies-Bouldin Score](https://en.wikipedia.org/wiki/Davies%E2%80%93Bouldin_index) 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 $`T_i`$ is the number of samples in cluster $`i`$, $`X_j`$ is the $`j`$\-th sample in cluster $`i`$, and $`A_i`$ is the centroid of cluster $`i`$. This quantity is the average distance between all the samples in cluster $`i`$ and its centroid. Let

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

e.g. the distance between the centroids of cluster $`i`$ and cluster $`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` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): float tensor with shape `(N,d)` with the embedded data. `d` is the dimensionality of the embedding space.
    
*   `labels` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): single integer tensor with shape `(N,)` with cluster labels
    

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

*   `chs` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): A tensor with the Calinski Harabasz Score
    

Parameters:

**kwargs** ([`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")) – Additional keyword arguments, see [Advanced metric settings](/docs/torchmetrics/1.9.0/user-guide/overview.md#metric-kwargs) for more info.

Example::

```python
>>> 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)
```

#### `plot(val=None, ax=None)` {#torchmetrics.clustering.DaviesBouldinScore.plot}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/clustering/davies_bouldin_score.py#L100-L138)

Plot a single or multiple values from the metric.

Parameters:

*   **val** (`Union`\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)"), [`Sequence`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence "(in Python v3.15)")\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")\], [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]) – Either a single result from calling metric.forward or metric.compute or a list of these results. If no value is provided, will automatically call metric.compute and plot that result.
    
*   **ax** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.2)")\]) – An matplotlib axis object. If provided will add plot to that axis
    

Return type:

[`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple "(in Python v3.15)")\[[`Figure`](https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure "(in Matplotlib v3.11.2)"), `Union`\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.2)"), [`ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray "(in NumPy v2.5)")\]\]

Returns:

Figure and Axes object

Raises:

[**ModuleNotFoundError**](https://docs.python.org/3/builtins/exceptions.html#ModuleNotFoundError "(in Python v3.15)") – If matplotlib is not installed

```python
>>> # Example plotting a single value
>>> import torch
>>> from torchmetrics.clustering import DaviesBouldinScore
>>> metric = DaviesBouldinScore()
>>> metric.update(torch.randn(20, 3), torch.randint(0, 2, (20,)))
>>> fig_, ax_ = metric.plot(metric.compute())
```

[![../../\_images/davies\_bouldin\_score-1.png](/docs/torchmetrics/1.9.0/static/_images/davies_bouldin_score-1.png)](/docs/torchmetrics/1.9.0/static/_images/davies_bouldin_score-1.png)

```python
>>> # Example plotting multiple values
>>> import torch
>>> from torchmetrics.clustering import DaviesBouldinScore
>>> metric = DaviesBouldinScore()
>>> values = []
>>> for _ in range(10):
...     values.append(metric(torch.randn(20, 3), torch.randint(0, 2, (20,))))
>>> fig_, ax_ = metric.plot(values)
```

[![../../\_images/davies\_bouldin\_score-2.png](/docs/torchmetrics/1.9.0/static/_images/davies_bouldin_score-2.png)](/docs/torchmetrics/1.9.0/static/_images/davies_bouldin_score-2.png)

## Functional Interface {#functional-interface}

### `torchmetrics.functional.clustering.davies_bouldin_score(data, labels)` {#torchmetrics.functional.clustering.davies_bouldin_score}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/functional/clustering/davies_bouldin_score.py#L23-L66)

Compute the Davies bouldin score for clustering algorithms.

Parameters:

*   **data** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – float tensor with shape `(N,d)` with the embedded data.
    
*   **labels** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – single integer tensor with shape `(N,)` with cluster labels
    

Return type:

[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")

Returns:

Scalar tensor with the Davies bouldin score

Example

```python
>>> from torch import randn, randint
>>> from torchmetrics.functional.clustering import davies_bouldin_score
>>> data = randn(20, 3)
>>> labels = randint(0, 3, (20,))
>>> davies_bouldin_score(data, labels)
tensor(2.7418)
```

---

**Navigation**

- Parent: [Clustering](/docs/torchmetrics/1.9.0/clustering.md)
