All problems
Hits@k
Problem
Compute the hits@k metric for link prediction.
true_edges is a list of [u, v] pairs and predictions is a ranked list of predicted [u, v] pairs. Return the fraction of true edges that appear among the first k predictions, or 0.0 when there are no true edges. Endpoint order is ignored.
Hint
Hits@k rewards ranking true edges near the top of the prediction list.
Test cases
case 1
in: [[[0,2],[1,3]],[[0,1],[0,2],[2,3],[1,3]],2]
expected: 0.5
case 2
in: [[[0,2],[1,3]],[[0,1],[0,2],[2,3],[1,3]],4]
expected: 1
case 3
in: [[[0,2],[1,3]],[[0,1],[0,2],[2,3],[1,3]],0]
expected: 0
case 4
in: [[[0,2]],[[0,1]],1]
expected: 0
Starter codePython
def hits_at_k(true_edges, predictions, k):
# Your code here
pass4 test cases · run your code against them in the editor