Answers for "how to print confusion matrix in python with labels"

1

how to find the labels of the confusion matrix in python

""" In order to find the labels just use the Counter function to count 
the records from y_test and then check row-wise sum of the confusion 
matrix. Then apply the labels to the corresponding rows using the 
inbuilt seaborn plot as shown below"""

from collections import Counter
Counter(y_test).keys()
Counter(y_test).values()

import seaborn as sns
import matplotlib.pyplot as plt     

ax= plt.subplot()
sns.heatmap(cm, annot=True, fmt='g', ax=ax);  #annot=True to annotate cells, ftm='g' to disable scientific notation

# labels, title and ticks
ax.set_xlabel('Predicted labels');ax.set_ylabel('True labels'); 
ax.set_title('Confusion Matrix'); 
ax.xaxis.set_ticklabels(['business', 'health']); ax.yaxis.set_ticklabels(['health', 'business']);
Posted by: Guest on May-23-2021
0

print labels on confusion_matrix

import pandas as pd
cmtx = pd.DataFrame(
    confusion_matrix(y_true, y_pred, labels=['yes', 'no']), 
    index=['true:yes', 'true:no'], 
    columns=['pred:yes', 'pred:no']
)
print(cmtx)
# Output:
#           pred:yes  pred:no
# true:yes         1        2
# true:no          0        3
Posted by: Guest on April-17-2020
0

confusion matrix with labels sklearn

Predicted  0  1  2  All
True                   
0          3  0  0    3
1          0  1  2    3
2          2  1  3    6
All        5  2  5   12
Posted by: Guest on March-07-2021

Code answers related to "how to print confusion matrix in python with labels"

Python Answers by Framework

Browse Popular Code Answers by Language