From 439888e9d347a9dd939992849e8bf3f07d5eee72 Mon Sep 17 00:00:00 2001 From: Davis King Date: Sun, 17 Feb 2013 10:44:55 -0500 Subject: [PATCH] Made average_precision() a little more generalized. --- dlib/statistics/average_precision.h | 22 +++++++++++++--- dlib/statistics/average_precision_abstract.h | 27 +++++++++++++++++++- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/dlib/statistics/average_precision.h b/dlib/statistics/average_precision.h index 6e7cecf75..978a2c4c6 100644 --- a/dlib/statistics/average_precision.h +++ b/dlib/statistics/average_precision.h @@ -9,16 +9,30 @@ namespace dlib { - inline double average_precision ( - const std::vector& items, + 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 precision_sum = 0; double relevant_count = 0; for (unsigned long i = 0; i < items.size(); ++i) { - if (items[i]) + if (get_bool_part(items[i])) { ++relevant_count; precision_sum += relevant_count / (i+1); @@ -33,6 +47,8 @@ namespace dlib return 1; } +// ---------------------------------------------------------------------------------------- + } #endif // DLIB_AVERAGE_PREcISION_H__ diff --git a/dlib/statistics/average_precision_abstract.h b/dlib/statistics/average_precision_abstract.h index 56602ac33..147c3ea8f 100644 --- a/dlib/statistics/average_precision_abstract.h +++ b/dlib/statistics/average_precision_abstract.h @@ -7,8 +7,14 @@ namespace dlib { + +// ---------------------------------------------------------------------------------------- + + template < + typename alloc + > double average_precision ( - const std::vector& items, + const std::vector& items, unsigned long missing_relevant_items = 0 ); /*! @@ -29,6 +35,25 @@ namespace dlib is 0.5. !*/ +// ---------------------------------------------------------------------------------------- + + template < + typename T, + typename alloc + > + double average_precision ( + const std::vector,alloc>& items, + unsigned long missing_relevant_items = 0 + ); + /*! + ensures + - this function is equivalent to copying the bool values from items into a + std::vector and then calling the above average_precision() routine on + it and returning the result. + !*/ + +// ---------------------------------------------------------------------------------------- + } #endif // DLIB_AVERAGE_PREcISION_H__