Cluster Accuracy

Module Interface

class torchmetrics.clustering.ClusterAccuracy(num_classes, **kwargs)[source]

Compute Cluster Accuracy between predicted and target clusters.

Cluster Accuracy=maxg1Nn=1N1g(pn)=tn

Where g is a function that maps predicted clusters p to target clusters t, N is the number of samples, pn is the predicted cluster for sample n, tn is the target cluster for sample n, and 1 is the indicator function. The function g is determined by solving the linear sum assignment problem.

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

  • acc_score (Tensor): A tensor with the Cluster Accuracy score

Parameters:
Raises:
  • RuntimeError – If torch_linear_assignment is not installed. To install, run pip install torchmetrics[clustering].

  • ValueError – If num_classes is not a positive integer

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

Functional Interface

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

Computes the clustering accuracy between the predicted and target clusters.

Parameters:
  • preds (Tensor) – predicted cluster labels

  • target (Tensor) – ground truth cluster labels

  • num_classes (int) – number of classes

Return type:

Tensor

Returns:

Scalar tensor with clustering accuracy between 0.0 and 1.0

Raises:

RuntimeError – If torch_linear_assignment is not installed

Example

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

You are viewing an outdated version of TorchMetrics Docs

Click here to view the latest version→