kneeliverse.metrics

The following module provides a set of methods used for computing regression based metrics.

  1# coding: utf-8
  2
  3'''
  4The following module provides a set of methods
  5used for computing regression based metrics.
  6'''
  7
  8__author__ = 'Mário Antunes'
  9__version__ = '1.0'
 10__email__ = 'mario.antunes@ua.pt'
 11__status__ = 'Development'
 12__license__ = 'MIT'
 13__copyright__ = '''
 14Copyright (c) 2021-2023 Stony Brook University
 15Copyright (c) 2021-2023 The Research Foundation of SUNY
 16'''
 17
 18import enum
 19import numpy as np
 20
 21
 22class Metrics(enum.Enum):
 23    """
 24    Enum that defines the different metrics linear functions.
 25    These metrics are used on the `knee.rdp` and `knee.multi_knee`. 
 26    """
 27    r2 = 'r2'
 28    rmspe = 'rmspe'
 29    rmsle = 'rmsle'
 30    rpd = 'rpd'
 31    smape = 'smape'
 32
 33    def __str__(self):
 34        return self.value
 35
 36
 37class R2(enum.Enum):
 38    """
 39    Enum that defines the types of coefficient of determination
 40    """
 41    adjusted = 'adjusted'
 42    classic = 'classic'
 43
 44    def __str__(self):
 45        return self.value
 46
 47
 48def r2(y: np.ndarray, y_hat: np.ndarray, r2: R2 = R2.classic) -> float:
 49    """
 50    Computes the coefficient of determination (R2).
 51
 52    Args:
 53        y (np.ndarray): the real value of the points in the y axis coordinates
 54        y_hat (np.ndarray): the predicted value of the points in the y axis coordinates
 55        r2 (R2): select the type of coefficient of determination (default: R2.classic)
 56
 57    Returns:
 58        float: coefficient of determination (R2)
 59    """
 60    y_mean = np.mean(y)
 61    rss = np.sum(np.square(y-y_hat))
 62    tss = np.sum(np.square(y-y_mean))
 63    rv = 0.0
 64
 65    if tss == 0:
 66        rv = 1.0 - rss
 67    else:
 68        rv = 1.0 - (rss/tss)
 69
 70    if r2 is R2.adjusted:
 71        rv = 1.0 - (1.0 - rv)*((len(y)-1)/(len(y)-2))
 72
 73    return rv
 74
 75
 76def rmse(y: np.ndarray, y_hat: np.ndarray) -> float:
 77    """
 78    Computes the Root Mean Squared Error (RMSE).
 79
 80    Args:
 81        y (np.ndarray): the real value of the points in the y axis coordinates
 82        y_hat (np.ndarray): the predicted value of the points in the y axis coordinates
 83
 84    Returns:
 85        float: Root Mean Squared Error (RMSE)
 86    """
 87    return np.sqrt(np.mean(np.square(y - y_hat)))
 88
 89
 90def rmsle(y: np.ndarray, y_hat: np.ndarray) -> float:
 91    """
 92    Computes the Root Mean Squared Log Error (RMSLE):
 93    $$
 94    RMSLE(y, \\hat{y}) = \\sqrt{\\frac{\\sum_{i=1}^{n}(\\log (y_i+1) - \\log (\\hat{y_i}+1))^2}{n}}
 95    $$
 96
 97    Args:
 98        y (np.ndarray): the real value of the points in the y axis coordinates
 99        y_hat (np.ndarray): the predicted value of the points in the y axis coordinates
100
101    Returns:
102        float: Root Mean Squared Log Error (RMSLE)
103    """
104    return np.sqrt(np.mean(np.square((np.log(y+1) - np.log(y_hat+1)))))
105
106
107def rmspe(y: np.ndarray, y_hat: np.ndarray, eps: float = 1e-16) -> float:
108    """
109    Computes the Root Mean Squared Percentage Error (RMSPE).
110
111    Args:
112        y (np.ndarray): the real value of the points in the y axis coordinates
113        y_hat (np.ndarray): the predicted value of the points in the y axis coordinates
114        eps (float): eps value to prevent division by zero (default: 1E-16)
115
116    Returns:
117        float: Root Mean Squared Percentage Error (RMSPE)
118    """
119    return np.sqrt(np.mean(np.square((y - y_hat) / (y+eps))))
120
121
122def rpd(y: np.ndarray, y_hat: np.ndarray, eps: float = 1e-16) -> float:
123    """
124    Computes the Relative Percentage Difference (RPD).
125
126    Args:
127        y (np.ndarray): the real value of the points in the y axis coordinates
128        y_hat (np.ndarray): the predicted value of the points in the y axis coordinates
129        eps (float): eps value to prevent division by zero (default: 1E-16)
130
131    Returns:
132        float: Relative Percentage Difference (RPD)
133    """
134    return np.mean(np.abs((y - y_hat) / (np.maximum(y, y_hat)+eps)))
135
136
137def residuals(y: np.ndarray, y_hat: np.ndarray) -> float:
138    """
139    Computes the residual error of the fit.
140
141    Args:
142        y (np.ndarray): the real value of the points in the y axis coordinates
143        y_hat (np.ndarray): the predicted value of the points in the y axis coordinates
144
145    Returns:
146        float: residual error of the fit
147    """
148    return np.sum(np.square((y-y_hat)))
149
150
151def smape(y: np.ndarray, y_hat: np.ndarray, eps: float = 1e-16) -> float:
152    """
153    Computes Symmetric Mean Absolute Percentage Error (SMAPE).
154
155    Args:
156        y (np.ndarray): the real value of the points in the y axis coordinates
157        y_hat (np.ndarray): the predicted value of the points in the y axis coordinates
158
159    Returns:
160        float: residual error of the fit
161    """
162    return np.mean(2.0 * np.abs(y_hat - y) / (np.abs(y) + np.abs(y_hat) + eps))
class Metrics(enum.Enum):
23class Metrics(enum.Enum):
24    """
25    Enum that defines the different metrics linear functions.
26    These metrics are used on the `knee.rdp` and `knee.multi_knee`. 
27    """
28    r2 = 'r2'
29    rmspe = 'rmspe'
30    rmsle = 'rmsle'
31    rpd = 'rpd'
32    smape = 'smape'
33
34    def __str__(self):
35        return self.value

Enum that defines the different metrics linear functions. These metrics are used on the knee.rdp and knee.multi_knee.

r2 = <Metrics.r2: 'r2'>
rmspe = <Metrics.rmspe: 'rmspe'>
rmsle = <Metrics.rmsle: 'rmsle'>
rpd = <Metrics.rpd: 'rpd'>
smape = <Metrics.smape: 'smape'>
Inherited Members
enum.Enum
name
value
class R2(enum.Enum):
38class R2(enum.Enum):
39    """
40    Enum that defines the types of coefficient of determination
41    """
42    adjusted = 'adjusted'
43    classic = 'classic'
44
45    def __str__(self):
46        return self.value

Enum that defines the types of coefficient of determination

adjusted = <R2.adjusted: 'adjusted'>
classic = <R2.classic: 'classic'>
Inherited Members
enum.Enum
name
value
def r2( y: numpy.ndarray, y_hat: numpy.ndarray, r2: R2 = <R2.classic: 'classic'>) -> float:
49def r2(y: np.ndarray, y_hat: np.ndarray, r2: R2 = R2.classic) -> float:
50    """
51    Computes the coefficient of determination (R2).
52
53    Args:
54        y (np.ndarray): the real value of the points in the y axis coordinates
55        y_hat (np.ndarray): the predicted value of the points in the y axis coordinates
56        r2 (R2): select the type of coefficient of determination (default: R2.classic)
57
58    Returns:
59        float: coefficient of determination (R2)
60    """
61    y_mean = np.mean(y)
62    rss = np.sum(np.square(y-y_hat))
63    tss = np.sum(np.square(y-y_mean))
64    rv = 0.0
65
66    if tss == 0:
67        rv = 1.0 - rss
68    else:
69        rv = 1.0 - (rss/tss)
70
71    if r2 is R2.adjusted:
72        rv = 1.0 - (1.0 - rv)*((len(y)-1)/(len(y)-2))
73
74    return rv

Computes the coefficient of determination (R2).

Arguments:
  • y (np.ndarray): the real value of the points in the y axis coordinates
  • y_hat (np.ndarray): the predicted value of the points in the y axis coordinates
  • r2 (R2): select the type of coefficient of determination (default: R2.classic)
Returns:

float: coefficient of determination (R2)

def rmse(y: numpy.ndarray, y_hat: numpy.ndarray) -> float:
77def rmse(y: np.ndarray, y_hat: np.ndarray) -> float:
78    """
79    Computes the Root Mean Squared Error (RMSE).
80
81    Args:
82        y (np.ndarray): the real value of the points in the y axis coordinates
83        y_hat (np.ndarray): the predicted value of the points in the y axis coordinates
84
85    Returns:
86        float: Root Mean Squared Error (RMSE)
87    """
88    return np.sqrt(np.mean(np.square(y - y_hat)))

Computes the Root Mean Squared Error (RMSE).

Arguments:
  • y (np.ndarray): the real value of the points in the y axis coordinates
  • y_hat (np.ndarray): the predicted value of the points in the y axis coordinates
Returns:

float: Root Mean Squared Error (RMSE)

def rmsle(y: numpy.ndarray, y_hat: numpy.ndarray) -> float:
 91def rmsle(y: np.ndarray, y_hat: np.ndarray) -> float:
 92    """
 93    Computes the Root Mean Squared Log Error (RMSLE):
 94    $$
 95    RMSLE(y, \\hat{y}) = \\sqrt{\\frac{\\sum_{i=1}^{n}(\\log (y_i+1) - \\log (\\hat{y_i}+1))^2}{n}}
 96    $$
 97
 98    Args:
 99        y (np.ndarray): the real value of the points in the y axis coordinates
100        y_hat (np.ndarray): the predicted value of the points in the y axis coordinates
101
102    Returns:
103        float: Root Mean Squared Log Error (RMSLE)
104    """
105    return np.sqrt(np.mean(np.square((np.log(y+1) - np.log(y_hat+1)))))

Computes the Root Mean Squared Log Error (RMSLE): $$ RMSLE(y, \hat{y}) = \sqrt{\frac{\sum_{i=1}^{n}(\log (y_i+1) - \log (\hat{y_i}+1))^2}{n}} $$

Arguments:
  • y (np.ndarray): the real value of the points in the y axis coordinates
  • y_hat (np.ndarray): the predicted value of the points in the y axis coordinates
Returns:

float: Root Mean Squared Log Error (RMSLE)

def rmspe(y: numpy.ndarray, y_hat: numpy.ndarray, eps: float = 1e-16) -> float:
108def rmspe(y: np.ndarray, y_hat: np.ndarray, eps: float = 1e-16) -> float:
109    """
110    Computes the Root Mean Squared Percentage Error (RMSPE).
111
112    Args:
113        y (np.ndarray): the real value of the points in the y axis coordinates
114        y_hat (np.ndarray): the predicted value of the points in the y axis coordinates
115        eps (float): eps value to prevent division by zero (default: 1E-16)
116
117    Returns:
118        float: Root Mean Squared Percentage Error (RMSPE)
119    """
120    return np.sqrt(np.mean(np.square((y - y_hat) / (y+eps))))

Computes the Root Mean Squared Percentage Error (RMSPE).

Arguments:
  • y (np.ndarray): the real value of the points in the y axis coordinates
  • y_hat (np.ndarray): the predicted value of the points in the y axis coordinates
  • eps (float): eps value to prevent division by zero (default: 1E-16)
Returns:

float: Root Mean Squared Percentage Error (RMSPE)

def rpd(y: numpy.ndarray, y_hat: numpy.ndarray, eps: float = 1e-16) -> float:
123def rpd(y: np.ndarray, y_hat: np.ndarray, eps: float = 1e-16) -> float:
124    """
125    Computes the Relative Percentage Difference (RPD).
126
127    Args:
128        y (np.ndarray): the real value of the points in the y axis coordinates
129        y_hat (np.ndarray): the predicted value of the points in the y axis coordinates
130        eps (float): eps value to prevent division by zero (default: 1E-16)
131
132    Returns:
133        float: Relative Percentage Difference (RPD)
134    """
135    return np.mean(np.abs((y - y_hat) / (np.maximum(y, y_hat)+eps)))

Computes the Relative Percentage Difference (RPD).

Arguments:
  • y (np.ndarray): the real value of the points in the y axis coordinates
  • y_hat (np.ndarray): the predicted value of the points in the y axis coordinates
  • eps (float): eps value to prevent division by zero (default: 1E-16)
Returns:

float: Relative Percentage Difference (RPD)

def residuals(y: numpy.ndarray, y_hat: numpy.ndarray) -> float:
138def residuals(y: np.ndarray, y_hat: np.ndarray) -> float:
139    """
140    Computes the residual error of the fit.
141
142    Args:
143        y (np.ndarray): the real value of the points in the y axis coordinates
144        y_hat (np.ndarray): the predicted value of the points in the y axis coordinates
145
146    Returns:
147        float: residual error of the fit
148    """
149    return np.sum(np.square((y-y_hat)))

Computes the residual error of the fit.

Arguments:
  • y (np.ndarray): the real value of the points in the y axis coordinates
  • y_hat (np.ndarray): the predicted value of the points in the y axis coordinates
Returns:

float: residual error of the fit

def smape(y: numpy.ndarray, y_hat: numpy.ndarray, eps: float = 1e-16) -> float:
152def smape(y: np.ndarray, y_hat: np.ndarray, eps: float = 1e-16) -> float:
153    """
154    Computes Symmetric Mean Absolute Percentage Error (SMAPE).
155
156    Args:
157        y (np.ndarray): the real value of the points in the y axis coordinates
158        y_hat (np.ndarray): the predicted value of the points in the y axis coordinates
159
160    Returns:
161        float: residual error of the fit
162    """
163    return np.mean(2.0 * np.abs(y_hat - y) / (np.abs(y) + np.abs(y_hat) + eps))

Computes Symmetric Mean Absolute Percentage Error (SMAPE).

Arguments:
  • y (np.ndarray): the real value of the points in the y axis coordinates
  • y_hat (np.ndarray): the predicted value of the points in the y axis coordinates
Returns:

float: residual error of the fit