Scikit-learn confusion matrix

前端 未结 4 1386
情书的邮戳
情书的邮戳 2021-02-01 04:15

I can\'t figure out if I\'ve setup my binary classification problem correctly. I labeled the positive class 1 and the negative 0. However It is my understanding that by default

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-01 04:56

    Following the example of wikipedia. If a classification system has been trained to distinguish between cats and non cats, a confusion matrix will summarize the results of testing the algorithm for further inspection. Assuming a sample of 27 animals — 8 cats, and 19 non cats, the resulting confusion matrix could look like the table below:

    With sklearn

    If you want to maintain the structure of the wikipedia confusion matrix, first go the predicted values and then the actual class.

    from sklearn.metrics import confusion_matrix
    y_true = [0,0,0,1,0,0,1,0,0,1,0,1,0,0,0,0,1,0,0,1,1,0,1,0,0,0,0]
    y_pred = [0,0,0,1,0,0,1,0,0,1,0,1,0,0,0,0,1,0,0,0,0,1,0,1,0,0,0]
    confusion_matrix(y_pred, y_true, labels=[1,0])
    
    Out[1]: 
    array([[ 5,  2],
           [ 3, 17]], dtype=int64)
    

    Another way with crosstab pandas

    true = pd.Categorical(list(np.where(np.array(y_true) == 1, 'cat','non-cat')), categories = ['cat','non-cat'])
    pred = pd.Categorical(list(np.where(np.array(y_pred) == 1, 'cat','non-cat')), categories = ['cat','non-cat'])
    
    pd.crosstab(pred, true, 
                rownames=['pred'], 
                colnames=['Actual'], margins=False, margins_name="Total")
    
    Out[2]: 
    Actual   cat  non-cat
    pred                 
    cat        5        2
    non-cat    3       17
    

    I hope it serves you

提交回复
热议问题