-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
added mean absolute percentage error #10464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ccf9d04
added mean absolute percentage error
Humzafazal72 9e39c41
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 548ed93
added mean_absolute_percentage_error
Humzafazal72 3741150
Merge branch 'master' of https://github.com/Humzafazal72/Python
Humzafazal72 cde334a
added mean_absolute_percentage_error
Humzafazal72 2e104c8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 138ad8a
added mean_absolute_percentage_error
Humzafazal72 d4ae365
Merge branch 'master' of https://github.com/Humzafazal72/Python
Humzafazal72 031463d
added mean_absolute_percentage_error
Humzafazal72 c7c9b16
Merge branch 'TheAlgorithms:master' into master
Humzafazal72 2ddedeb
added mean absolute percentage error
Humzafazal72 7cbb3ec
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f7d9fe2
added mean absolute percentage error
Humzafazal72 28ab8a4
Merge branch 'master' of https://github.com/Humzafazal72/Python
Humzafazal72 6b59a3f
added mean absolute percentage error
Humzafazal72 ca9576d
added mean absolute percentage error
Humzafazal72 3d3b400
added mean absolute percentage error
Humzafazal72 de7bd3e
added mean absolute percentage error
Humzafazal72 3f9062b
Update machine_learning/loss_functions.py
tianyizheng02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
machine_learning/loss_functions/mean_absolute_percentage_error.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import numpy as np | ||
|
||
""" | ||
Mean Absolute Percentage Error : | ||
It calculates the average of the absolute percentage differences between the | ||
predicted and true values. | ||
|
||
Formula = (Σ|y_true[i]-Y_pred[i]/y_true[i]|)/n | ||
|
||
https://stephenallwright.com/good-mape-score/ | ||
|
||
""" | ||
|
||
|
||
def mean_absolute_percentage_error(y_true: np.ndarray, y_pred: np.ndarray) -> float: | ||
""" | ||
Calculate the Mean Absolute Percentage Error between y_true and y_pred. | ||
|
||
Parameters: | ||
y_true (np.ndarray): Numpy array containing true/target values. | ||
y_pred (np.ndarray): Numpy array containing predicted values. | ||
|
||
Returns: | ||
float: The Mean Absolute Percentage error between y_true and y_pred. | ||
|
||
Examples: | ||
>>> y_true = np.array([10, 20, 30, 40]) | ||
>>> y_pred = np.array([12, 18, 33, 45]) | ||
>>> mean_absolute_percentage_error(y_true, y_pred) | ||
13.124999992135416 | ||
|
||
>>> y_true = np.array([1, 2, 3, 4]) | ||
>>> y_pred = np.array([2, 3, 4, 5]) | ||
>>> mean_absolute_percentage_error(y_true, y_pred) | ||
52.083332977430565 | ||
|
||
>>> y_true = np.array([5, 0, 10, 20]) | ||
>>> y_pred = np.array([5, 0, 9, 15]) | ||
>>> mean_absolute_percentage_error(y_true, y_pred) | ||
8.749999994374999 | ||
""" | ||
try: | ||
if len(y_true) != len(y_pred): | ||
error_message = "the length of the two arrays should be same." | ||
raise ValueError(error_message) | ||
|
||
# Calculate the absolute percentage difference between y_true and y_pred | ||
# added 1e-9 to avoid division by 0 (smoothing). | ||
absolute_percentage_diff = np.abs((y_true - y_pred) / (y_true + 0.00000001)) | ||
|
||
# Calculate the mean and multiply by 100 for percentage. | ||
error = np.mean(absolute_percentage_diff) * 100 | ||
|
||
return error | ||
|
||
except ValueError as e: | ||
raise e | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
machine_learning/loss_functions/mean_absolute_percentage_error.py
, please provide doctest for the functionmean_absolute_percentage_error