Fowlkes-Mallows Index

Module Interface

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

Compute Fowlkes-Mallows Index.

\[FMI(U,V) = \frac{TP}{\sqrt{(TP + FP) * (TP + FN)}}\]

Where \(TP\) is the number of true positives, \(FP\) is the number of false positives, and \(FN\) is the number of false negatives.

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

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

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

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

  • fmi (Tensor): A tensor with the Fowlkes-Mallows index.

Parameters:

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

Example::
>>> import torch
>>> from torchmetrics.clustering import FowlkesMallowsIndex
>>> preds = torch.tensor([2, 2, 0, 1, 0])
>>> target = torch.tensor([2, 2, 1, 1, 0])
>>> fmi = FowlkesMallowsIndex()
>>> fmi(preds, target)
tensor(0.5000)
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 FowlkesMallowsIndex
>>> metric = FowlkesMallowsIndex()
>>> metric.update(torch.randint(0, 4, (10,)), torch.randint(0, 4, (10,)))
>>> fig_, ax_ = metric.plot(metric.compute())
../_images/fowlkes_mallows_index-1.png
>>> # Example plotting multiple values
>>> import torch
>>> from torchmetrics.clustering import FowlkesMallowsIndex
>>> metric = FowlkesMallowsIndex()
>>> values = [ ]
>>> for _ in range(10):
...     values.append(metric(torch.randint(0, 4, (10,)), torch.randint(0, 4, (10,))))
>>> fig_, ax_ = metric.plot(values)
../_images/fowlkes_mallows_index-2.png

Functional Interface

torchmetrics.functional.clustering.fowlkes_mallows_index(preds, target)[source]

Compute Fowlkes-Mallows index between two clusterings.

Parameters:
  • preds (Tensor) – predicted cluster labels

  • target (Tensor) – ground truth cluster labels

Return type:

Tensor

Returns:

Scalar tensor with Fowlkes-Mallows index

Example

>>> import torch
>>> from torchmetrics.functional.clustering import fowlkes_mallows_index
>>> preds = torch.tensor([2, 2, 0, 1, 0])
>>> target = torch.tensor([2, 2, 1, 1, 0])
>>> fowlkes_mallows_index(preds, target)
tensor(0.5000)