blob: 6c2e7e0b1f81ab54cea3daa478878037e3841cc8 (
plain)
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
62
63
64
65
66
|
// Copyright (C) 2013 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_AVERAGE_PREcISION_Hh_
#define DLIB_AVERAGE_PREcISION_Hh_
#include "average_precision_abstract.h"
#include <vector>
namespace dlib
{
namespace impl
{
inline bool get_bool_part (
const bool& b
) { return b; }
template <typename T>
bool get_bool_part(const std::pair<T,bool>& item) { return item.second; }
}
// ----------------------------------------------------------------------------------------
template <typename T, typename alloc>
double average_precision (
const std::vector<T,alloc>& items,
unsigned long missing_relevant_items = 0
)
{
using namespace dlib::impl;
double relevant_count = 0;
// find the precision values
std::vector<double> precision;
for (unsigned long i = 0; i < items.size(); ++i)
{
if (get_bool_part(items[i]))
{
++relevant_count;
precision.push_back(relevant_count / (i+1));
}
}
double precision_sum = 0;
double max_val = 0;
// now sum over the interpolated precision values
for (std::vector<double>::reverse_iterator i = precision.rbegin(); i != precision.rend(); ++i)
{
max_val = std::max(max_val, *i);
precision_sum += max_val;
}
relevant_count += missing_relevant_items;
if (relevant_count != 0)
return precision_sum/relevant_count;
else
return 1;
}
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_AVERAGE_PREcISION_Hh_
|