Returns the performance of a binary classification task (i.e., predicts True or False) as a tuple of (TP, TN, FP, FN): - TP: true positives = correct hits, - TN: true negatives = correct rejections, - FP: false positives = false alarm (= type I error), -
(classify=lambda document: False, documents=[(None,False)])
| 99 | MACRO = "macro" |
| 100 | |
| 101 | def confusion_matrix(classify=lambda document: False, documents=[(None,False)]): |
| 102 | """ Returns the performance of a binary classification task (i.e., predicts True or False) |
| 103 | as a tuple of (TP, TN, FP, FN): |
| 104 | - TP: true positives = correct hits, |
| 105 | - TN: true negatives = correct rejections, |
| 106 | - FP: false positives = false alarm (= type I error), |
| 107 | - FN: false negatives = misses (= type II error). |
| 108 | The given classify() function returns True or False for a document. |
| 109 | The list of documents contains (document, bool)-tuples for testing, |
| 110 | where True means a document that should be identified as True by classify(). |
| 111 | """ |
| 112 | TN = TP = FN = FP = 0 |
| 113 | for document, b1 in documents: |
| 114 | b2 = classify(document) |
| 115 | if b1 and b2: |
| 116 | TP += 1 # true positive |
| 117 | elif not b1 and not b2: |
| 118 | TN += 1 # true negative |
| 119 | elif not b1 and b2: |
| 120 | FP += 1 # false positive (type I error) |
| 121 | elif b1 and not b2: |
| 122 | FN += 1 # false negative (type II error) |
| 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. |
no outgoing calls
no test coverage detected
searching dependent graphs…