def get_ap(s): '''This is a function to get average precision''' a = 0.0 b = 0.0 for i in range(len(s)): if s[i] > 0: a += p.get_p(s, i + 1) b += 1 #print "Line: %s, ap is %f with a = %f, b = %f"%(s, a/b, a, b) if b == 0: b = 1 return a / b
def get_ap(s): '''This is a function to get average precision''' a = 0.0 b = 0.0 for i in range(len(s)): if s[i]>0: a += p.get_p(s, i+1) b += 1 #print "Line: %s, ap is %f with a = %f, b = %f"%(s, a/b, a, b) if b == 0: b = 1 return a/b
qCount = 0 count1 = 1 count3 = 1 count10 = 1 while 1: line = f.readline() if not line: break; arrays = re.split(r" ",line) #print arrays s = [float(i) for i in arrays] #print s qCount += 1 apTtl += ap.get_ap(s) for tmp_k in k: if len(line)<(4*tmp_k-1): continue; if tmp_k ==1: ttl1 += ndcg.get_ndcg(s, tmp_k) p1 += p.get_p(s,tmp_k) count1 = count1 + 1 if tmp_k ==3: ttl3 += ndcg.get_ndcg(s, tmp_k) p3 += p.get_p(s,tmp_k) count3 = count3 + 1 if tmp_k ==10: ttl10 += ndcg.get_ndcg(s, tmp_k) p10 += p.get_p(s,tmp_k) count10 = count10 + 1 print "NDCG@1: %f, NDCG@3: %f, NDCG@10: %f, MAP: %f"%(ttl1/count1,ttl3/count3,ttl10/count10, apTtl/qCount)
count1 = 1 count3 = 1 count10 = 1 while 1: line = f.readline() if not line: break arrays = re.split(r" ", line) #print arrays s = [float(i) for i in arrays] #print s qCount += 1 apTtl += ap.get_ap(s) for tmp_k in k: if len(line) < (4 * tmp_k - 1): continue if tmp_k == 1: ttl1 += ndcg.get_ndcg(s, tmp_k) p1 += p.get_p(s, tmp_k) count1 = count1 + 1 if tmp_k == 3: ttl3 += ndcg.get_ndcg(s, tmp_k) p3 += p.get_p(s, tmp_k) count3 = count3 + 1 if tmp_k == 10: ttl10 += ndcg.get_ndcg(s, tmp_k) p10 += p.get_p(s, tmp_k) count10 = count10 + 1 print "NDCG@1: %f, NDCG@3: %f, NDCG@10: %f, MAP: %f" % ( ttl1 / count1, ttl3 / count3, ttl10 / count10, apTtl / qCount)