Example #1
0
def _ndcg_score(y_true, y_pred, qid, k=None, dcg_func=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)
    idcg = np.array([dcg_func(np.sort(y_true[a:b]), np.arange(0, b - a), k=k)
                     for a, b in group_offsets(qid)])
    assert (dcg <= idcg).all()
    idcg[idcg == 0] = 1
    return dcg / idcg
Example #2
0
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
Example #3
0
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)])
Example #4
0
def ap_score(y_true, y_pred, qid):
    return np.array([_ap_score(y_true[a:b], y_pred[a:b]) for a, b in group_offsets(qid)])
Example #5
0
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)])