Commit 439888e9 authored by Davis King's avatar Davis King

Made average_precision() a little more generalized.

parent f5fe20c1
...@@ -9,16 +9,30 @@ ...@@ -9,16 +9,30 @@
namespace dlib namespace dlib
{ {
inline double average_precision ( namespace impl
const std::vector<bool>& items, {
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 unsigned long missing_relevant_items = 0
) )
{ {
using namespace dlib::impl;
double precision_sum = 0; double precision_sum = 0;
double relevant_count = 0; double relevant_count = 0;
for (unsigned long i = 0; i < items.size(); ++i) for (unsigned long i = 0; i < items.size(); ++i)
{ {
if (items[i]) if (get_bool_part(items[i]))
{ {
++relevant_count; ++relevant_count;
precision_sum += relevant_count / (i+1); precision_sum += relevant_count / (i+1);
...@@ -33,6 +47,8 @@ namespace dlib ...@@ -33,6 +47,8 @@ namespace dlib
return 1; return 1;
} }
// ----------------------------------------------------------------------------------------
} }
#endif // DLIB_AVERAGE_PREcISION_H__ #endif // DLIB_AVERAGE_PREcISION_H__
......
...@@ -7,8 +7,14 @@ ...@@ -7,8 +7,14 @@
namespace dlib namespace dlib
{ {
// ----------------------------------------------------------------------------------------
template <
typename alloc
>
double average_precision ( double average_precision (
const std::vector<bool>& items, const std::vector<bool,alloc>& items,
unsigned long missing_relevant_items = 0 unsigned long missing_relevant_items = 0
); );
/*! /*!
...@@ -29,6 +35,25 @@ namespace dlib ...@@ -29,6 +35,25 @@ namespace dlib
is 0.5. is 0.5.
!*/ !*/
// ----------------------------------------------------------------------------------------
template <
typename T,
typename alloc
>
double average_precision (
const std::vector<std::pair<T,bool>,alloc>& items,
unsigned long missing_relevant_items = 0
);
/*!
ensures
- this function is equivalent to copying the bool values from items into a
std::vector<bool> and then calling the above average_precision() routine on
it and returning the result.
!*/
// ----------------------------------------------------------------------------------------
} }
#endif // DLIB_AVERAGE_PREcISION_H__ #endif // DLIB_AVERAGE_PREcISION_H__
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment