def _ndcg_score(y_true, y_pred, qid, k=None, dcg_func=None, idcg_cache=None): assert dcg_func is not None y_true = np.maximum(y_true, 0) dcg = _dcg_score(y_true, y_pred, qid, k=k, dcg_func=dcg_func) if idcg_cache: idcg = [] for a, b in group_offsets(qid): if qid[a] not in idcg_cache: idcg_cache[qid[a]] = dcg_func(np.sort(y_true[a:b]), np.arange(0, b - a), k=k) idcg.append(idcg_cache[qid[a]]) else: idcg = [ dcg_func(np.sort(y_true[a:b]), np.arange(0, b - a), k=k) for a, b in group_offsets(qid) ] idcg = np.array(idcg) assert (dcg <= idcg).all() idcg[idcg == 0] = 1 return dcg / idcg
def MAPScorer(y_true, y_pred, qid): return np.mean(np.array([_map_score(y_true[a:b], y_pred[a:b]) for a, b in group_offsets(qid)]))
def r_precision(y_true, y_pred, qid): return np.mean(np.array([rp(y_true[a:b], y_pred[a:b]) for a, b in group_offsets(qid)]))
def mean_reciprocal_rank(y_true, y_pred, qid): # MAP (Mean Average Precision) return np.mean(np.array([mrr(y_true[a:b], y_pred[a:b]) for a, b in group_offsets(qid)]))
def _dcg_score(y_true, y_pred, qid, k=None, dcg_func=None): assert dcg_func is not None y_true = np.maximum(y_true, 0) return np.array([ dcg_func(y_true[a:b], y_pred[a:b], k=k) for a, b in group_offsets(qid) ])
def p_score(y_true, y_pred, qid, k=None): return np.array([ _p_score(y_true[a:b], y_pred[a:b], k=k) for a, b in group_offsets(qid) ])