// 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 namespace dlib { namespace impl { inline bool get_bool_part ( const bool& b ) { return b; } template bool get_bool_part(const std::pair& item) { return item.second; } } // ---------------------------------------------------------------------------------------- template double average_precision ( const std::vector& items, unsigned long missing_relevant_items = 0 ) { using namespace dlib::impl; double relevant_count = 0; // find the precision values std::vector 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::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_