Skip to content
Back to skills

Clusteraccuracy

ASecurity

Compute the ClusterAccuracy metric — provided by torchmetrics. Use when the user has predictions and ground-truth and needs to compute ClusterAccuracy, or asks how to score with ClusterAccuracy.

  • 3 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added September 11, 2026
documentationpython

Security analysis

A96/100
  • mediumInstalls packages at runtime which could introduce malicious dependencies

Pro shows the line behind each finding and how to fix it

Scanned September 11, 2026

npx -y skills add qhjqhj00/research-skills-pool --skill clusteraccuracy --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Clusteraccuracy?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Clusteraccuracy
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/qhjqhj00-clusteraccuracy/badge)](https://www.skillsdirectory.com/skills/qhjqhj00-clusteraccuracy)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: clusteraccuracy
description: Compute the ClusterAccuracy metric — provided by torchmetrics. Use when the user has predictions and ground-truth and needs to compute ClusterAccuracy, or asks how to score with ClusterAccuracy.
metadata:
  skill_kind: metric
  source_lib: torchmetrics
  import_path: torchmetrics.clustering.ClusterAccuracy
  source: library_introspection
---

# clusteraccuracy

> Metric `ClusterAccuracy` from `torchmetrics` (torchmetrics.clustering.ClusterAccuracy)

## When to invoke this skill

The user has predictions + ground truth and asks to evaluate with ClusterAccuracy, or
mentions `torchmetrics.clustering.ClusterAccuracy` directly, or wants the standard torchmetrics implementation.

## Reference signature

```python
from torchmetrics.clustering import ClusterAccuracy

# ClusterAccuracy(num_classes: int, **kwargs: Any) -> None
```

## Library docstring

```
Compute `Cluster Accuracy`_ between predicted and target clusters.

.. math::

    \text{Cluster Accuracy} = \max_g \frac{1}{N} \sum_{n=1}^N \mathbb{1}_{g(p_n) = t_n}

Where :math:`g` is a function that maps predicted clusters :math:`p` to target clusters :math:`t`, :math:`N` is the
number of samples, :math:`p_n` is the predicted cluster for sample :math:`n`, :math:`t_n` is the target cluster for
sample :math:`n`, and :math:`\mathbb{1}` is the indicator function. The function :math:`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`` (:class:`~torch.Tensor`): single integer tensor with shape ``(N,)`` with predicted cluster labels
- ``target`` (:class:`~torch.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`` (:class:`~torch.Tensor`): A tensor with the Cluster Accuracy score

Args:
    num_classes: number of classes
    kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

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.)
```

## Quick recipe

```python
import torchmetrics.clustering as _m
score = _m.ClusterAccuracy(y_true, y_pred)
```

## Don'ts

- Don't reimplement when the library version handles edge cases (NaN, ties, empty inputs) better than a hand-rolled formula.
- Always check the library version's argument order — sklearn is `(y_true, y_pred)` while torchmetrics is `(preds, target)`.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.