Skip to content

Commit

Permalink
Add Matthews Correlation Coefficient (#561)
Browse files Browse the repository at this point in the history
* Add Matthews Correlation Coefficient (MCC) metric

* Reorder arguments in _multilabel_mcc_reference function

* update linux version for code check

* revert ubuntu version change & update action versions

* use torch instead of dlpack to convert tensors to numpy

* update numpy conversion methods

* set dtype for multilabel test inputs

* fix doctest error

* update implementation of multilabel confusion matrix

* revert implementation update & add print statements for debugging

* fix tests

* use float64 for internal computations

* Refactor test files and remove debug print statement

* Fix data type in Matthews correlation coefficient calculation

* Update dtype in _mcc_reduce function

* Fix NaN handling in confusion matrix computation

* use epsilon to check `denom` to avoid precision issues

---------

Signed-off-by: Amrit Krishnan <amrit110@gmail.com>
Co-authored-by: Amrit Krishnan <amrit110@gmail.com>
  • Loading branch information
fcogidi and amrit110 authored Mar 5, 2024
1 parent f34e373 commit b22c555
Show file tree
Hide file tree
Showing 17 changed files with 1,119 additions and 70 deletions.
5 changes: 5 additions & 0 deletions cyclops/evaluate/metrics/experimental/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
)
from cyclops.evaluate.metrics.experimental.mae import MeanAbsoluteError
from cyclops.evaluate.metrics.experimental.mape import MeanAbsolutePercentageError
from cyclops.evaluate.metrics.experimental.matthews_corr_coef import (
BinaryMCC,
MulticlassMCC,
MultilabelMCC,
)
from cyclops.evaluate.metrics.experimental.metric_dict import MetricDict
from cyclops.evaluate.metrics.experimental.mse import MeanSquaredError
from cyclops.evaluate.metrics.experimental.negative_predictive_value import (
Expand Down
1 change: 1 addition & 0 deletions cyclops/evaluate/metrics/experimental/confusion_matrix.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Confusion matrix."""

from types import ModuleType
from typing import Any, Optional, Tuple, Union

Expand Down
5 changes: 5 additions & 0 deletions cyclops/evaluate/metrics/experimental/functional/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
from cyclops.evaluate.metrics.experimental.functional.mape import (
mean_absolute_percentage_error,
)
from cyclops.evaluate.metrics.experimental.functional.matthews_corr_coef import (
binary_mcc,
multiclass_mcc,
multilabel_mcc,
)
from cyclops.evaluate.metrics.experimental.functional.mse import mean_squared_error
from cyclops.evaluate.metrics.experimental.functional.negative_predictive_value import (
binary_npv,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Functions for computing the confusion matrix for classification tasks."""

# mypy: disable-error-code="no-any-return"
import warnings
from types import ModuleType
from typing import Literal, Optional, Tuple, Union

Expand Down Expand Up @@ -40,6 +42,16 @@ def _normalize_confusion_matrix(
if normalize == "all":
return safe_divide(confmat, xp.sum(confmat, axis=(-1, -2), keepdims=True))

nan_elements = int(0 or apc.size(confmat[xp.isnan(confmat)]))
if nan_elements:
confmat[xp.isnan(confmat)] = 0
warnings.warn(
f"Encountered {nan_elements} NaN elements in the confusion matrix. "
"These elements were replaced with 0.",
category=RuntimeWarning,
stacklevel=1,
)

return confmat


Expand Down
Loading

0 comments on commit b22c555

Please sign in to comment.