Skip to content

Adds MAPE function to Loss functions - #13357

Open
morgen-code wants to merge 3 commits into
TheAlgorithms:masterfrom
morgen-code:master
Open

Adds MAPE function to Loss functions#13357
morgen-code wants to merge 3 commits into
TheAlgorithms:masterfrom
morgen-code:master

Conversation

@morgen-code

Copy link
Copy Markdown

Describe your change:

Fixes #13311
Adds the algorithm to calculate mean_absolute_percentage_error in loss_functions.py file in machine_learning directory.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass labels Sep 10, 2026

@priya-sundaram-dev priya-sundaram-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Doctests call the wrong function. They invoke symmetric_mean_absolute_percentage_error(...), but this PR defines mean_absolute_percentage_error. So the doctests are actually testing SMAPE, not MAPE. Change the calls to mean_absolute_percentage_error. (The expected value 0.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.)
  2. Docstring formula/name. It reads SMAPE = …; it should say MAPE = (1/n) * Σ( |y_true - y_pred| / |y_true| ).
  3. 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 """.
  4. 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.

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

Labels

awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding SMAPE and MAPE loss functions in Machine Learning

3 participants