# Davies Bouldin Score¶

## Module Interface¶

class torchmetrics.clustering.DaviesBouldinScore(**kwargs)[source]

Compute Davies-Bouldin Score for clustering algorithms.

Given the following quantities:

$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

$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:

$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): float tensor with shape (N,d) with the embedded data. d is the dimensionality of the embedding space.

• labels (Tensor): single integer tensor with shape (N,) with cluster labels

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

Parameters:

kwargs (Any) – Additional keyword arguments, see Advanced metric settings for more info.

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

plot(val=None, ax=None)[source]

Plot a single or multiple values from the metric.

Parameters:
Return type:
Returns:

Figure and Axes object

Raises:

ModuleNotFoundError – If matplotlib is not installed

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

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


## Functional Interface¶

torchmetrics.functional.clustering.davies_bouldin_score(data, labels)[source]

Compute the Davies bouldin score for clustering algorithms.

Parameters:
Return type:

Tensor

Returns:

Scalar tensor with the Davies bouldin score

Example

>>> import torch
>>> from torchmetrics.functional.clustering import davies_bouldin_score
>>> _ = torch.manual_seed(42)
>>> data = torch.randn(10, 3)
>>> labels = torch.randint(0, 2, (10,))
>>> davies_bouldin_score(data, labels)
tensor(1.3249)