-
-
Notifications
You must be signed in to change notification settings - Fork 167
/
regression.py
78 lines (55 loc) · 1.82 KB
/
regression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from sklearn.metrics import mean_squared_error as mse
from sklearn.metrics import mean_absolute_error as mae # noqa
from sklearn.metrics import r2_score as r2 # noqa
from ml_metrics import quadratic_weighted_kappa as kappa # noqa
import numpy as np
from ..const import EPS
def mape(y, p):
"""Mean Absolute Percentage Error (MAPE).
Args:
y (numpy.array): target
p (numpy.array): prediction
Returns:
e (numpy.float64): MAPE
"""
filt = np.abs(y) > EPS
return np.mean(np.abs(1 - p[filt] / y[filt]))
def rmse(y, p):
"""Root Mean Squared Error (RMSE).
Args:
y (numpy.array): target
p (numpy.array): prediction
Returns:
e (numpy.float64): RMSE
"""
# check and get number of samples
assert y.shape == p.shape
return np.sqrt(mse(y, p))
def gini(y, p):
"""Normalized Gini Coefficient.
Args:
y (numpy.array): target
p (numpy.array): prediction
Returns:
e (numpy.float64): normalized Gini coefficient
"""
# check and get number of samples
assert y.shape == p.shape
n_samples = y.shape[0]
# sort rows on prediction column
# (from largest to smallest)
arr = np.array([y, p]).transpose()
true_order = arr[arr[:, 0].argsort()][::-1, 0]
pred_order = arr[arr[:, 1].argsort()][::-1, 0]
# get Lorenz curves
l_true = np.cumsum(true_order) / np.sum(true_order)
l_pred = np.cumsum(pred_order) / np.sum(pred_order)
l_ones = np.linspace(1/n_samples, 1, n_samples)
# get Gini coefficients (area between curves)
g_true = np.sum(l_ones - l_true)
g_pred = np.sum(l_ones - l_pred)
# normalize to true Gini coefficient
return g_pred / g_true