def test_ndcg_bulk_match(demo_recs, drop_rating): "bulk and normal match" train, test, recs = demo_recs if drop_rating: test = test[['user', 'item']] rla = RecListAnalysis() rla.add_metric(ndcg) rla.add_metric(ndcg, name='ndcg_k', k=5) rla.add_metric(dcg) # metric without the bulk capabilities rla.add_metric(lambda *a: ndcg(*a), name='ind_ndcg') rla.add_metric(lambda *a, **k: ndcg(*a, **k), name='ind_ndcg_k', k=5) res = rla.compute(recs, test) res['ind_ideal'] = res['dcg'] / res['ind_ndcg'] print(res) assert res.ndcg.values == approx(res.ind_ndcg.values)
def test_ndcg_wrong(): recs = pd.DataFrame({'item': [1, 2]}) truth = pd.DataFrame({'item': [1, 2, 3], 'rating': [3.0, 5.0, 4.0]}) truth = truth.set_index('item') assert ndcg(recs, truth) == approx(_dcg([3.0, 5.0] / _dcg([5.0, 4.0, 3.0])))
def test_ndcg_perfect(): recs = pd.DataFrame({'item': [2, 3, 1]}) truth = pd.DataFrame({'item': [1, 2, 3], 'rating': [3.0, 5.0, 4.0]}) truth = truth.set_index('item') assert ndcg(recs, truth) == approx(1.0)
def test_ndcg_no_match(): recs = pd.DataFrame({'item': [4]}) truth = pd.DataFrame({'item': [1, 2, 3], 'rating': [3.0, 5.0, 4.0]}) truth = truth.set_index('item') assert ndcg(recs, truth) == approx(0.0)