-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.go
More file actions
61 lines (55 loc) · 1.54 KB
/
Copy pathutils.go
File metadata and controls
61 lines (55 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package example
import (
"fmt"
"github.com/habedi/hann/core"
)
// FormatResults returns a formatted string of neighbor results.
// maxResults specifies how many items to include.
func FormatResults(results []core.Neighbor, maxResults int) string {
s := ""
limit := maxResults
if len(results) < limit {
limit = len(results)
}
for i := 0; i < limit; i++ {
n := results[i]
s += fmt.Sprintf("id=%d (dist=%.3f) ", n.ID, n.Distance)
}
return s
}
// FormatGroundTruth returns a formatted string of ground-truth neighbor results.
// maxResults specifies how many items to include.
func FormatGroundTruth(neighbors []int, distances []float64, k, maxResults int) string {
s := ""
limit := maxResults
if len(neighbors) < limit {
limit = len(neighbors)
}
for j := 0; j < limit; j++ {
s += fmt.Sprintf("id=%d (dist=%.3f) ", neighbors[j], distances[j])
}
return s
}
// RecallAtK computes Recall@k as the fraction of all ground-truth items that appear in the top k predictions.
func RecallAtK(predicted []core.Neighbor, groundTruth []int, k int) float64 {
if k <= 0 || len(groundTruth) == 0 {
return 0.0
}
// Build a set of predicted IDs from the top k predictions.
predSet := make(map[int]struct{})
limit := k
if len(predicted) < k {
limit = len(predicted)
}
for i := 0; i < limit; i++ {
predSet[predicted[i].ID] = struct{}{}
}
// Count ground-truth items that appear in the predictions.
correct := 0
for _, id := range groundTruth {
if _, ok := predSet[id]; ok {
correct++
}
}
return float64(correct) / float64(len(groundTruth))
}