Lightning-AI / Lightning-AI/torchmetrics

MCC Multilabel with average macro

Open
#3,363 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
2.5k
Forks
526
Avg merge
6d 11h
Merged PRs (30d)
5

Description

🚀 Feature

The current behaviour of the multilabel MCC is to calculate the overall MCC by adding the TP, TN, FP & FN of each label (C matrices of shape (2,2)). This does, however, lead to an unintended behaviour in which the metric is computed as a micro average and never as macro.

Motivation

There are disbalanced multilabel classification tasks which might rely on the MCC as their primary metric. By not offering the option to select the average on this metric, this often leads to a not representative metric regarding the problem.

Pitch

Add the possibility to compute macro average on MCC.

Alternatives

I've done this implementation for my code, although it is a bit messy, but to give an idea of what we are looking for

class MacroMCCFromBinary(Metric):
    """Macro-averaged MCC: mean of per-label binary MCCs (mirrors STL heads)."""

    def __init__(self, num_labels: int):
        super().__init__()
        self.num_labels = num_labels
        self._binary_mccs = torch.nn.ModuleList(
            [MatthewsCorrCoef(task="binary") for _ in range(num_labels)]
        )

    def update(self, preds: torch.Tensor, target: torch.Tensor) -> None:
        for i, mcc in enumerate(self._binary_mccs):
            mcc.update(preds[:, i], target[:, i])

    def compute(self) -> torch.Tensor:
        return torch.stack([mcc.compute() for mcc in self._binary_mccs]).mean()

    def reset(self) -> None:
        for mcc in self._binary_mccs:
            mcc.reset()
Additional context

I think part of the fix is located on https://github.com/Lightning-AI/torchmetrics/blob/master/src/torchmetrics/functional/classification/matthews_corrcoef.py

There must be some kind of fix.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading src/torchmetrics/functional/classification/matthews_corrcoef.py and trace how multilabel MCC currently aggregates label confusion matrices. Check the surrounding classification metric APIs for the expected averaging pattern, then verify that MCC supports selecting macro averaging while preserving the current behavior when no option is selected.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.