Returns an (accuracy, precision, recall, F1-score)-tuple. With average=None, precision & recall are computed for the positive class (True). With average=MACRO, precision & recall for positive and negative class are macro-averaged.
(classify=lambda document:False, documents=[], average=None)
| 123 | return TP, TN, FP, FN |
| 124 | |
| 125 | def test(classify=lambda document:False, documents=[], average=None): |
| 126 | """ Returns an (accuracy, precision, recall, F1-score)-tuple. |
| 127 | With average=None, precision & recall are computed for the positive class (True). |
| 128 | With average=MACRO, precision & recall for positive and negative class are macro-averaged. |
| 129 | """ |
| 130 | TP, TN, FP, FN = confusion_matrix(classify, documents) |
| 131 | A = float(TP + TN) / ((TP + TN + FP + FN) or 1) |
| 132 | P1 = float(TP) / ((TP + FP) or 1) # positive class precision |
| 133 | R1 = float(TP) / ((TP + FN) or 1) # positive class recall |
| 134 | P0 = float(TN) / ((TN + FN) or 1) # negative class precision |
| 135 | R0 = float(TN) / ((TN + FP) or 1) # negative class recall |
| 136 | if average is None: |
| 137 | P, R = (P1, R1) |
| 138 | if average == MACRO: |
| 139 | P, R = ((P1 + P0) / 2, |
| 140 | (R1 + R0) / 2) |
| 141 | F1 = 2 * P * R / ((P + R) or 1) |
| 142 | return (A, P, R, F1) |
| 143 | |
| 144 | def accuracy(classify=lambda document:False, documents=[], average=None): |
| 145 | """ Returns the percentage of correct classifications (true positives + true negatives). |
searching dependent graphs…