题目
- 只做了第一题,是给若干个confidence以及groundtruth label,计算在不同recall下的precision。
- 思路:将confidence降序排列,依次降低阈值,直到所有的正例都被召回,计算各个不同召回率下的precision即可。
代码
#include <cstdlib>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <stdio.h>
#include <numeric>
#include <algorithm>
#include <functional>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <memory>
#include <memory.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
typedef unsigned char uchar;
//#define G_DEBUG
// 定义unordered_set<pair<int,int>, pairhash> sets时会用到
struct pairhash {
public:
template <typename T, typename U>
std::size_t operator()(const std::pair<T, U> &x) const
{
return std::hash<T>()(x.first) ^ std::hash<U>()(x.second);
}
};
struct score
{
int gt;
double conf;
score(int _gt, double _conf) { gt = _gt; conf = _conf; }
score() { }
};
bool cmp(score& left, score &right)
{
if (left.conf < right.conf)
return false;
else if (left.conf > right.conf)
return true;
else
return left.gt >= right.gt;
}
int main()
{
#ifdef G_DEBUG
// 调试使用
ifstream file("data.txt");
int N = 0;
file >> N;
vector<score> confidence(N);
vector<int> gt(N, 0);
for (int i = 0; i < N; i++)
{
file >> confidence[i].gt >> confidence[i].conf;
}
file.close();
#else
int N = 0;
cin >> N;
vector<score> confidence(N);
vector<int> gt(N, 0);
for (int i = 0; i < N; i++)
{
cin >> confidence[i].gt >> confidence[i].conf;
}
#endif
int positive = 0;
for (auto & val : confidence)
positive += val.gt;
vector<int> nums(7, 0);
for (int i = 0; i < nums.size(); i++)
nums[i] = positive / 10 * (i + 3);
nums.push_back(positive);
std::sort(confidence.begin(), confidence.end(), cmp);
int num_idx = 0;
int curr_gt_pos = 0;
int idx = 0;
while (idx < N && num_idx < 7)
{
if (curr_gt_pos == nums[num_idx])
{
double curr_precision = curr_gt_pos*1.0 / idx;
while (idx < N && curr_gt_pos < nums[num_idx + 1])
{
curr_gt_pos += confidence[idx].gt;
idx++;
//curr_precision = max( curr_precision, curr_gt_pos*1.0 / idx);
}
curr_precision = curr_precision * 100 + 0.5;
int out = (int)curr_precision;
cout << out << endl;
num_idx++;
}
else
{
curr_gt_pos += confidence[idx].gt;
idx++;
}
}
system("pause");
return 0;
}