|
1 | 1 | import pytest |
2 | 2 |
|
3 | 3 | import torch |
| 4 | +import math |
4 | 5 | from sklearn.metrics import ndcg_score |
5 | 6 | import dhg.metrics.recommender as dm |
6 | 7 |
|
@@ -30,3 +31,13 @@ def test_ndcg(): |
30 | 31 | assert dm.ndcg(y_true, y_score, k=3) == pytest.approx(ndcg_score(y_true, y_score, k=3)) |
31 | 32 | assert dm.ndcg(y_true, y_score, k=4) == pytest.approx(ndcg_score(y_true, y_score, k=4)) |
32 | 33 | assert dm.ndcg(y_true, y_score, k=5) == pytest.approx(ndcg_score(y_true, y_score, k=5)) |
| 34 | + |
| 35 | + y_true = torch.tensor([0, 1, 0, 0, 1, 1]) |
| 36 | + y_pred = torch.tensor([0.8, 0.9, 0.6, 0.7, 0.4, 0.5]) |
| 37 | + assert dm.ndcg(y_true, y_pred, k=2) == pytest.approx((1 / math.log2(2)) / (1 / math.log2(2) + 1 / math.log2(3))) |
| 38 | + assert dm.ndcg(y_true, y_pred, k=3) == pytest.approx((1 / math.log2(2)) / (1 / math.log2(2) + 1 / math.log2(3) + 1 / math.log2(4))) |
| 39 | + assert dm.ndcg(y_true, y_pred, k=5) == pytest.approx((1 / math.log2(2) + 1 / math.log2(6)) / (1 / math.log2(2) + 1 / math.log2(3) + 1 / math.log2(4))) |
| 40 | + |
| 41 | + y_true = torch.tensor([3, 2, 3, 0, 1, 2, 3, 2]) |
| 42 | + y_pred = torch.tensor([0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2]) |
| 43 | + assert dm.ndcg(y_true, y_pred, k=6) == pytest.approx(0.785, abs=1e-4) |
0 commit comments