Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions orca_python/metrics/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,13 @@ def amae(y_true, y_pred):
costs = np.reshape(np.tile(range(n_class), n_class), (n_class, n_class))
costs = np.abs(costs - np.transpose(costs))
errors = costs * cm

# Remove rows with all zeros in the confusion matrix
non_zero_cm_rows = ~np.all(cm == 0, axis=1)
errors = errors[non_zero_cm_rows]
cm = cm[non_zero_cm_rows]

per_class_maes = np.sum(errors, axis=1) / np.sum(cm, axis=1).astype("double")
per_class_maes = per_class_maes[~np.isnan(per_class_maes)]
return np.mean(per_class_maes)


Expand Down Expand Up @@ -249,8 +254,13 @@ def mmae(y_true, y_pred):
costs = np.reshape(np.tile(range(n_class), n_class), (n_class, n_class))
costs = np.abs(costs - np.transpose(costs))
errors = costs * cm

# Remove rows with all zeros in the confusion matrix
non_zero_cm_rows = ~np.all(cm == 0, axis=1)
errors = errors[non_zero_cm_rows]
cm = cm[non_zero_cm_rows]

per_class_maes = np.sum(errors, axis=1) / np.sum(cm, axis=1).astype("double")
per_class_maes = per_class_maes[~np.isnan(per_class_maes)]
return per_class_maes.max()


Expand Down
12 changes: 12 additions & 0 deletions orca_python/metrics/tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ def test_amae():
actual = amae(y_true, y_pred)
npt.assert_almost_equal(expected, actual, decimal=6)

y_true = np.array([0, 1, 2, 3, 3])
y_pred = np.array([0, 1, 2, 3, 4])
expected = 0.125
actual = amae(y_true, y_pred)
npt.assert_almost_equal(expected, actual, decimal=6)


def test_gm():
"""Test the Geometric Mean (GM) metric."""
Expand Down Expand Up @@ -147,6 +153,12 @@ def test_mmae():
actual = mmae(y_true, y_pred)
npt.assert_almost_equal(expected, actual, decimal=6)

y_true = np.array([0, 1, 2, 3, 3])
y_pred = np.array([0, 1, 2, 3, 4])
expected = 0.5
actual = mmae(y_true, y_pred)
npt.assert_almost_equal(expected, actual, decimal=6)


def test_ms():
"""Test the Mean Sensitivity (MS) metric."""
Expand Down