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:

  • chs (Tensor): A tensor with the Calinski Harabasz Score

Parameters:

kwargs (Any) – Additional keyword arguments, see Advanced metric settings 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)
plot(val=None, ax=None)[source]

Plot a single or multiple values from the metric.

Parameters:
  • val (Union[Tensor, Sequence[Tensor], None]) – 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[Axes]) – An matplotlib axis object. If provided will add plot to that axis

Return type:

tuple[Figure, Union[Axes, ndarray]]

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())
../_images/davies_bouldin_score-1.png
>>> # 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)
../_images/davies_bouldin_score-2.png

Functional Interface

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

Compute the Davies bouldin score for clustering algorithms.

Parameters:
  • data (Tensor) – float tensor with shape (N,d) with the embedded data.

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

Return type:

Tensor

Returns:

Scalar tensor with the Davies bouldin score

Example

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