Search code examples
scikit-learnclassificationconfusion-matrix

Get multi-class confusion matrix equal to number of class labels


I trained Random Forest Classifier in sklearn to predict multi-class classification problem.

My dataset has four class labels. But my code create 2x2 confusion matrix

y_predict = rf.predict(X_test)
conf_mat = sklearn.metrics.confusion_matrix(y_test, y_predict)
print(conf_mat)

Output:

[[0,   0]

 [394, 39]]

How can I get 4x4 confusion matrix to analyze TP, TN, FP, FN.


Solution

  • From the documentation at
    http://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html

    y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
    y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
    confusion_matrix(y_true, y_pred, labels=["ant", "bird", "cat"])
    

    Result :

    array([[2, 0, 0], 
           [0, 0, 1], 
           [1, 0, 2]])