def computePreRec(CM, classNames):
 numOfClasses = CM.shape[0]
 if (len(classNames) != numOfClasses):
  print 'Error in computePreRec! Confusion matrix and classNames list must be of the same size!'
  return
 Precision = []
 Recall = []
 F1 = []
 for (i, c) in enumerate(classNames):
  Precision.append((CM[(i, i)] / numpy.sum(CM[:, i])))
  Recall.append((CM[(i, i)] / numpy.sum(CM[i, :])))
  F1.append((((2 * Precision[(-1)]) * Recall[(-1)]) / (Precision[(-1)] + Recall[(-1)])))
 return (Recall, Precision, F1)

def computePreRec ( CM , classNames ) : numOfClasses = CM . shape [ 0 ] if ( len ( classNames ) != numOfClasses ) : print 'Error in computePreRec! Confusion matrix and classNames list must be of the same size!' return Precision = [ ] Recall = [ ] F1 = [ ] for ( i , c ) in enumerate ( classNames ) : Precision . append ( ( CM [ ( i , i ) ] / numpy . sum ( CM [ : , i ] ) ) ) Recall . append ( ( CM [ ( i , i ) ] / numpy . sum ( CM [ i , : ] ) ) ) F1 . append ( ( ( ( 2 * Precision [ ( - 1 ) ] ) * Recall [ ( - 1 ) ] ) / ( Precision [ ( - 1 ) ] + Recall [ ( - 1 ) ] ) ) ) return ( Recall , Precision , F1 )
this function computes the precision .