Skip to content

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 19 commits into from
Oct 25, 2023
Merged
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
ccf9d04
added mean absolute percentage error
Humzafazal72 Oct 14, 2023
9e39c41
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 14, 2023
548ed93
added mean_absolute_percentage_error
Humzafazal72 Oct 14, 2023
3741150
Merge branch 'master' of https://github.com/Humzafazal72/Python
Humzafazal72 Oct 14, 2023
cde334a
added mean_absolute_percentage_error
Humzafazal72 Oct 14, 2023
2e104c8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 14, 2023
138ad8a
added mean_absolute_percentage_error
Humzafazal72 Oct 14, 2023
d4ae365
Merge branch 'master' of https://github.com/Humzafazal72/Python
Humzafazal72 Oct 14, 2023
031463d
added mean_absolute_percentage_error
Humzafazal72 Oct 14, 2023
c7c9b16
Merge branch 'TheAlgorithms:master' into master
Humzafazal72 Oct 24, 2023
2ddedeb
added mean absolute percentage error
Humzafazal72 Oct 24, 2023
7cbb3ec
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 24, 2023
f7d9fe2
added mean absolute percentage error
Humzafazal72 Oct 24, 2023
28ab8a4
Merge branch 'master' of https://github.com/Humzafazal72/Python
Humzafazal72 Oct 24, 2023
6b59a3f
added mean absolute percentage error
Humzafazal72 Oct 24, 2023
ca9576d
added mean absolute percentage error
Humzafazal72 Oct 24, 2023
3d3b400
added mean absolute percentage error
Humzafazal72 Oct 25, 2023
de7bd3e
added mean absolute percentage error
Humzafazal72 Oct 25, 2023
3f9062b
Update machine_learning/loss_functions.py
tianyizheng02 Oct 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions machine_learning/loss_functions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,58 @@
import numpy as np


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.

Mean Absolute Percentage Error 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

Source: https://stephenallwright.com/good-mape-score/

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


def binary_cross_entropy(
y_true: np.ndarray, y_pred: np.ndarray, epsilon: float = 1e-15
) -> float:
Expand Down