-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccuracy_metrics.py
executable file
·60 lines (40 loc) · 1.57 KB
/
accuracy_metrics.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
def calculate_model_accuracy(y_true, y_pred, metric: str):
if metric == 'mse':
return mse(y_true, y_pred)
elif metric == 'rmse':
return rmse(y_true, y_pred)
elif metric == 'nrmse':
return nrmse(y_true, y_pred)
elif metric == 'mape':
return mape(y_true, y_pred)
elif metric == 'smape':
return smape(y_true, y_pred)
elif metric == 'arv':
return arv(y_true, y_pred)
elif metric == 'mae':
return mae(y_true, y_pred)
else:
return 'This competence metric is not yet implemented.'
def mape(y_true, y_pred):
from numpy import mean, abs
epsilon = 1e-6
return mean(abs((y_true - y_pred) / (y_true + epsilon))) * 100
def mae(y_true, y_pred):
from sklearn.metrics import mean_absolute_error
return mean_absolute_error(y_true, y_pred)
def rmse(y_true, y_pred):
from sklearn.metrics import mean_squared_error
return mean_squared_error(y_true, y_pred, squared=False)
def mse(y_true, y_pred):
from sklearn.metrics import mean_squared_error
return mean_squared_error(y_true, y_pred)
def nrmse(y_true, y_pred):
return rmse(y_true, y_pred) / (y_true.max() - y_true.min())
def arv(y_true, y_pred):
from numpy import repeat
return mse(y_true, y_pred) / mse(repeat(y_true.mean(), len(y_true)), y_pred)
def smape(y_true, y_pred):
from numpy import abs, mean
epsilon = 1e-6
# return 100 / len(y_true) * sum(2 * abs(y_true - y_pred) / (abs(y_true) + abs(y_pred)))
return mean(2.0 * abs(y_true - y_pred) / ((abs(y_true) + abs(y_pred)) + epsilon)) * 100