MatthewsCorrCoef#
- class ignite.metrics.MatthewsCorrCoef(output_transform=<function MatthewsCorrCoef.<lambda>>, check_compute_fn=False, device=device(type='cpu'), skip_unrolling=False)[source]#
Compute the Matthews correlation coefficient (MCC).
The Matthews correlation coefficient is used in machine learning as a measure of the quality of binary and multiclass classifications. It takes into account true and false positives and negatives and is generally regarded as a balanced measure which can be used even if the classes are of very different sizes. The MCC is in essence a correlation coefficient value between -1 and +1. A coefficient of +1 represents a perfect prediction, 0 an average random prediction and -1 an inverse prediction.
This metric is suitable for both binary and multiclass classification. In the binary case, it is calculated using the entries of the confusion matrix, whereas for multiclass tasks, it is computed as a generalized correlation coefficient.
In case of multiclass classification with shape (N, C) for y_pred and (N, C) for y, the predicted class is determined by the argmax of y_pred and y. In case of multiclass classification with shape (N, C) for y_pred and (N,) for y, the predicted class is determined by the argmax of y_pred and the true class is determined by the value in y.
- Args:
- output_transform: a callable that is used to transform the
Engine’sprocess_function’s output into the form expected by the metric. This can be useful if, for example, you have a multi-output model and you want to compute the metric with respect to one of the outputs. By default, this metric requires the output as(x, y).- device: specifies which device updates are accumulated on. Setting the
metric’s device to be the same as your
updatearguments ensures theupdatemethod is non-blocking. By default, CPU.- check_compute_fn: if True, compute_fn is run on the first batch of data to ensure there are no issues.
If issues exist, user is warned that there might be an issue with the compute_fn. Default, True.
- skip_unrolling: specifies whether output should be unrolled before being fed to update method. Should be
true for multi-output model, for example, if
y_predcontains multi-output as(y_pred_a, y_pred_b)Alternatively,output_transformcan be used to handle this.
Examples:
from collections import OrderedDict import torch from torch import nn, optim from ignite.engine import * from ignite.handlers import * from ignite.metrics import * from ignite.metrics.clustering import * from ignite.metrics.fairness import * from ignite.metrics.rec_sys import * from ignite.metrics.regression import * from ignite.utils import * # create default evaluator for doctests def eval_step(engine, batch): return batch default_evaluator = Engine(eval_step) # create default optimizer for doctests param_tensor = torch.zeros([1], requires_grad=True) default_optimizer = torch.optim.SGD([param_tensor], lr=0.1) # create default trainer for doctests # as handlers could be attached to the trainer, # each test must define his own trainer using `.. testsetup:` def get_default_trainer(): def train_step(engine, batch): return batch return Engine(train_step) # create default model for doctests default_model = nn.Sequential(OrderedDict([ ('base', nn.Linear(4, 2)), ('fc', nn.Linear(2, 1)) ])) manual_seed(666)
y_pred = torch.tensor([+1, +1, +1, -1]) y_true = torch.tensor([+1, -1, +1, +1]) matthews_corrcoef = MatthewsCorrCoef() matthews_corrcoef.attach(default_evaluator, 'mcc') state = default_evaluator.run([[y_pred, y_true]]) print(state.metrics['mcc'])
-0.33...
New in version 0.5.4.
Methods