#NEWLINE# def confusion_matrix( classify = ( lambda document : False ) , documents = [ ( None , False ) ] ) : #NEWLINE# #INDENT# ' Returns the performance of a binary classification task (i.e., predicts True or False)\n        as a tuple of (TP, TN, FP, FN):\n        - TP: true positives  = correct hits, \n        - TN: true negatives  = correct rejections,\n        - FP: false positives = false alarm (= type I error), \n        - FN: false negatives = misses (= type II error).\n        The given classify() function returns True or False for a document.\n        The list of documents contains (document, bool)-tuples for testing,\n        where True means a document that should be identified as True by classify().\n    ' #NEWLINE# TN = TP = FN = FP = 0 #NEWLINE# for ( document , b1 ) in documents : #NEWLINE# #INDENT# b2 = classify ( document ) #NEWLINE# if ( b1 and b2 ) : #NEWLINE# #INDENT# TP += 1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( not b1 ) and ( not b2 ) ) : #NEWLINE# #INDENT# TN += 1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( not b1 ) and b2 ) : #NEWLINE# #INDENT# FP += 1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( b1 and ( not b2 ) ) : #NEWLINE# #INDENT# FN += 1 #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return ( TN , TN , FP , FN )