| 239 | typename T |
| 240 | > |
| 241 | matrix<double,1,2> test_ranking_function ( |
| 242 | const ranking_function& funct, |
| 243 | const std::vector<ranking_pair<T> >& samples |
| 244 | ) |
| 245 | { |
| 246 | // make sure requires clause is not broken |
| 247 | DLIB_ASSERT(is_ranking_problem(samples), |
| 248 | "\t double test_ranking_function()" |
| 249 | << "\n\t invalid inputs were given to this function" |
| 250 | << "\n\t samples.size(): " << samples.size() |
| 251 | << "\n\t is_ranking_problem(samples): " << is_ranking_problem(samples) |
| 252 | ); |
| 253 | |
| 254 | unsigned long total_pairs = 0; |
| 255 | unsigned long total_wrong = 0; |
| 256 | |
| 257 | std::vector<double> rel_scores; |
| 258 | std::vector<double> nonrel_scores; |
| 259 | std::vector<unsigned long> rel_counts; |
| 260 | std::vector<unsigned long> nonrel_counts; |
| 261 | |
| 262 | running_stats<double> rs; |
| 263 | std::vector<std::pair<double,bool> > total_scores; |
| 264 | std::vector<bool> total_ranking; |
| 265 | |
| 266 | for (unsigned long i = 0; i < samples.size(); ++i) |
| 267 | { |
| 268 | rel_scores.resize(samples[i].relevant.size()); |
| 269 | nonrel_scores.resize(samples[i].nonrelevant.size()); |
| 270 | total_scores.clear(); |
| 271 | |
| 272 | for (unsigned long k = 0; k < rel_scores.size(); ++k) |
| 273 | { |
| 274 | rel_scores[k] = funct(samples[i].relevant[k]); |
| 275 | total_scores.push_back(std::make_pair(rel_scores[k], true)); |
| 276 | } |
| 277 | |
| 278 | for (unsigned long k = 0; k < nonrel_scores.size(); ++k) |
| 279 | { |
| 280 | nonrel_scores[k] = funct(samples[i].nonrelevant[k]); |
| 281 | total_scores.push_back(std::make_pair(nonrel_scores[k], false)); |
| 282 | } |
| 283 | |
| 284 | // Now compute the average precision for this sample. We need to sort the |
| 285 | // results and the back them into total_ranking. Note that we sort them so |
| 286 | // that, if you get a block of ranking values that are all equal, the elements |
| 287 | // marked as true will come last. This prevents a ranking from outputting a |
| 288 | // constant value for everything and still getting a good MAP score. |
| 289 | std::sort(total_scores.rbegin(), total_scores.rend(), impl::compare_first_reverse_second); |
| 290 | total_ranking.clear(); |
| 291 | for (unsigned long i = 0; i < total_scores.size(); ++i) |
| 292 | total_ranking.push_back(total_scores[i].second); |
| 293 | rs.add(average_precision(total_ranking)); |
| 294 | |
| 295 | |
| 296 | count_ranking_inversions(rel_scores, nonrel_scores, rel_counts, nonrel_counts); |
| 297 | |
| 298 | total_pairs += rel_scores.size()*nonrel_scores.size(); |
no test coverage detected