Adds MAPE function to Loss functions - #13357
morgen-code wants to merge 3 commits into
Conversation
priya-sundaram-dev
left a comment
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.
|
@morgen-code @priya-sundaram-dev Can either of you manage this rebase? |
|
Before anyone spends time on the rebase — heads up that this one looks superseded. So rather than rebasing, I think #13357 can be closed as a duplicate. Two small notes for the record, in case anything from here is worth folding into the existing function:
Thanks @morgen-code for the contribution and the interest in the loss-function module! Happy to help if you'd like to pick up a different addition. @cclauss let me know if you'd prefer I do anything else here. |
Describe your change:
Fixes #13311
Adds the algorithm to calculate
mean_absolute_percentage_errorinloss_functions.pyfile inmachine_learningdirectory.Checklist: