-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevaluation_metrics.py
58 lines (53 loc) · 1.85 KB
/
evaluation_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
"""
functions used to calculate the metrics for multi-label classification
Created on 27/08/2017
@author: Qian Wang
codes used in the paper:
Qian Wang, Ke Chen, Multi-Label Zero-Shot Human Action Recognition via Joint Latent Embedding
"""
import numpy as np
def prf_cal(y_pred,y_true,k):
"""
function to calculate top-k precision/recall/f1-score
"""
GT=np.sum(y_true[y_true==1.])
instance_num=y_true.shape[0]
prediction_num=instance_num*k
sort_indices = np.argsort(y_pred)
sort_indices=sort_indices[:,::-1]
static_indices = np.indices(sort_indices.shape)
sorted_annotation= y_true[static_indices[0],sort_indices]
top_k_annotation=sorted_annotation[:,0:k]
TP=np.sum(top_k_annotation[top_k_annotation==1.])
recall=TP/GT
precision=TP/prediction_num
f1=2.*recall*precision/(recall+precision)
return precision, recall, f1
def cemap_cal(y_pred,y_true):
"""
function to calculate L-MAP and I-MAP
"""
nTest = y_true.shape[0]
nLabel = y_true.shape[1]
ap = np.zeros(nTest)
for i in range(0,nTest):
for j in range(0,nLabel):
R = np.sum(y_true[i,:])
if y_true[i,j]==1:
r = np.sum(y_pred[i,:]>=y_pred[i,j])
rb = np.sum(y_pred[i,np.nonzero(y_true[i,:])] >= y_pred[i,j])
ap[i] = ap[i] + rb/(r*1.0)
ap[i] = ap[i]/R
imap = np.nanmean(ap)
ap = np.zeros(nLabel)
for i in range(0,nLabel):
for j in range(0,nTest):
R = np.sum(y_true[:,i])
if y_true[j,i]==1:
r = np.sum(y_pred[:,i] >= y_pred[j,i])
rb = np.sum(y_pred[np.nonzero(y_true[:,i]),i] >= y_pred[j,i])
ap[i] = ap[i] + rb/(r*1.0)
ap[i] = ap[i]/R
lmap = np.nanmean(ap)
return lmap,imap