From ccf9d046024a22add413752d2270efa79d55e6c8 Mon Sep 17 00:00:00 2001 From: Humza Date: Sat, 14 Oct 2023 23:41:21 +0500 Subject: [PATCH 01/15] added mean absolute percentage error --- .../mean_absolute_percentage_error.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 machine_learning/loss_functions/mean_absolute_percentage_error.py diff --git a/machine_learning/loss_functions/mean_absolute_percentage_error.py b/machine_learning/loss_functions/mean_absolute_percentage_error.py new file mode 100644 index 000000000000..1e9768d751e0 --- /dev/null +++ b/machine_learning/loss_functions/mean_absolute_percentage_error.py @@ -0,0 +1,48 @@ +import numpy as np +import doctest + +""" +Mean Absolute Percentage Error (MAPE): +MAPE calculates the average of the absolute percentage differences between the +predicted and true values. + +MAPE = (Σ|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 (MAPE) 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 MAPE between y_true and y_pred. + """ + try: + if len(y_true) != len(y_pred): + raise ValueError( + f"The length of the target array ({len(y_true)}) and" + f" the predicted array ({len(y_pred)}) are not the same." + ) + + # Calculate the absolute percentage difference between y_true and y_pred + # addded 1e-9 to avoid divison by 0 (smoothing). + absolute_percentage_diff = np.abs((y_true - y_pred) / (y_true + 1e-9)) + + # Calculate the mean and multiply by 100 for percentage. + mape = np.mean(absolute_percentage_diff) * 100 + + return mape + + except ValueError as e: + raise e + + +if __name__ == "__main__": + doctest.testmod() From 9e39c41eb8da18b00eba72bf8fc5445125492857 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 14 Oct 2023 18:51:19 +0000 Subject: [PATCH 02/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../loss_functions/mean_absolute_percentage_error.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/loss_functions/mean_absolute_percentage_error.py b/machine_learning/loss_functions/mean_absolute_percentage_error.py index 1e9768d751e0..dc2937ee0921 100644 --- a/machine_learning/loss_functions/mean_absolute_percentage_error.py +++ b/machine_learning/loss_functions/mean_absolute_percentage_error.py @@ -2,7 +2,7 @@ import doctest """ -Mean Absolute Percentage Error (MAPE): +Mean Absolute Percentage Error (MAPE): MAPE calculates the average of the absolute percentage differences between the predicted and true values. From 548ed935b68e25f4787760407e9fc8659936d4f6 Mon Sep 17 00:00:00 2001 From: Humza Date: Sat, 14 Oct 2023 23:57:11 +0500 Subject: [PATCH 03/15] added mean_absolute_percentage_error --- .../mean_absolute_percentage_error.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/machine_learning/loss_functions/mean_absolute_percentage_error.py b/machine_learning/loss_functions/mean_absolute_percentage_error.py index 1e9768d751e0..76fa3264558f 100644 --- a/machine_learning/loss_functions/mean_absolute_percentage_error.py +++ b/machine_learning/loss_functions/mean_absolute_percentage_error.py @@ -23,6 +23,22 @@ def mean_absolute_percentage_error(y_true: np.ndarray, y_pred: np.ndarray) -> fl Returns: float: The MAPE 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) + 9.722222222222221 + + >>> y_true = np.array([1, 2, 3, 4]) + >>> y_pred = np.array([2, 3, 4, 5]) + >>> mean_absolute_percentage_error(y_true, y_pred) + 25.0 + + >>> y_true = np.array([5, 0, 10, 20]) + >>> y_pred = np.array([5, 0, 9, 15]) + >>> mean_absolute_percentage_error(y_true, y_pred) + 18.75 """ try: if len(y_true) != len(y_pred): From cde334a1b12032d8f73d3cfb0af2224db19b1e8a Mon Sep 17 00:00:00 2001 From: Humza Date: Sun, 15 Oct 2023 00:15:45 +0500 Subject: [PATCH 04/15] added mean_absolute_percentage_error --- .../loss_functions/mean_absolute_percentage_error.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/machine_learning/loss_functions/mean_absolute_percentage_error.py b/machine_learning/loss_functions/mean_absolute_percentage_error.py index 5da50a1fe497..655ac5f93897 100644 --- a/machine_learning/loss_functions/mean_absolute_percentage_error.py +++ b/machine_learning/loss_functions/mean_absolute_percentage_error.py @@ -1,5 +1,4 @@ import numpy as np -import doctest """ Mean Absolute Percentage Error (MAPE): @@ -42,10 +41,8 @@ def mean_absolute_percentage_error(y_true: np.ndarray, y_pred: np.ndarray) -> fl """ try: if len(y_true) != len(y_pred): - raise ValueError( - f"The length of the target array ({len(y_true)}) and" - f" the predicted array ({len(y_pred)}) are not the same." - ) + error_message = "the lenght of the two arrays should be same." + raise ValueError(error_message) # Calculate the absolute percentage difference between y_true and y_pred # addded 1e-9 to avoid divison by 0 (smoothing). @@ -61,4 +58,5 @@ def mean_absolute_percentage_error(y_true: np.ndarray, y_pred: np.ndarray) -> fl if __name__ == "__main__": + import doctest doctest.testmod() From 2e104c854fa26394b51226a4e42b301c3464138b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 14 Oct 2023 19:16:57 +0000 Subject: [PATCH 05/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../loss_functions/mean_absolute_percentage_error.py | 1 + 1 file changed, 1 insertion(+) diff --git a/machine_learning/loss_functions/mean_absolute_percentage_error.py b/machine_learning/loss_functions/mean_absolute_percentage_error.py index 655ac5f93897..29e1f079949e 100644 --- a/machine_learning/loss_functions/mean_absolute_percentage_error.py +++ b/machine_learning/loss_functions/mean_absolute_percentage_error.py @@ -59,4 +59,5 @@ def mean_absolute_percentage_error(y_true: np.ndarray, y_pred: np.ndarray) -> fl if __name__ == "__main__": import doctest + doctest.testmod() From 138ad8a2d42dbdc41bfbc480e1dd030c867e3645 Mon Sep 17 00:00:00 2001 From: Humza Date: Sun, 15 Oct 2023 00:24:33 +0500 Subject: [PATCH 06/15] added mean_absolute_percentage_error --- .../mean_absolute_percentage_error.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/machine_learning/loss_functions/mean_absolute_percentage_error.py b/machine_learning/loss_functions/mean_absolute_percentage_error.py index 655ac5f93897..b592f8bfedd2 100644 --- a/machine_learning/loss_functions/mean_absolute_percentage_error.py +++ b/machine_learning/loss_functions/mean_absolute_percentage_error.py @@ -1,11 +1,11 @@ import numpy as np """ -Mean Absolute Percentage Error (MAPE): -MAPE calculates the average of the absolute percentage differences between the +Mean Absolute Percentage Error : +It calculates the average of the absolute percentage differences between the predicted and true values. -MAPE = (Σ|y_true[i]-Y_pred[i]/y_true[i]|)/n +Formula = (Σ|y_true[i]-Y_pred[i]/y_true[i]|)/n https://stephenallwright.com/good-mape-score/ @@ -14,14 +14,14 @@ def mean_absolute_percentage_error(y_true: np.ndarray, y_pred: np.ndarray) -> float: """ - Calculate the Mean Absolute Percentage Error (MAPE) between y_true and y_pred. + 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 MAPE between y_true and y_pred. + float: The Mean Absolute Percentage error between y_true and y_pred. Examples: >>> y_true = np.array([10, 20, 30, 40]) @@ -41,17 +41,17 @@ def mean_absolute_percentage_error(y_true: np.ndarray, y_pred: np.ndarray) -> fl """ try: if len(y_true) != len(y_pred): - error_message = "the lenght of the two arrays should be same." + 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 - # addded 1e-9 to avoid divison by 0 (smoothing). + # added 1e-9 to avoid division by 0 (smoothing). absolute_percentage_diff = np.abs((y_true - y_pred) / (y_true + 1e-9)) # Calculate the mean and multiply by 100 for percentage. - mape = np.mean(absolute_percentage_diff) * 100 + error = np.mean(absolute_percentage_diff) * 100 - return mape + return error except ValueError as e: raise e From 031463d7b95c0a37e7a2aae31da4e1f37b327f28 Mon Sep 17 00:00:00 2001 From: Humza Date: Sun, 15 Oct 2023 00:50:17 +0500 Subject: [PATCH 07/15] added mean_absolute_percentage_error --- .../loss_functions/mean_absolute_percentage_error.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/machine_learning/loss_functions/mean_absolute_percentage_error.py b/machine_learning/loss_functions/mean_absolute_percentage_error.py index fa23ad85b446..3e695602ef57 100644 --- a/machine_learning/loss_functions/mean_absolute_percentage_error.py +++ b/machine_learning/loss_functions/mean_absolute_percentage_error.py @@ -27,17 +27,17 @@ def mean_absolute_percentage_error(y_true: np.ndarray, y_pred: np.ndarray) -> fl >>> y_true = np.array([10, 20, 30, 40]) >>> y_pred = np.array([12, 18, 33, 45]) >>> mean_absolute_percentage_error(y_true, y_pred) - 9.722222222222221 + 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) - 25.0 + 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) - 18.75 + 8.749999994374999 """ try: if len(y_true) != len(y_pred): @@ -46,7 +46,7 @@ def mean_absolute_percentage_error(y_true: np.ndarray, y_pred: np.ndarray) -> fl # 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 + 1e-9)) + 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 From 2ddedeb031cb3502732ca375289f906f163fc502 Mon Sep 17 00:00:00 2001 From: Humzafazal72 Date: Tue, 24 Oct 2023 18:45:04 +0500 Subject: [PATCH 08/15] added mean absolute percentage error --- machine_learning/loss_functions.py | 56 +++++++++++++++++ .../mean_absolute_percentage_error.py | 63 ------------------- 2 files changed, 56 insertions(+), 63 deletions(-) delete mode 100644 machine_learning/loss_functions/mean_absolute_percentage_error.py diff --git a/machine_learning/loss_functions.py b/machine_learning/loss_functions.py index ef34296360e2..8876f8188db3 100644 --- a/machine_learning/loss_functions.py +++ b/machine_learning/loss_functions.py @@ -1,6 +1,62 @@ 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: diff --git a/machine_learning/loss_functions/mean_absolute_percentage_error.py b/machine_learning/loss_functions/mean_absolute_percentage_error.py deleted file mode 100644 index 3e695602ef57..000000000000 --- a/machine_learning/loss_functions/mean_absolute_percentage_error.py +++ /dev/null @@ -1,63 +0,0 @@ -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() From 7cbb3ec9cdc3a232d7eee52f6b82ab7b91392c88 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Oct 2023 13:45:49 +0000 Subject: [PATCH 09/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/loss_functions.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/machine_learning/loss_functions.py b/machine_learning/loss_functions.py index 8876f8188db3..7cc1755119bc 100644 --- a/machine_learning/loss_functions.py +++ b/machine_learning/loss_functions.py @@ -1,14 +1,11 @@ import numpy as np -def mean_absolute_percentage_error( - y_true: np.ndarray, - y_pred: np.ndarray - ) -> float: +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 + + 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 @@ -56,7 +53,6 @@ def mean_absolute_percentage_error( raise e - def binary_cross_entropy( y_true: np.ndarray, y_pred: np.ndarray, epsilon: float = 1e-15 ) -> float: From f7d9fe2e2c954f0dadc49ed285c569348d3712fc Mon Sep 17 00:00:00 2001 From: Humzafazal72 Date: Tue, 24 Oct 2023 18:49:59 +0500 Subject: [PATCH 10/15] added mean absolute percentage error --- machine_learning/loss_functions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/machine_learning/loss_functions.py b/machine_learning/loss_functions.py index 8876f8188db3..4202b1c67337 100644 --- a/machine_learning/loss_functions.py +++ b/machine_learning/loss_functions.py @@ -7,8 +7,8 @@ def mean_absolute_percentage_error( ) -> float: """ Calculate the Mean Absolute Percentage Error between y_true and y_pred. - - Mean Absolute Percentage Error calculates the average of the absolute + + 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 From 6b59a3f008d9aa8d32f8bc06faf2a5f21f5488cb Mon Sep 17 00:00:00 2001 From: Humzafazal72 Date: Tue, 24 Oct 2023 18:54:16 +0500 Subject: [PATCH 11/15] added mean absolute percentage error --- machine_learning/loss_functions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/loss_functions.py b/machine_learning/loss_functions.py index 057af52334fe..7cc1755119bc 100644 --- a/machine_learning/loss_functions.py +++ b/machine_learning/loss_functions.py @@ -5,7 +5,7 @@ def mean_absolute_percentage_error(y_true: np.ndarray, y_pred: np.ndarray) -> fl """ Calculate the Mean Absolute Percentage Error between y_true and y_pred. - Mean Absolute Percentage Error calculates the average of the absolute. + 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 From ca9576d3fbd1b0d56fcf83a2b28979e1e8a33c82 Mon Sep 17 00:00:00 2001 From: Humzafazal72 Date: Tue, 24 Oct 2023 21:54:28 +0500 Subject: [PATCH 12/15] added mean absolute percentage error --- machine_learning/loss_functions.py | 98 ++++++++++++++---------------- 1 file changed, 46 insertions(+), 52 deletions(-) diff --git a/machine_learning/loss_functions.py b/machine_learning/loss_functions.py index 7cc1755119bc..3540ca1167f3 100644 --- a/machine_learning/loss_functions.py +++ b/machine_learning/loss_functions.py @@ -1,58 +1,6 @@ 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: @@ -349,6 +297,52 @@ def mean_squared_logarithmic_error(y_true: np.ndarray, y_pred: np.ndarray) -> fl return np.mean(squared_logarithmic_errors) +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 + """ + if len(y_true) != len(y_pred): + raise ValueError("The length of the two arrays should be the same.") + + # 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 + + if __name__ == "__main__": import doctest From 3d3b4003be649ef8fb4cb497ef5513c9d20f5770 Mon Sep 17 00:00:00 2001 From: Humzafazal72 Date: Wed, 25 Oct 2023 10:17:18 +0500 Subject: [PATCH 13/15] added mean absolute percentage error --- machine_learning/loss_functions.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/machine_learning/loss_functions.py b/machine_learning/loss_functions.py index 3540ca1167f3..1d2cde1d8cd9 100644 --- a/machine_learning/loss_functions.py +++ b/machine_learning/loss_functions.py @@ -297,7 +297,9 @@ def mean_squared_logarithmic_error(y_true: np.ndarray, y_pred: np.ndarray) -> fl return np.mean(squared_logarithmic_errors) -def mean_absolute_percentage_error(y_true: np.ndarray, y_pred: np.ndarray) -> float: +def mean_absolute_percentage_error( + y_true: np.ndarray, y_pred: np.ndarray, epsilon: float = 1e-9 +) -> float: """ Calculate the Mean Absolute Percentage Error between y_true and y_pred. @@ -319,28 +321,27 @@ def mean_absolute_percentage_error(y_true: np.ndarray, y_pred: np.ndarray) -> fl >>> 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 + 0.13124999992135416 >>> 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 + 0.52083332977430565 >>> 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 + 0.08749999994374999 """ if len(y_true) != len(y_pred): raise ValueError("The length of the two arrays should be the same.") + y_true = np.where(y_true == 0, epsilon, y_true) # 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)) + absolute_percentage_diff = np.abs((y_true - y_pred) / y_true) - # Calculate the mean and multiply by 100 for percentage. - error = np.mean(absolute_percentage_diff) * 100 - return error + # Calculate the mean. + return np.mean(absolute_percentage_diff) if __name__ == "__main__": From de7bd3ebde416a6f762e4c9699ece1001afa1f4d Mon Sep 17 00:00:00 2001 From: Humzafazal72 Date: Wed, 25 Oct 2023 10:48:24 +0500 Subject: [PATCH 14/15] added mean absolute percentage error --- machine_learning/loss_functions.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/machine_learning/loss_functions.py b/machine_learning/loss_functions.py index 1d2cde1d8cd9..18169149cc98 100644 --- a/machine_learning/loss_functions.py +++ b/machine_learning/loss_functions.py @@ -298,7 +298,7 @@ def mean_squared_logarithmic_error(y_true: np.ndarray, y_pred: np.ndarray) -> fl def mean_absolute_percentage_error( - y_true: np.ndarray, y_pred: np.ndarray, epsilon: float = 1e-9 + y_true: np.ndarray, y_pred: np.ndarray, epsilon: float = 1e-15 ) -> float: """ Calculate the Mean Absolute Percentage Error between y_true and y_pred. @@ -321,17 +321,17 @@ def mean_absolute_percentage_error( >>> y_true = np.array([10, 20, 30, 40]) >>> y_pred = np.array([12, 18, 33, 45]) >>> mean_absolute_percentage_error(y_true, y_pred) - 0.13124999992135416 + 0.13125 >>> y_true = np.array([1, 2, 3, 4]) >>> y_pred = np.array([2, 3, 4, 5]) >>> mean_absolute_percentage_error(y_true, y_pred) - 0.52083332977430565 + 0.5208333333333333 - >>> y_true = np.array([5, 0, 10, 20]) - >>> y_pred = np.array([5, 0, 9, 15]) + >>> y_true = np.array([34, 37, 44, 47, 48, 48, 46, 43, 32, 27, 26, 24]) + >>> y_pred = np.array([37, 40, 46, 44, 46, 50, 45, 44, 34, 30, 22, 23]) >>> mean_absolute_percentage_error(y_true, y_pred) - 0.08749999994374999 + 0.064671076436071 """ if len(y_true) != len(y_pred): raise ValueError("The length of the two arrays should be the same.") From 3f9062b967cef1d14d38986a254ea339399f2a3e Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Wed, 25 Oct 2023 19:00:28 -0400 Subject: [PATCH 15/15] Update machine_learning/loss_functions.py --- machine_learning/loss_functions.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/machine_learning/loss_functions.py b/machine_learning/loss_functions.py index 18169149cc98..e5b7a713b6f2 100644 --- a/machine_learning/loss_functions.py +++ b/machine_learning/loss_functions.py @@ -337,10 +337,8 @@ def mean_absolute_percentage_error( raise ValueError("The length of the two arrays should be the same.") y_true = np.where(y_true == 0, epsilon, y_true) - # Calculate the absolute percentage difference between y_true and y_pred absolute_percentage_diff = np.abs((y_true - y_pred) / y_true) - # Calculate the mean. return np.mean(absolute_percentage_diff)