---
title: Dunn Index
description: Compute Dunn Index.
canonical: 'https://lightning.ai/docs/torchmetrics/stable/clustering/dunn_index'
---
# Dunn Index {#dunn-index}

## Module Interface {#module-interface}

### `class torchmetrics.clustering.DunnIndex(p=2, **kwargs)` {#torchmetrics.clustering.DunnIndex}

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

Compute [Dunn Index](https://en.wikipedia.org/wiki/Dunn_index).

```math
DI_m = \frac{\min_{1\leq i<j\leq m} \delta(C_i,C_j)}{\max_{1\leq k\leq m} \Delta_k}
```

Where $`C_i`$ is a cluster of tensors, $`C_j`$ is a cluster of tensors, and $`\delta(C_i,C_j)`$ is the intercluster distance metric for $`m`$ clusters.

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:

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

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
>>> import torch
>>> from torchmetrics.clustering import DunnIndex
>>> data = torch.tensor([[0, 0], [0.5, 0], [1, 0], [0.5, 1]])
>>> labels = torch.tensor([0, 0, 0, 1])
>>> dunn_index = DunnIndex(p=2)
>>> dunn_index(data, labels)
tensor(2.)
```

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

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/clustering/dunn_index.py#L89-L129)

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 DunnIndex
>>> data = torch.tensor([[0, 0], [0.5, 0], [1, 0], [0.5, 1]])
>>> labels = torch.tensor([0, 0, 0, 1])
>>> metric = DunnIndex(p=2)
>>> metric.update(data, labels)
>>> fig_, ax_ = metric.plot(metric.compute())
```

[![../../\_images/dunn\_index-1.png](/docs/torchmetrics/1.9.0/static/_images/dunn_index-1.png)](/docs/torchmetrics/1.9.0/static/_images/dunn_index-1.png)

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

[![../../\_images/dunn\_index-2.png](/docs/torchmetrics/1.9.0/static/_images/dunn_index-2.png)](/docs/torchmetrics/1.9.0/static/_images/dunn_index-2.png)

## Functional Interface {#functional-interface}

### `torchmetrics.functional.clustering.dunn_index(data, labels, p=2)` {#torchmetrics.functional.clustering.dunn_index}

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

Compute the Dunn index.

Parameters:

*   **data** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – feature vectors
    
*   **labels** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – cluster labels
    
*   **p** ([`float`](https://docs.python.org/3/builtins/functions.html#float "(in Python v3.15)")) – p-norm used for distance metric
    

Return type:

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

Returns:

scalar tensor with the dunn index

Example

```python
>>> from torchmetrics.functional.clustering import dunn_index
>>> data = torch.tensor([[0, 0], [0.5, 0], [1, 0], [0.5, 1]])
>>> labels = torch.tensor([0, 0, 0, 1])
>>> dunn_index(data, labels)
tensor(2.)
```

---

**Navigation**

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