Scikit-learn, get accuracy scores for each class

前端 未结 6 1205
说谎
说谎 2021-01-07 17:49

Is there a built-in way for getting accuracy scores for each class separatetly? I know in sklearn we can get overall accuracy by using metric.accuracy_score. Is

6条回答
  •  天涯浪人
    2021-01-07 18:27

    The question is misleading. Accuracy scores for each class equal the overall accuracy score. Consider the confusion matrix:

    from sklearn.metrics import confusion_matrix
    import numpy as np
    
    y_true = [0, 1, 2, 2, 2]
    y_pred = [0, 0, 2, 2, 1]
    
    #Get the confusion matrix
    cm = confusion_matrix(y_true, y_pred)
    print(cm)
    

    This gives you:

     [[1 0 0]
      [1 0 0]
      [0 1 2]]
    

    Accuracy is calculated as the proportion of correctly classified samples to all samples:

    accuracy = (TP + TN) / (P + N)
    

    Regarding the confusion matrix, the numerator (TP + TN) is the sum of the diagonal. The denominator is the sum of all cells. Both are the same for every class.

提交回复
热议问题