Scikit-learn: How to obtain True Positive, True Negative, False Positive and False Negative

前端 未结 16 1274
一生所求
一生所求 2020-12-02 04:25

My problem:

I have a dataset which is a large JSON file. I read it and store it in the trainList variable.

Next, I pre-process

16条回答
  •  半阙折子戏
    2020-12-02 05:23

    I wrote a version that works using only numpy. I hope it helps you.

    import numpy as np
    
    def perf_metrics_2X2(yobs, yhat):
        """
        Returns the specificity, sensitivity, positive predictive value, and 
        negative predictive value 
        of a 2X2 table.
    
        where:
        0 = negative case
        1 = positive case
    
        Parameters
        ----------
        yobs :  array of positive and negative ``observed`` cases
        yhat : array of positive and negative ``predicted`` cases
    
        Returns
        -------
        sensitivity  = TP / (TP+FN)
        specificity  = TN / (TN+FP)
        pos_pred_val = TP/ (TP+FP)
        neg_pred_val = TN/ (TN+FN)
    
        Author: Julio Cardenas-Rodriguez
        """
        TP = np.sum(  yobs[yobs==1] == yhat[yobs==1] )
        TN = np.sum(  yobs[yobs==0] == yhat[yobs==0] )
        FP = np.sum(  yobs[yobs==1] == yhat[yobs==0] )
        FN = np.sum(  yobs[yobs==0] == yhat[yobs==1] )
    
        sensitivity  = TP / (TP+FN)
        specificity  = TN / (TN+FP)
        pos_pred_val = TP/ (TP+FP)
        neg_pred_val = TN/ (TN+FN)
    
        return sensitivity, specificity, pos_pred_val, neg_pred_val
    

提交回复
热议问题