- Best AI Text Generators for High Quality Content Writing
- Tensorflow Error on Macbook M1 Pro - NotFoundError: Graph execution error
- How does GPT-like transformers utilize only the decoder to do sequence generation?
- How to set all tensors to cuda device?
- How should I use torch.compile properly?
- How do I check if PyTorch is using the GPU?
- WARNING:tensorflow:Using a while_loop for converting cause there is no registered converter for this op
- How to use OneCycleLR?
- Error in Python script "Expected 2D array, got 1D array instead:"?
- How to save model in .pb format and then load it for inference in Tensorflow?
- Top 6 AI Logo Generator Up Until Now- Smarter Than Midjourney
- Best 9 AI Story Generator Tools
- The Top 6 AI Voice Generator Tools
- Best AI Low Code/No Code Tools for Rapid Application Development
- YOLOV8 how does it handle different image sizes
- Best AI Tools For Email Writing & Assistants
- 8 Data Science Competition Platforms Beyond Kaggle
- Data Analysis Books that You Can Buy
- Robotics Books that You Can Buy
- Data Visualization Books that You can Buy
How to create image of confusion matrix in Python
A confusion matrix is a table that summarizes the performance of an estimator, in terms of its accuracy and its false alarm rate.
There are two types of confusion matrices:
The first type is called a “true positives” (TP) to “false positives” (FP) matrix. This matrix shows the number of positives that were correctly identified as such, and the number of negatives that were incorrectly identified as positive. The second type is called a “true negatives” (TN) to “false negatives” (FN) matrix. This matrix shows the number of negatives that were correctly identified as such, and the number of positives that were incorrectly identified as negative.
How to create image of confusion matrix in Python
Solution 1:
OPTION 1:
After you get array of the confusion matrix from sklearn.metrics
, you can use matplotlib.pyplot.matshow()
or seaborn.heatmap
to generate the plot of the confusion matrix from that array.
e.g.
import pandas as pd
import seaborn as sn
import matplotlib.pyplot as plt
cfm = [[35, 0, 6],
[0, 0, 3],
[5, 50, 1]]
classes = ["0", "1", "2"]
df_cfm = pd.DataFrame(cfm, index = classes, columns = classes)
plt.figure(figsize = (10,7))
cfm_plot = sn.heatmap(df_cfm, annot=True)
cfm_plot.figure.savefig("cfm.png")
OPTION 2:
You can use plot_confusion_matrix()
from sklearn
to create image of confusion matrix directly from an estimater (i.e. classifier).
e.g.
cfm_plot = plot_confusion_matrix(<estimator>, <X>, <Y>)
cfm_plot.savefig("cfm.png")
Both options use savefig()
to save the result as the png file.
REF: https://scikit-learn.org/stable/modules/generated/sklearn.metrics.plot_confusion_matrix.html
Solution 2:
To see classification report visually, maybe a better method rather than saving odd plots, is saving it as a table, or some table-like object.
See sklearn's classification_report, it produces a nice table as an output, it has an argument output_dict
which is False
by default, pass this as true like
import json
from sklearn.metrics import classification_report
def save_json(obj, path):
with open(path, 'w') as jf:
json.dump(obj, jf)
report = classification_report(y_true, y_pred, output_dict=True)
save_json(report, 'path/to/save_dir/myreport.json'
you can also try to get dataframe of that resulting dict with
import pandas as pd
report_df = pd.DataFrame(report)
report_df.to_csv('saving/path/df.csv')
Thank you for reading the article.