whenever

  • Home

  • Tags21

  • Categories6

  • Archives122

  • About

C++计算召回率

Posted on 2019-08-12 In 近似最近邻搜索

引言

近似最近邻搜索中评估算法的搜索性能经常用到召回率,召回率的计算公式一般为:
$$
Recall@K = \frac{R_1 \bigcap R_2}{K}
$$
其中,$R_1$ 为搜索算法返回的 $K$ 个元素组成的集合,$R_2$ 为查询点真实的 $K$ 个近邻点,$Recall@K$ 为返回 $K$ 个近邻点时的召回率。

C++代码实现召回率的计算

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
#include <iostream>
#include <fstream>
#include <vector>

void load_ivecs_data(const char* filename, //从文件中读取ivecs格式的数据
std::vector<std::vector<unsigned> >& results, unsigned &num, unsigned &dim) {
std::ifstream in(filename, std::ios::binary);
if (!in.is_open()) {
std::cout << "open file error" << std::endl;
exit(-1);
}
in.read((char*)&dim, 4);
//std::cout<<"data dimension: "<<dim<<std::endl;
in.seekg(0, std::ios::end);
std::ios::pos_type ss = in.tellg();
size_t fsize = (size_t)ss;
num = (unsigned)(fsize / (dim + 1) / 4);
results.resize(num);
for (unsigned i = 0; i < num; i++) results[i].resize(dim);

in.seekg(0, std::ios::beg);
for (size_t i = 0; i < num; i++) {
in.seekg(4, std::ios::cur);
in.read((char*)results[i].data(), dim * 4);
}
in.close();
}

void cal_recall(std::vector<std::vector<unsigned> > results, std::vector<std::vector<unsigned> > true_data, unsigned num, unsigned k) {//召回率计算
float mean_acc = 0;
for(size_t i = 0; i < num; i++) {
float acc = 0;
for(size_t j = 0; j < k; j++) {
for(size_t m = 0; m < k; m++) {
if(results[i][j] == true_data[i][m]) {
acc++;
break;
}
}
}
mean_acc += acc / k;
}
std::cout << "recall: " << mean_acc / num << std::endl;
}

int main(int argc, char** argv) {
std::vector<std::vector<unsigned> > true_data;
std::vector<std::vector<unsigned> > results;
unsigned dim, num;
load_ivecs_data(argv[1], true_data, num, dim);
load_ivecs_data(argv[2], results, num, dim);
cal_recall(results, true_data, num, dim);
return 0;
}

参考文献

[1]付聪, NSG : Navigating Spread-out Graph For Approximate Nearest Neighbor Search, https://github.com/ZJULearning/nsg, 2019.8.12.

稀罕作者
Mengzhao Wang WeChat Pay

WeChat Pay

Mengzhao Wang Alipay

Alipay

# ANNS # C/C++ # 源码阅读
C++读取fvecs格式数据(SIFT1M数据集的结构)
PAT乙级1005 || 继续(3n+1)猜想(详解,C/C++示例,测试点分析)
  • Table of Contents
  • Overview
Mengzhao Wang

Mengzhao Wang

Try? All the way !
122 posts
6 categories
21 tags
  1. 1. 引言
  2. 2. C++代码实现召回率的计算
  3. 3. 参考文献
© 2021 Mengzhao Wang