プリンキピア

第21章推薦システム

21.6.4. Evaluator21.6.4 評価器

In this section, we adopt the splitting by time strategy to construct the training and test sets. Two evaluation measures including hit rate at given cutting off ℓ (Hit@ℓ) and area under the ROC curve (AUC) are used to assess the model effectiveness. Hit rate at given position ℓ for each user indicates that whether the recommended item is included in the top ℓ ranked list. The formal definition is as follows:
英語のヒント
この節では、時間による分割の方式を採用して訓練集合とテスト集合を作る。モデルの有効性を評価するために、与えられた打ち切りℓでのヒット率(Hit@ℓ)と、ROC曲線の下の面積(AUC)の二つの評価尺度を使う。利用者ごとの与えられた位置ℓでのヒット率は、推薦されたアイテムが上位ℓ件の順位リストに含まれるかどうかを示す。形式的な定義は次のとおりである。
Hit@ℓ=1m∑u∈𝒰𝟏(ranku,gu<=ℓ),(21.6.4)
where 𝟏 denotes an indicator function that is equal to one if the ground truth item is ranked in the top ℓ list, otherwise it is equal to zero. ranku,gu denotes the ranking of the ground truth item gu of the user u in the recommendation list (The ideal ranking is 1). m is the number of users. 𝒰 is the user set.
英語のヒント
ここで𝟏は、正解のアイテムが上位ℓ件のリストに順位づけされていれば1、そうでなければ0になる指示関数を表す。ranku,guは、推薦リストでの利用者uの正解のアイテムguの順位を表す(理想的な順位は1である)。mは利用者の数である。𝒰は利用者の集合である。
The definition of AUC is as follows:
英語のヒント
AUCの定義は次のとおりである。
AUC=1m∑u∈𝒰1|ℐ∖Su|∑j∈I∖Su𝟏(ranku,gu<ranku,j),(21.6.5)
where ℐ is the item set. Su is the candidate items of user u. Note that many other evaluation protocols such as precision, recall and normalized discounted cumulative gain (NDCG) can also be used.
英語のヒント
ここでℐはアイテムの集合である。Suは利用者uの候補アイテムである。適合率、再現率、正規化減損累積利得(NDCG)のような、ほかの多くの評価の手順も使えることに注意しよう。
The following function calculates the hit counts and AUC for each user.
英語のヒント
次の関数は、利用者ごとにヒット数とAUCを計算する。
#@save
def hit_and_auc(rankedlist, test_matrix, k):
    hits_k = [(idx, val) for idx, val in enumerate(rankedlist[:k])
              if val in set(test_matrix)]
    hits_all = [(idx, val) for idx, val in enumerate(rankedlist)
                if val in set(test_matrix)]
    max = len(rankedlist) - 1
    auc = 1.0 * (max - hits_all[0][0]) / max if len(hits_all) > 0 else 0
    return len(hits_k), auc
Then, the overall Hit rate and AUC are calculated as follows.
英語のヒント
それから、全体のヒット率とAUCを次のように計算する。
#@save
def evaluate_ranking(net, test_input, seq, candidates, num_users, num_items,
                     devices):
    ranked_list, ranked_items, hit_rate, auc = {}, {}, [], []
    all_items = set([i for i in range(num_users)])
    for u in range(num_users):
        neg_items = list(all_items - set(candidates[int(u)]))
        user_ids, item_ids, x, scores = [], [], [], []
        [item_ids.append(i) for i in neg_items]
        [user_ids.append(u) for _ in neg_items]
        x.extend([np.array(user_ids)])
        if seq is not None:
            x.append(seq[user_ids, :])
        x.extend([np.array(item_ids)])
        test_data_iter = gluon.data.DataLoader(
            gluon.data.ArrayDataset(*x), shuffle=False, last_batch="keep",
            batch_size=1024)
        for index, values in enumerate(test_data_iter):
            x = [gluon.utils.split_and_load(v, devices, even_split=False)
                 for v in values]
            scores.extend([list(net(*t).asnumpy()) for t in zip(*x)])
        scores = [item for sublist in scores for item in sublist]
        item_scores = list(zip(item_ids, scores))
        ranked_list[u] = sorted(item_scores, key=lambda t: t[1], reverse=True)
        ranked_items[u] = [r[0] for r in ranked_list[u]]
        temp = hit_and_auc(ranked_items[u], test_input[u], 50)
        hit_rate.append(temp[0])
        auc.append(temp[1])
    return np.mean(np.array(hit_rate)), np.mean(np.array(auc))