Skip to content

Commit

Permalink
add errors
Browse files Browse the repository at this point in the history
  • Loading branch information
xyluo25 committed Apr 10, 2024
1 parent b3dd6f9 commit 63c805c
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 1 deletion.
19 changes: 18 additions & 1 deletion pyufunc/util_ai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,22 @@
# Author/Copyright: Mr. Xiangyong Luo
##############################################################

from ._error_measure import (mean_absolute_error,
mean_squared_error,
root_mean_squared_error,
mean_squared_log_error,
mean_absolute_percentage_error,
mean_percentage_error,
r2_score,
)

__all__ = []
__all__ = [
# error measure
"mean_absolute_error",
"mean_squared_error",
"root_mean_squared_error",
"mean_squared_log_error",
"mean_absolute_percentage_error",
"mean_percentage_error",
"r2_score",
]
239 changes: 239 additions & 0 deletions pyufunc/util_ai/_error_measure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
# -*- coding:utf-8 -*-
##############################################################
# Created Date: Tuesday, April 9th 2024
# Contact Info: [email protected]
# Author/Copyright: Mr. Xiangyong Luo
##############################################################
from __future__ import annotations
from typing import TYPE_CHECKING, Iterable
from pyufunc.pkg_utils import requires, import_package
if TYPE_CHECKING:
import numpy as np


@requires("numpy", verbose=False)
def mean_absolute_error(y_true: Iterable, y_pred: Iterable) -> float:
"""Calculate mean absolute error between y_true and y_pred
Args:
y_true: Iterable, The true values
y_pred: Iterable, The predicted values
Returns:
float, The mean absolute error
Example:
>>> from pyufunc import mean_absolute_error
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> mean_absolute_error(y_true, y_pred)
0.5
"""
import_package("numpy", verbose=False)
import numpy as np

# TDD: check if y_true and y_pred are iterable
if not isinstance(y_true, Iterable):
raise TypeError("Input should be an iterable object")
if not isinstance(y_pred, Iterable):
raise TypeError("Input should be an iterable object")

# covert to numpy array if not
y_true = np.array(y_true)
y_pred = np.array(y_pred)

return np.mean(np.abs(y_true - y_pred))


@requires("numpy", verbose=False)
def mean_squared_error(y_true: Iterable, y_pred: Iterable) -> float:
"""Calculate mean squared error between y_true and y_pred
Args:
y_true: Iterable, The true values
y_pred: Iterable, The predicted values
Returns:
float, The mean squared error
Example:
>>> from pyufunc import mean_squared_error
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> mean_squared_error(y_true, y_pred)
0.375
"""
import_package("numpy", verbose=False)
import numpy as np

# TDD: check if y_true and y_pred are iterable
if not isinstance(y_true, Iterable):
raise TypeError("Input should be an iterable object")
if not isinstance(y_pred, Iterable):
raise TypeError("Input should be an iterable object")

# covert to numpy array if not
y_true = np.array(y_true)
y_pred = np.array(y_pred)

return np.mean(np.square(y_true - y_pred))


@requires("numpy", verbose=False)
def mean_squared_log_error(y_true: Iterable, y_pred: Iterable) -> float:
"""Calculate mean squared log error between y_true and y_pred
Args:
y_true: Iterable, The true values
y_pred: Iterable, The predicted values
Returns:
float, The mean squared log error
Example:
>>> from pyufunc import mean_squared_log_error
>>> y_true = [3, 5, 2.5, 7]
>>> y_pred = [2.5, 5, 4, 8]
>>> mean_squared_log_error(y_true, y_pred)
0.03973012298459379
"""
import_package("numpy", verbose=False)
import numpy as np

# TDD: check if y_true and y_pred are iterable
if not isinstance(y_true, Iterable):
raise TypeError("Input should be an iterable object")
if not isinstance(y_pred, Iterable):
raise TypeError("Input should be an iterable object")

# covert to numpy array if not
y_true = np.array(y_true)
y_pred = np.array(y_pred)

return np.mean(np.square(np.log1p(y_true) - np.log1p(y_pred)))


@requires("numpy", verbose=False)
def root_mean_squared_error(y_true: Iterable, y_pred: Iterable) -> float:
"""Calculate root mean squared error between y_true and y_pred
Args:
y_true: Iterable, The true values
y_pred: Iterable, The predicted values
Returns:
float, The root mean squared error
Example:
>>> from pyufunc import root_mean_squared_error
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> root_mean_squared_error(y_true, y_pred)
0.6123724356957945
"""
import_package("numpy", verbose=False)
import numpy as np

# TDD: check if y_true and y_pred are iterable
if not isinstance(y_true, Iterable):
raise TypeError("Input should be an iterable object")
if not isinstance(y_pred, Iterable):
raise TypeError("Input should be an iterable object")

# covert to numpy array if not
y_true = np.array(y_true)
y_pred = np.array(y_pred)

return np.sqrt(np.mean(np.square(y_true - y_pred)))


@requires("numpy", verbose=False)
def mean_absolute_percentage_error(y_true: Iterable, y_pred: Iterable) -> float:
"""Calculate mean absolute percentage error between y_true and y_pred
Args:
y_true: Iterable, The true values
y_pred: Iterable, The predicted values
Returns:
float, The mean absolute percentage error
Example:
>>> from pyufunc import mean_absolute_percentage_error
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> mean_absolute_percentage_error(y_true, y_pred)
0.3273809523809524
"""
import_package("numpy", verbose=False)
import numpy as np

# TDD: check if y_true and y_pred are iterable
if not isinstance(y_true, Iterable):
raise TypeError("Input should be an iterable object")
if not isinstance(y_pred, Iterable):
raise TypeError("Input should be an iterable object")

# covert to numpy array if not
y_true = np.array(y_true)
y_pred = np.array(y_pred)

return np.mean(np.abs((y_true - y_pred) / y_true))


@requires("numpy", verbose=False)
def mean_percentage_error(y_true: Iterable, y_pred: Iterable) -> float:
"""Calculate mean percentage error between y_true and y_pred
Args:
y_true: Iterable, The true values
y_pred: Iterable, The predicted values
Returns:
float, The mean percentage error
Example:
>>> from pyufunc import mean_percentage_error
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> mean_percentage_error(y_true, y_pred)
0.08134920634920635
"""
import_package("numpy", verbose=False)
import numpy as np

# TDD: check if y_true and y_pred are iterable
if not isinstance(y_true, Iterable):
raise TypeError("Input should be an iterable object")
if not isinstance(y_pred, Iterable):
raise TypeError("Input should be an iterable object")

# covert to numpy array if not
y_true = np.array(y_true)
y_pred = np.array(y_pred)

return np.mean((y_true - y_pred) / y_true)


@requires("numpy", verbose=False)
def r2_score(y_true: Iterable, y_pred: Iterable) -> float:
"""Calculate R^2 (coefficient of determination) regression score function.
Args:
y_true: Iterable, The true values
y_pred: Iterable, The predicted values
Returns:
float, The R^2 score
Example:
>>> from pyufunc import r2_score
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> r2_score(y_true, y_pred)
0.9486081370449679
"""
import_package("numpy", verbose=False)
import numpy as np

# TDD: check if y_true and y_pred are iterable
if not isinstance(y_true, Iterable):
raise TypeError("Input should be an iterable object")
if not isinstance(y_pred, Iterable):
raise TypeError("Input should be an iterable object")

# covert to numpy array if not
y_true = np.array(y_true)
y_pred = np.array(y_pred)

mean_y = np.mean(y_true)
ss_res = np.sum(np.square(y_true - y_pred))
ss_tot = np.sum(np.square(y_true - mean_y))

return 1 - ss_res / ss_tot
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# # we mentioned this package relay no denpendencies
# # only built-in packages in python are used
# # actually, some third-party packages
# # such as pandas, numpy are widely used
# # we consider them as built-in packages
# pandas
# numpy

0 comments on commit 63c805c

Please sign in to comment.