Cluster Accuracy
Module Interface
- class torchmetrics.clustering.ClusterAccuracy(num_classes, **kwargs)[source]
Compute Cluster Accuracy between predicted and target clusters.
Where
is a function that maps predicted clusters to target clusters , is the number of samples, is the predicted cluster for sample , is the target cluster for sample , and is the indicator function. The function 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
andupdate
the metric accepts the following input:preds
(Tensor
): single integer tensor with shape(N,)
with predicted cluster labelstarget
(Tensor
): single integer tensor with shape(N,)
with ground truth cluster labels
As output of
forward
andcompute
the metric returns the following output:acc_score
(Tensor
): A tensor with the Cluster Accuracy score
- Parameters:
num_classes (
int
) – number of classeskwargs (
Any
) – Additional keyword arguments, see Advanced metric settings for more info.
- Raises:
RuntimeError – If
torch_linear_assignment
is not installed. To install, runpip 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 callingmetric.forward
ormetric.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:
- 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())
>>> # 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)
Functional Interface
- torchmetrics.functional.clustering.cluster_accuracy(preds, target, num_classes)[source]
Computes the clustering accuracy between the predicted and target clusters.
- Parameters:
- Return type:
- 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)