-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
63 lines (50 loc) · 1.52 KB
/
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
61
62
63
from sklearn.metrics import roc_auc_score as ras
import numpy as np
def accuracy_score(y_true, y_pred):
TP = sum(y_true & y_pred)
FP = sum(y_true & ~y_pred)
FN = sum(~y_true & y_pred)
TN = sum(~y_true & ~y_pred)
return (TP + TN) / (TP + FP + FN + TN)
def precision_score(y_true, y_pred):
TP = sum(y_true & y_pred)
FP = sum(y_true & ~y_pred)
try:
return TP / (TP + FP)
except:
return None
def recall_score(y_true, y_pred):
TP = sum(y_true & y_pred)
FN = sum(~y_true & y_pred)
try:
return TP / (TP + FN)
except:
return None
def f_score(y_true, y_pred, beta=1):
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)
return (1 + beta**2) * (precision*recall) / (beta**2 * precision + recall)
def roc_auc_score(y_true, y_pred):
try:
return ras(y_true, y_pred)
except:
return None
def MWS(y_true, y_pred):
"""
Средневзвешанная метрика.
out: mean(weight * (y_true == y_pred))
"""
unique = np.unique(y_true)
length = len(unique)
num = np.array([sum(y_true == u) for u in unique])
total = sum(num)
w = {u: total / (length*n) for n,u in zip(num, unique)}
n = len(y_true)
weight = np.zeros(n)
for k in range(n):
weight[k] = w[y_true[k]]
return np.mean(weight * (y_pred == y_true))
def MAE(y_true, y_pred):
return np.mean(abs(y_true - y_pred))
def MRE(y_true, y_pred):
return np.mean(abs( (y_true - y_pred)/ y_true))