Matthews Corr. Coef.¶
Module Interface¶
- class torchmetrics.MatthewsCorrCoef(num_classes, threshold=0.5, **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 classestarget(long tensor):(N, ...)
If preds and target are the same shape and preds is a float tensor, we use the
self.thresholdargument 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.
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/probabilitiestarget¶ (
Tensor) –target(long tensor), tensor with shape(N, ...)with ground true labelsthreshold¶ (
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