See section 4 "Evaluation Metrics" in the paper about the F1 metric used.
(items)
| 131 | |
| 132 | |
| 133 | def f1(items): |
| 134 | """See section 4 "Evaluation Metrics" in the paper about the F1 metric used.""" |
| 135 | results = list(zip(*items)) |
| 136 | # Group the positive ("yes" = 1) golds and predictions by question. |
| 137 | gold_positives, pred_positives = defaultdict(list), defaultdict(list) |
| 138 | for gold, pred, question in zip(results[0], results[1], results[2]): |
| 139 | gold_positives[question].append(gold) |
| 140 | pred_positives[question].append(pred) |
| 141 | f1 = [] |
| 142 | for question in gold_positives.keys(): |
| 143 | gp, pp = sum(gold_positives[question]), sum(pred_positives[question]) |
| 144 | tp = sum(np.logical_and(gold_positives[question], pred_positives[question])) |
| 145 | p = tp / pp if pp > 0.0 else 1.0 |
| 146 | r = tp / gp if gp > 0.0 else 1.0 |
| 147 | if p + r > 0.0: |
| 148 | f1.append(2.0 * (p * r) / (p + r)) |
| 149 | return np.mean(f1) |