support/models.py
def class_perf(self, y_est, y_true):
        '''
        Given class label estimates and true values, compute the
        fraction of correct classifications made.
        '''
        
        # Input:
        # y_est and y_true are (k x 1) matrices of labels.

        # Output:
        # Returns a dictionary with two components, (1) being
        # the fraction of correctly classified labels, and
        # (2) being a dict of per-label precison/recall/F1
        # scores. 

        # First, get the classification rate.
        k = y_est.size
        num_correct = (y_est == y_true).sum()
        frac_correct = num_correct / k

        # Then, get precision/recall for each class.
        prec_rec = { i:None for i in range(self.nc) } # initialize

        for c in range(self.nc):

            idx_c = (y_true == c)
            idx_notc = (idx_c == False)

            TP = (y_est[idx_c] == c).sum()
            FN = idx_c.sum() - TP
            FP = (y_est[idx_notc] == c).sum()
            TN = idx_notc.sum() - FP

            # Precision.
            if (TP == 0 and FP == 0):
                prec = 0
            else:
                prec = TP / (TP+FP)

            # Recall.
            if (TP == 0 and FN == 0):
                rec = 0
            else:
                rec = TP / (TP+FN)

            # F1 (harmonic mean of precision and recall).
            if (prec == 0 or rec == 0):
                f1 = 0
            else:
                f1 = 2 * prec * rec / (prec + rec)

            prec_rec[c] = {"P": prec,
                           "R": rec,
                           "F1": f1}

        return {"rate": frac_correct,
                "PRF1": prec_rec}

        Given class label estimates and true values, compute the
        fraction of correct classifications made.
        
Given class label estimates and true values, compute the
fraction of correct classifications made.
Given class label estimates and true values compute the fraction of correct classifications made .
def class_perf(self, y_est, y_true):
        k = y_est.size
        num_correct = (y_est == y_true).sum()
        frac_correct = num_correct / k
        prec_rec = { i:None for i in range(self.nc) } 
        for c in range(self.nc):
            idx_c = (y_true == c)
            idx_notc = (idx_c == False)
            TP = (y_est[idx_c] == c).sum()
            FN = idx_c.sum() - TP
            FP = (y_est[idx_notc] == c).sum()
            TN = idx_notc.sum() - FP
            if (TP == 0 and FP == 0):
                prec = 0
            else:
                prec = TP / (TP+FP)
            if (TP == 0 and FN == 0):
                rec = 0
            else:
                rec = TP / (TP+FN)
            if (prec == 0 or rec == 0):
                f1 = 0
            else:
                f1 = 2 * prec * rec / (prec + rec)
            prec_rec[c] = {"P": prec,
                           "R": rec,
                           "F1": f1}
        return {"rate": frac_correct,
                "PRF1": prec_rec}
def class_perf ( self , y_est , y_true ) : k = y_est . size num_correct = ( y_est == y_true ) . sum ( ) frac_correct = num_correct / k prec_rec = { i : None for i in range ( self . nc ) } for c in range ( self . nc ) : idx_c = ( y_true == c ) idx_notc = ( idx_c == False ) TP = ( y_est [ idx_c ] == c ) . sum ( ) FN = idx_c . sum ( ) - TP FP = ( y_est [ idx_notc ] == c ) . sum ( ) TN = idx_notc . sum ( ) - FP if ( TP == 0 and FP == 0 ) : prec = 0 else : prec = TP / ( TP + FP ) if ( TP == 0 and FN == 0 ) : rec = 0 else : rec = TP / ( TP + FN ) if ( prec == 0 or rec == 0 ) : f1 = 0 else : f1 = 2 * prec * rec / ( prec + rec ) prec_rec [ c ] = { "P" : prec , "R" : rec , "F1" : f1 } return { "rate" : frac_correct , "PRF1" : prec_rec }
Given class label estimates and true values, compute the
fraction of correct classifications made.
Given class label estimates and true values compute the fraction of correct classifications made .
'''
        Given class label estimates and true values, compute the
        fraction of correct classifications made.
        ''' # Input: # y_est and y_true are (k x 1) matrices of labels. # Output: # Returns a dictionary with two components, (1) being # the fraction of correctly classified labels, and # (2) being a dict of per-label precison/recall/F1 # scores.  # First, get the classification rate. # Then, get precision/recall for each class. # initialize # Precision. # Recall. # F1 (harmonic mean of precision and recall).
self
y_est
y_true