sklearn plot confusion matrix with labels

前端 未结 7 1212
生来不讨喜
生来不讨喜 2020-11-29 19:09

I want to plot a confusion matrix to visualize the classifer\'s performance, but it shows only the numbers of the labels, not the labels themselves:

from skl         


        
7条回答
  •  离开以前
    2020-11-29 19:32

    You might be interested by https://github.com/pandas-ml/pandas-ml/

    which implements a Python Pandas implementation of Confusion Matrix.

    Some features:

    • plot confusion matrix
    • plot normalized confusion matrix
    • class statistics
    • overall statistics

    Here is an example:

    In [1]: from pandas_ml import ConfusionMatrix
    In [2]: import matplotlib.pyplot as plt
    
    In [3]: y_test = ['business', 'business', 'business', 'business', 'business',
            'business', 'business', 'business', 'business', 'business',
            'business', 'business', 'business', 'business', 'business',
            'business', 'business', 'business', 'business', 'business']
    
    In [4]: y_pred = ['health', 'business', 'business', 'business', 'business',
           'business', 'health', 'health', 'business', 'business', 'business',
           'business', 'business', 'business', 'business', 'business',
           'health', 'health', 'business', 'health']
    
    In [5]: cm = ConfusionMatrix(y_test, y_pred)
    
    In [6]: cm
    Out[6]:
    Predicted  business  health  __all__
    Actual
    business         14       6       20
    health            0       0        0
    __all__          14       6       20
    
    In [7]: cm.plot()
    Out[7]: 
    
    In [8]: plt.show()
    

    In [9]: cm.print_stats()
    Confusion Matrix:
    
    Predicted  business  health  __all__
    Actual
    business         14       6       20
    health            0       0        0
    __all__          14       6       20
    
    
    Overall Statistics:
    
    Accuracy: 0.7
    95% CI: (0.45721081772371086, 0.88106840959427235)
    No Information Rate: ToDo
    P-Value [Acc > NIR]: 0.608009812201
    Kappa: 0.0
    Mcnemar's Test P-Value: ToDo
    
    
    Class Statistics:
    
    Classes                                 business health
    Population                                    20     20
    P: Condition positive                         20      0
    N: Condition negative                          0     20
    Test outcome positive                         14      6
    Test outcome negative                          6     14
    TP: True Positive                             14      0
    TN: True Negative                              0     14
    FP: False Positive                             0      6
    FN: False Negative                             6      0
    TPR: (Sensitivity, hit rate, recall)         0.7    NaN
    TNR=SPC: (Specificity)                       NaN    0.7
    PPV: Pos Pred Value (Precision)                1      0
    NPV: Neg Pred Value                            0      1
    FPR: False-out                               NaN    0.3
    FDR: False Discovery Rate                      0      1
    FNR: Miss Rate                               0.3    NaN
    ACC: Accuracy                                0.7    0.7
    F1 score                               0.8235294      0
    MCC: Matthews correlation coefficient        NaN    NaN
    Informedness                                 NaN    NaN
    Markedness                                     0      0
    Prevalence                                     1      0
    LR+: Positive likelihood ratio               NaN    NaN
    LR-: Negative likelihood ratio               NaN    NaN
    DOR: Diagnostic odds ratio                   NaN    NaN
    FOR: False omission rate                       1      0
    

提交回复
热议问题