#NEWLINE# def conf_matrix( p , labels , names = [ '1' , '0' ] , threshold = 0.5 , show = True ) : #NEWLINE# #INDENT# "\n    Returns error rate and true/false positives in a binary classification problem\n    - Actual classes are displayed by column.\n    - Predicted classes are displayed by row.\n\n    :param p: array of class '1' probabilities.\n    :param labels: array of actual classes.\n    :param names: list of class names, defaults to ['1','0'].\n    :param threshold: probability value used to decide the class.\n    :param show: whether the matrix should be shown or not\n    :type show: False|True\n    " #NEWLINE# assert ( p . size == labels . size ) , 'Arrays p and labels have different dimensions.' #NEWLINE# decision = np . ones ( ( labels . size , 1 ) ) #NEWLINE# decision [ ( p < threshold ) ] = 0 #NEWLINE# diff = ( decision - labels ) #NEWLINE# false_0 = diff [ ( diff == ( - 1 ) ) ] . size #NEWLINE# false_1 = diff [ ( diff == 1 ) ] . size #NEWLINE# true_1 = np . sum ( decision [ ( diff == 0 ) ] ) #NEWLINE# true_0 = ( ( ( labels . size - true_1 ) - false_0 ) - false_1 ) #NEWLINE# error = ( ( false_1 + false_0 ) / np . float ( labels . size ) ) #NEWLINE# if show : #NEWLINE# #INDENT# print ( ( 100.0 - ( error * 100 ) ) , '% instances correctly classified' ) #NEWLINE# print ( ( '%-10s|  %-10s|  %-10s| ' % ( '' , names [ 0 ] , names [ 1 ] ) ) ) #NEWLINE# print ( '----------|------------|------------|' ) #NEWLINE# print ( ( '%-10s|  %-10s|  %-10s| ' % ( names [ 0 ] , true_1 , false_0 ) ) ) #NEWLINE# print ( ( '%-10s|  %-10s|  %-10s| ' % ( names [ 1 ] , false_1 , true_0 ) ) ) #NEWLINE# #UNINDENT# return ( error , true_1 , false_1 , true_0 , false_0 )