Esempio n. 1
0
def a_ndcg(true,
           pred,
           aspects,
           k=10,
           alpha=0.5,
           user_col='user_id',
           item_col='item_id'):
    """Measures redundancy-aware quality and diversity."""
    if type(true) is pd.DataFrame:
        true = pandas_to_dict(true, user_col, item_col)
    if type(pred) is pd.DataFrame:
        pred = pandas_to_dict(pred, user_col, item_col)
    return user_mean_sub(_a_ndcg, true, pred, aspects, k, alpha)
Esempio n. 2
0
def coverage(items, recs, k=None, user_col='user_id', item_col='item_id'):
    """What percentage of items appears in recommendations?

    Args:
        items: list of unique item ids
        recs: dict of recommendations
        k: topk items to use from recs

    Returns: float
    """
    if type(recs) is pd.DataFrame:
        recs = pandas_to_dict(recs, user_col, item_col)
    topk = list(set(flatten_list(top_k(recs, k).values())))
    return np.isin(items, topk).mean()
Esempio n. 3
0
def popularity(log, pred, k=10, user_col='user_id', item_col='item_id'):
    """
    Mean popularity of recommendations.

    Args:
        log: pandas DataFrame with interactions
        pred: pandas DataFrame with recommendations
        k: top k items to use from recs
        user_col: column name for user ids
        item_col: column name for item ids

    """
    if type(pred) is pd.DataFrame:
        pred = pandas_to_dict(pred, user_col, item_col)
    scores = item_pop(log, user_col, item_col)
    return user_apply(_popularity, scores, pred, k, 0)
Esempio n. 4
0
def surprisal(log, pred, k=10, user_col='user_id', item_col='item_id'):
    if type(pred) is pd.DataFrame:
        pred = pandas_to_dict(pred, user_col, item_col)
    scores = -np.log2(item_pop(log, user_col, item_col))
    fill = np.log2(log[user_col].nunique())
    return user_apply(_popularity, scores, pred, k, fill)