From 2ca1caa3a85d087ee93e9f554aa9134dd79c7172 Mon Sep 17 00:00:00 2001 From: Lukas Wegmeth Date: Mon, 11 Mar 2024 16:13:47 +0100 Subject: [PATCH 1/3] Documentation fix for Recall and Hit The documentation contains max rather than min functions. Min functions are used in the implementation. --- lenskit/metrics/topn.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lenskit/metrics/topn.py b/lenskit/metrics/topn.py index 680559f6e..452ab2d40 100644 --- a/lenskit/metrics/topn.py +++ b/lenskit/metrics/topn.py @@ -74,7 +74,7 @@ def recall(recs, truth, k=None): Compute recommendation recall. This is computed as: .. math:: - \\frac{|L \\cap I_u^{\\mathrm{test}}|}{\\operatorname{max}\\{|I_u^{\\mathrm{test}}|, k\\}} + \\frac{|L \\cap I_u^{\\mathrm{test}}|}{\\operatorname{min}\\{|I_u^{\\mathrm{test}}|, k\\}} This metric has a bulk implementation. """ @@ -120,7 +120,7 @@ def hit(recs, truth, k=None): lists, this computes the *hit rate* :cite:p:`Deshpande2004-ht`. .. math:: - \\frac{|L \\cap I_u^{\\mathrm{test}}|}{\\operatorname{max}\\{|I_u^{\\mathrm{test}}|, k\\}} + \\frac{|L \\cap I_u^{\\mathrm{test}}|}{\\operatorname{min}\\{|I_u^{\\mathrm{test}}|, k\\}} This metric has a bulk implementation. """ From ed9bd9004c6ac628c017ae93e84fac6b89411b9e Mon Sep 17 00:00:00 2001 From: Lukas Wegmeth Date: Mon, 11 Mar 2024 16:29:55 +0100 Subject: [PATCH 2/3] Fixed Hit function Removed nrel and documentation formula. --- lenskit/metrics/topn.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/lenskit/metrics/topn.py b/lenskit/metrics/topn.py index 452ab2d40..969342fa0 100644 --- a/lenskit/metrics/topn.py +++ b/lenskit/metrics/topn.py @@ -119,17 +119,10 @@ def hit(recs, truth, k=None): is scored as 1, and lists with no relevant items as 0. When averaged over the recommendation lists, this computes the *hit rate* :cite:p:`Deshpande2004-ht`. - .. math:: - \\frac{|L \\cap I_u^{\\mathrm{test}}|}{\\operatorname{min}\\{|I_u^{\\mathrm{test}}|, k\\}} - This metric has a bulk implementation. """ - nrel = len(truth) - if nrel == 0: - return None - if k is not None: - nrel = min(nrel, k) + if k is not None: recs = recs.iloc[:k] good = recs["item"].isin(truth.index) From f4e1e274d2db77ef02ed488cf6e627407eaa36b1 Mon Sep 17 00:00:00 2001 From: Lukas Wegmeth Date: Mon, 11 Mar 2024 19:20:23 +0100 Subject: [PATCH 3/3] Fixed the fix Tests failed because the function must return None when truth is empty. --- lenskit/metrics/topn.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lenskit/metrics/topn.py b/lenskit/metrics/topn.py index 969342fa0..f95f07111 100644 --- a/lenskit/metrics/topn.py +++ b/lenskit/metrics/topn.py @@ -122,7 +122,10 @@ def hit(recs, truth, k=None): This metric has a bulk implementation. """ - if k is not None: + if len(truth) == 0: + return None + + if k is not None: recs = recs.iloc[:k] good = recs["item"].isin(truth.index)