Shortcuts

# Matthews Corr. Coef.¶

## Module Interface¶

class torchmetrics.MatthewsCorrCoef(num_classes, threshold=0.5, compute_on_step=None, **kwargs)[source]

Calculates Matthews correlation coefficient that measures the general correlation or quality of a classification.

In the binary case it is defined as:

where TP, TN, FP and FN are respectively the true postitives, true negatives, false positives and false negatives. Also works in the case of multi-label or multi-class input.

Note

This metric produces a multi-dimensional output, so it can not be directly logged.

Forward accepts

• preds (float or long tensor): (N, ...) or (N, C, ...) where C is the number of classes

• target (long tensor): (N, ...)

If preds and target are the same shape and preds is a float tensor, we use the self.threshold argument to convert into integer labels. This is the case for binary and multi-label probabilities.

If preds has an extra dimension as in the case of multi-class scores we perform an argmax on dim=1.

Parameters

Example

>>> from torchmetrics import MatthewsCorrCoef
>>> target = torch.tensor([1, 1, 0, 0])
>>> preds = torch.tensor([0, 1, 0, 0])
>>> matthews_corrcoef = MatthewsCorrCoef(num_classes=2)
>>> matthews_corrcoef(preds, target)
tensor(0.5774)


Initializes internal Module state, shared by both nn.Module and ScriptModule.

compute()[source]

Computes matthews correlation coefficient.

Return type

Tensor

update(preds, target)[source]

Update state with predictions and targets.

Parameters
Return type

None

## Functional Interface¶

torchmetrics.functional.matthews_corrcoef(preds, target, num_classes, threshold=0.5)[source]

Calculates Matthews correlation coefficient that measures the general correlation or quality of a classification. In the binary case it is defined as:

where TP, TN, FP and FN are respectively the true postitives, true negatives, false positives and false negatives. Also works in the case of multi-label or multi-class input.

Parameters
• preds (Tensor) – (float or long tensor), Either a (N, ...) tensor with labels or (N, C, ...) where C is the number of classes, tensor with labels/probabilities

• target (Tensor) – target (long tensor), tensor with shape (N, ...) with ground true labels

• num_classes (int) – Number of classes in the dataset.

• threshold (float) – Threshold value for binary or multi-label probabilities.

Example

>>> from torchmetrics.functional import matthews_corrcoef
>>> target = torch.tensor([1, 1, 0, 0])
>>> preds = torch.tensor([0, 1, 0, 0])
>>> matthews_corrcoef(preds, target, num_classes=2)
tensor(0.5774)

Return type

Tensor

© Copyright Copyright (c) 2020-2022, PyTorchLightning et al... Revision d27f1710.

Built with Sphinx using a theme provided by Read the Docs.
Versions
latest
stable
v0.8.1
v0.8.0
v0.7.3
v0.7.2
v0.7.1
v0.7.0
v0.6.2
v0.6.1
v0.6.0
v0.5.1
v0.5.0
v0.4.1
v0.4.0
v0.3.2
v0.3.1
v0.3.0
v0.2.0
v0.1.0