Compute the JaccardIndex metric — provided by torchmetrics. Use when the user has predictions and ground-truth and needs to compute JaccardIndex, or asks how to score with JaccardIndex.
Installs into .claude/skills of the current project.
Are you the author of Jaccardindex?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/qhjqhj00-jaccardindex)
---
name: jaccardindex
description: Compute the JaccardIndex metric — provided by torchmetrics. Use when the user has predictions and ground-truth and needs to compute JaccardIndex, or asks how to score with JaccardIndex.
metadata:
skill_kind: metric
source_lib: torchmetrics
import_path: torchmetrics.JaccardIndex
source: library_introspection
---
# jaccardindex
> Metric `JaccardIndex` from `torchmetrics` (torchmetrics.JaccardIndex)
## When to invoke this skill
The user has predictions + ground truth and asks to evaluate with JaccardIndex, or
mentions `torchmetrics.JaccardIndex` directly, or wants the standard torchmetrics implementation.
## Reference signature
```python
from torchmetrics import JaccardIndex
# JaccardIndex(task: Literal['binary', 'multiclass', 'multilabel'], threshold: float = 0.5, num_classes: Optional[int] = None, num_labels: Optional[int] = None, average: Optional[Literal['micro', 'macro', 'weighted', 'none']] = 'macro', ignore_index: Optional[int] = None, validate_args: bool = True, **kwargs: Any) -> torchmetrics.metric.Metric
```
## Library docstring
```
Calculate the Jaccard index for multilabel tasks.
The `Jaccard index`_ (also known as the intersection over union or jaccard similarity coefficient) is an statistic
that can be used to determine the similarity and diversity of a sample set. It is defined as the size of the
intersection divided by the union of the sample sets:
.. math:: J(A,B) = \frac{|A\cap B|}{|A\cup B|}
This function is a simple wrapper to get the task specific versions of this metric, which is done by setting the
``task`` argument to either ``'binary'``, ``'multiclass'`` or ``'multilabel'``. See the documentation of
:class:`~torchmetrics.classification.BinaryJaccardIndex`,
:class:`~torchmetrics.classification.MulticlassJaccardIndex` and
:class:`~torchmetrics.classification.MultilabelJaccardIndex` for the specific details of each argument influence
and examples.
Legacy Example:
>>> from torch import randint, tensor
>>> target = randint(0, 2, (10, 25, 25))
>>> pred = tensor(target)
>>> pred[2:5, 7:13, 9:15] = 1 - pred[2:5, 7:13, 9:15]
>>> jaccard = JaccardIndex(task="multiclass", num_classes=2)
>>> jaccard(pred, target)
tensor(0.9660)
```
## Quick recipe
```python
import torchmetrics as _m
score = _m.JaccardIndex(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)`.