Direct Graphical Models  v.1.7.0
AveragePrecision.cpp
1 #include "AveragePrecision.h"
2 #include "macroses.h"
3 
5 {
6  float getAveragePrecision(const vec_byte_t &predictions, const vec_float_t &potentials, const vec_byte_t &gt, byte state)
7  {
8  // Assertions
9  DGM_ASSERT(predictions.size() == potentials.size());
10  DGM_ASSERT(predictions.size() == gt.size());
11 
12  std::vector<std::tuple<byte, float, byte>> container; // < prediction | pot | gt >
13  for (size_t i = 0; i < predictions.size(); i++)
14  container.push_back(std::make_tuple(predictions[i], potentials[i], gt[i]));
15  // Sorting on potentials
16  std::sort(container.begin(), container.end(), [](auto const &left, auto const &right) { return std::get<1>(left) > std::get<1>(right); });
17 
18  float res = 0.0f;
19  int nRelevants = 0; // number of relevant elements
20  int nCoincidences = 0; // number of correct predictions
21  int i = 0;
22  for (auto &c : container) {
23  i++;
24  if (std::get<2>(c) == state) { // if (gt == state)
25  nRelevants++;
26  if (std::get<0>(c) == state) { // if (prediction = state)
27  nCoincidences++;
28  res += static_cast<float>(nCoincidences) / i;
29  }
30  }
31  }
32 
33  DGM_IF_WARNING(nRelevants == 0, "There are no states <state> in <gt>");
34 
35  if (nRelevants > 0) res /= nRelevants;
36 
37  return res;
38  }
39 
40 }
float getAveragePrecision(const vec_byte_t &predictions, const vec_float_t &potentials, const vec_byte_t &gt, byte state)
Returns Average Precision for the selected state (class) state.