---
title: Rand Score
description: Compute Rand Score (alternatively known as Rand Index).
canonical: 'https://lightning.ai/docs/torchmetrics/stable/clustering/rand_score'
---
# Rand Score {#rand-score}

## Module Interface {#module-interface}

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

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

Compute [Rand Score](https://link.springer.com/article/10.1007/BF01908075) (alternatively known as Rand Index).

```math
RS(U, V) = \text{number of agreeing pairs} / \text{number of pairs}
```

The number of agreeing pairs is every $`(i, j)`$ pair of samples where $`i \in U`$ and $`j \in V`$ (the predicted and true clusterings, respectively) that are in the same cluster for both clusterings. The metric is symmetric, therefore swapping $`U`$ and $`V`$ yields the same rand score.

This clustering metric is an extrinsic measure, because it requires ground truth clustering labels, which may not be available in practice since clustering in generally is used for unsupervised learning.

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

*   `preds` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.13)")): single integer tensor with shape `(N,)` with predicted cluster labels
    
*   `target` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.13)")): single integer tensor with shape `(N,)` with ground truth cluster labels
    

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

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

Parameters:

**kwargs** ([`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.14)")) – 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 RandScore
>>> preds = torch.tensor([2, 1, 0, 1, 0])
>>> target = torch.tensor([0, 2, 1, 1, 0])
>>> metric = RandScore()
>>> metric(preds, target)
tensor(0.6000)
```

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

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/clustering/rand_score.py#L87-L125)

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.13)"), [`Sequence`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence "(in Python v3.14)")\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.13)")\], [`None`](https://docs.python.org/3/library/constants.html#None "(in Python v3.14)")\]) – 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.14)")\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.1)")\]) – An matplotlib axis object. If provided will add plot to that axis
    

Return type:

[`tuple`](https://docs.python.org/3/library/stdtypes.html#tuple "(in Python v3.14)")\[[`Figure`](https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure "(in Matplotlib v3.11.1)"), `Union`\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.1)"), [`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/library/exceptions.html#ModuleNotFoundError "(in Python v3.14)") – If matplotlib is not installed

```python
>>> # Example plotting a single value
>>> import torch
>>> from torchmetrics.clustering import RandScore
>>> metric = RandScore()
>>> metric.update(torch.randint(0, 4, (10,)), torch.randint(0, 4, (10,)))
>>> fig_, ax_ = metric.plot(metric.compute())
```

[![../../\_images/rand\_score-1.png](/docs/torchmetrics/1.9.0/static/_images/rand_score-1.png)](/docs/torchmetrics/1.9.0/static/_images/rand_score-1.png)

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

[![../../\_images/rand\_score-2.png](/docs/torchmetrics/1.9.0/static/_images/rand_score-2.png)](/docs/torchmetrics/1.9.0/static/_images/rand_score-2.png)

## Functional Interface {#functional-interface}

### `torchmetrics.functional.clustering.rand_score(preds, target)` {#torchmetrics.functional.clustering.rand_score}

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

Compute the Rand score between two clusterings.

Parameters:

*   **preds** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.13)")) – predicted cluster labels
    
*   **target** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.13)")) – ground truth cluster labels
    

Return type:

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

Returns:

scalar tensor with the rand score

Example

```python
>>> from torchmetrics.functional.clustering import rand_score
>>> import torch
>>> rand_score(torch.tensor([0, 0, 1, 1]), torch.tensor([1, 1, 0, 0]))
tensor(1.)
>>> rand_score(torch.tensor([0, 0, 1, 2]), torch.tensor([0, 0, 1, 1]))
tensor(0.8333)
```

---

**Navigation**

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