Skip to content

Fix ValueError in macro_averaged_mean_absolute_error for missing classes (#1094)#1168

Open
prodduturisindhurdy wants to merge 1 commit intoscikit-learn-contrib:masterfrom
prodduturisindhurdy:fix-mae-crash-1094
Open

Fix ValueError in macro_averaged_mean_absolute_error for missing classes (#1094)#1168
prodduturisindhurdy wants to merge 1 commit intoscikit-learn-contrib:masterfrom
prodduturisindhurdy:fix-mae-crash-1094

Conversation

@prodduturisindhurdy
Copy link
Copy Markdown

Problem
The function macro_averaged_mean_absolute_error currently crashes with a ValueError (Found array with 0 samples) when a label exists in the predictions (y_pred) or the global labels set, but is entirely absent from the ground truth (y_true). This happens because the function attempts to calculate mean_absolute_error on an empty slice of data.

Solution
This PR introduces a defensive check within the label iteration loop:

Skip Empty Classes: If a class has zero occurrences in y_true, the loop now continues to the next label instead of attempting a calculation on empty arrays.

Robust Return: Changed the final return to use np.mean(mae) with a fallback to 0.0. This prevents a potential ZeroDivisionError if all classes were somehow skipped.

Changes
Modified imblearn/metrics/_classification.py to add the if len(indices) == 0: continue safety guard.

Added a new regression test test_macro_averaged_mean_absolute_error_missing_class in imblearn/metrics/tests/test_classification.py to prevent future regressions.

Verification
Manual Test: Verified that the reproduction script provided in #1094 now returns 0.5 instead of crashing.

Automated Tests: Ran pytest imblearn/metrics/tests/test_classification.py and all 52 tests passed.

Fixes #1094

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] macro_averaged_mean_absolute_error() raises ValueError

1 participant