Adds MAPE function to Loss functions - #13357
Open
morgen-code wants to merge 3 commits into
Open
Conversation
15 tasks
priya-sundaram-dev
suggested changes
Sep 10, 2026
priya-sundaram-dev
left a comment
Contributor
There was a problem hiding this comment.
Thanks for the PR! The math is right, but CI is red (ruff / pre-commit / build) and there are a few copy-paste issues to fix before this can go in:
- Doctests call the wrong function. They invoke
symmetric_mean_absolute_percentage_error(...), but this PR definesmean_absolute_percentage_error. So the doctests are actually testing SMAPE, not MAPE. Change the calls tomean_absolute_percentage_error. (The expected value0.058333…is in fact correct for MAPE on that data — 0.1, 0.05, 0.0333, 0.05 averaged — so just fixing the function name makes it pass.) - Docstring formula/name. It reads
SMAPE = …; it should sayMAPE = (1/n) * Σ( |y_true - y_pred| / |y_true| ). - Indentation. The docstring body is over-indented with mixed leading whitespace, which is part of the lint failure. Align everything to 4 spaces under the opening
""". - Line length. The
>>> float(mean_absolute_percentage_error(true_values, predicted_values))line will likely exceed the 88-char limit — shorten the local variable names (e.g.true/pred) so it fits.
Once those are fixed the doctests and ruff should pass. Also heads-up: #13355 (SMAPE) touches the same block of loss_functions.py, so a rebase may be needed depending on merge order.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your change:
Fixes #13311
Adds the algorithm to calculate
mean_absolute_percentage_errorinloss_functions.pyfile inmachine_learningdirectory.Checklist: