| 23 | |
| 24 | template <typename T, typename alloc> |
| 25 | double average_precision ( |
| 26 | const std::vector<T,alloc>& items, |
| 27 | unsigned long missing_relevant_items = 0 |
| 28 | ) |
| 29 | { |
| 30 | using namespace dlib::impl; |
| 31 | double relevant_count = 0; |
| 32 | // find the precision values |
| 33 | std::vector<double> precision; |
| 34 | for (unsigned long i = 0; i < items.size(); ++i) |
| 35 | { |
| 36 | if (get_bool_part(items[i])) |
| 37 | { |
| 38 | ++relevant_count; |
| 39 | precision.push_back(relevant_count / (i+1)); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | double precision_sum = 0; |
| 44 | double max_val = 0; |
| 45 | // now sum over the interpolated precision values |
| 46 | for (std::vector<double>::reverse_iterator i = precision.rbegin(); i != precision.rend(); ++i) |
| 47 | { |
| 48 | max_val = std::max(max_val, *i); |
| 49 | precision_sum += max_val; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | relevant_count += missing_relevant_items; |
| 54 | |
| 55 | if (relevant_count != 0) |
| 56 | return precision_sum/relevant_count; |
| 57 | else |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | // ---------------------------------------------------------------------------------------- |
| 62 | |