-
Notifications
You must be signed in to change notification settings - Fork 4
/
emetrics.py
83 lines (63 loc) · 2.12 KB
/
emetrics.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
79
80
81
82
83
import numpy as np
import subprocess
def get_aupr(Y, P):
Y = np.concatenate(Y)
P = np.concatenate(P)
if hasattr(Y, 'A'): Y = Y.A
if hasattr(P, 'A'): P = P.A
Y = np.where(Y > 7, 1, 0)
Y = Y.ravel()
P = P.ravel()
f = open("temp.txt", 'w')
for i in range(Y.shape[0]):
f.write("%f %d\n" % (P[i], Y[i]))
f.close()
f = open("foo.txt", 'w')
subprocess.call(["java", "-jar", "auc.jar", "temp.txt", "list"], stdout=f)
f.close()
f = open("foo.txt")
lines = f.readlines()
aucpr = float(lines[-2].split()[-1])
f.close()
return aucpr
def get_cindex(Y, P):
summ = 0
pair = 0
for i in range(1, len(Y)):
for j in range(0, i):
if i is not j:
if (Y[i] > Y[j]):
pair += 1
summ += 1 * (P[i] > P[j]) + 0.5 * (P[i] == P[j])
if pair is not 0:
return summ / pair
else:
return 0
def r_squared_error(y_obs, y_pred):
y_obs = np.array(y_obs)
y_pred = np.array(y_pred)
y_obs_mean = [np.mean(y_obs) for y in y_obs]
y_pred_mean = [np.mean(y_pred) for y in y_pred]
mult = sum((y_pred - y_pred_mean) * (y_obs - y_obs_mean))
mult = mult * mult
y_obs_sq = sum((y_obs - y_obs_mean) * (y_obs - y_obs_mean))
y_pred_sq = sum((y_pred - y_pred_mean) * (y_pred - y_pred_mean))
return mult / float(y_obs_sq * y_pred_sq)
def get_k(y_obs, y_pred):
y_obs = np.array(y_obs)
y_pred = np.array(y_pred)
return sum(y_obs * y_pred) / float(sum(y_pred * y_pred))
def squared_error_zero(y_obs, y_pred):
k = get_k(y_obs, y_pred)
y_obs = np.array(y_obs)
y_pred = np.array(y_pred)
y_obs_mean = [np.mean(y_obs) for y in y_obs]
upp = sum((y_obs - (k * y_pred)) * (y_obs - (k * y_pred)))
down = sum((y_obs - y_obs_mean) * (y_obs - y_obs_mean))
return 1 - (upp / float(down))
def get_rm2(ys_orig, ys_line):
ys_orig = np.concatenate(ys_orig)
ys_line = np.concatenate(ys_line)
r2 = r_squared_error(ys_orig, ys_line)
r02 = squared_error_zero(ys_orig, ys_line)
return r2 * (1 - np.sqrt(np.absolute((r2 * r2) - (r02 * r02))))