Made average_precision() a little more generalized.

This commit is contained in:
Davis King 2013-02-17 10:44:55 -05:00
parent f5fe20c1ff
commit 439888e9d3
2 changed files with 45 additions and 4 deletions

View File

@ -9,16 +9,30 @@
namespace dlib
{
inline double average_precision (
const std::vector<bool>& items,
namespace impl
{
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
)
{
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__

View File

@ -7,8 +7,14 @@
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <
typename alloc
>
double average_precision (
const std::vector<bool>& items,
const std::vector<bool,alloc>& 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<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__