Computes average precision for given ranked indexes. Arguments --------- ranks : zerro-based ranks of positive images nres : number of positive images Returns ------- ap : average precision
(ranks, nres)
| 686 | |
| 687 | |
| 688 | def compute_ap(ranks, nres): |
| 689 | """ |
| 690 | Computes average precision for given ranked indexes. |
| 691 | Arguments |
| 692 | --------- |
| 693 | ranks : zerro-based ranks of positive images |
| 694 | nres : number of positive images |
| 695 | Returns |
| 696 | ------- |
| 697 | ap : average precision |
| 698 | """ |
| 699 | |
| 700 | # number of images ranked by the system |
| 701 | nimgranks = len(ranks) |
| 702 | |
| 703 | # accumulate trapezoids in PR-plot |
| 704 | ap = 0 |
| 705 | |
| 706 | recall_step = 1. / nres |
| 707 | |
| 708 | for j in np.arange(nimgranks): |
| 709 | rank = ranks[j] |
| 710 | |
| 711 | if rank == 0: |
| 712 | precision_0 = 1. |
| 713 | else: |
| 714 | precision_0 = float(j) / rank |
| 715 | |
| 716 | precision_1 = float(j + 1) / (rank + 1) |
| 717 | |
| 718 | ap += (precision_0 + precision_1) * recall_step / 2. |
| 719 | |
| 720 | return ap |
| 721 | |
| 722 | |
| 723 | def compute_map(ranks, gnd, kappas=[]): |