1

I am trying to plot a confusion matrix for my topic classification. However, when I plot it, I can't see anything clearly. I get scribbled X and Y axis without anything properly plotted. How can I plot it in a better way to see how my documents are being classified? I have 22465 documents and 88 topics.

Here is my code:

from pandas_confusion import ConfusionMatrix
import matplotlib.pyplot as plt
model = LogisticRegression()
model = model.fit(matrix_tmp, label_tmp)
pred = model.predict(matrix_tmp_test)

print(metrics.accuracy_score(label_tmp_test,pred))
print(metrics.classification_report(label_tmp_test,pred))

cm=ConfusionMatrix(label_tmp_test,pred)
print(cm)
cm.plot()
plt.show()

This is the plot I receive. enter image description here

1

2 Answers 2

0

Disclaimer, Hi, PyCM is a python module which can help you to show a multi-class confusion matrix through different types of reports such as a html report and also plotting it.

There is a simple code for saving a html report of a confusion matrix.

cm.save_html("file_name",color=(R,G,B))
0

You can do it by the code blow:

from torch import randint
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
import seaborn as sn
import pandas as pd
import numpy as np

cf_matrix = confusion_matrix(randint(5, (20,)), randint(5, (20,)))
CLASSES = [1,2,3,4,5]
df_cm = pd.DataFrame(cf_matrix / np.sum(cf_matrix, axis=1)[:, None], index = [i for i in CLASSES], columns = [i for i in CLASSES])
plt.figure(figsize = (12,7))
sn.heatmap(df_cm, annot=True)
plt.savefig("output1.png")

please note that at this line;

cf_matrix = confusion_matrix(randint(5, (20,)), randint(5, (20,)))

The first parameter of confusion_matrix is True Boxes and the second parameter is Pred Boxes

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.