-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathevaluation.py
81 lines (65 loc) · 2.29 KB
/
evaluation.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
import numpy as np
from sklearn.metrics import mean_absolute_error, mean_squared_error
from pylab import *
import matplotlib
import matplotlib.pyplot as plt
def get_topn(r_pred, train_mat, n=10):
unrated_items = r_pred * (train_mat==0)
idx = np.argsort(-unrated_items)
return idx[:,:n]
def recall_precision(topn, test_mat):
n,m = test_mat.shape
hits,total_pred,total_true = 0.,0.,0.
for u in range(n):
hits += len([i for i in topn[u,:] if test_mat[u,i]>0])
size_pred = len(topn[u,:])
size_true = np.sum(test_mat[u,:]>0,axis=0)
total_pred += size_pred
total_true += size_true
recall = hits/total_true
precision = hits/total_pred
return recall, precision
def mae_rmse(r_pred, test_mat):
y_pred = r_pred[test_mat>0]
y_true = test_mat[test_mat>0]
mae = mean_absolute_error(y_true, y_pred)
rmse = np.sqrt(mean_squared_error(y_true, y_pred))
return mae, rmse
def evaluation(pred_mat, train_mat, test_mat):
topn = get_topn(pred_mat, train_mat, n=10)
mae, rmse = mae_rmse(pred_mat, test_mat)
recall, precision = recall_precision(topn, test_mat)
return mae, rmse, recall, precision
def get_hit(ranklist,rated_item):
result = 0
for item in ranklist:
if item==rated_item:
result = 1
return result
def get_ndcg(ranklist,rated_item):
result = 0
for i in range(len(ranklist)):
item = ranklist[i]
if item==rated_item:
result = math.log(2)/math.log(i+2)
return result
def hit_ndcg(test_sequence, ranklist):
length = len(test_sequence)
hits,ndcgs=[],[]
for idx in range(length):
user = test_sequence[idx,0].astype(np.int32)
rated_item = test_sequence[idx,1].astype(np.int32)
hr = get_hit(ranklist[user],rated_item)
ndcg = get_ndcg(ranklist[user],rated_item)
hits.append(hr)
ndcgs.append(ndcg)
#hr,ndcg = np.array(hits).mean(),np.array(ndcgs).mean()
return hits,ndcgs
def figure(values_list, name=''):
fig=plt.figure(name)
x = range(len(values_list))
plot(x, values_list, color='g',linewidth=3)
plt.title(name + ' curve')
plt.xlabel('Iterations')
plt.ylabel(name)
show()