How to Visualize Confusion Matrix using Scikit-Learn
by vigneshchennai74 Updated: Feb 15, 2023
This program is calculating and visualizes a confusion matrix for a classification problem using the Scikit-Learn library in Python. The confusion matrix is a table that summarizes the performance of a classification algorithm by showing the number of true positives, true negatives, false positives, and false negatives for each class.
The confusion_matrix() function from scikit-learn takes the y_true and y_pred arrays as inputs and calculates a confusion matrix, which shows the number of true positives, true negatives, false positives, and false negatives for each class.
The ConfusionMatrixDisplay() function from scikit-learn takes the confusion matrix as input and creates a plot of the matrix. The resulting plot displays the confusion matrix as a grid of colored squares, where the color of each square represents the number of data points that fall into that category.
The plt.show() function displays the plot on the screen. Matplotlib is used to plot the confusion matrix generated by the Scikit-Learn library. The ConfusionMatrixDisplay() function from Scikit-Learn creates a plot of the confusion matrix using Matplotlib.
This helps users to evaluate the performance of a classification algorithm and understand its strengths and weaknesses.
Preview of the output that you will get on running this code from your IDE
In this solution we have used Sklearn library.
from sklearn.metrics import classification_report, confusion_matrix, ConfusionMatrixDisplay from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt y_true = [2, 0, 2, 2, 0, 1] y_pred = [0, 0, 2, 2, 0, 2] confusion_matrix1 =confusion_matrix(y_true, y_pred) display = ConfusionMatrixDisplay(confusion_matrix1).plot() plt.show()
- Copy the code using the "Copy" button above, and paste it in a Python file in your IDE.
- Run the file to get the output
I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.
I found this code snippet by searching for "ConfusionMatrixDisplay(confusion_matrix).plot() doesn" in kandi. You can try any such use case!
I tested this solution in the following versions. Be mindful of changes when working with other versions.
- The solution is created in Python 3.7.15 version
- The solution is tested on scikit-learn 1.0.2 version
Using this solution, we are able to plot the confusion matrix using Scikit learn library in Python with simple steps. This process also facilities an easy to use, hassle free method to create a hands-on working version of code which would help us plot a confusion matrix in Python.
scikit-learn: machine learning in Python
Python 54382 Version:1.2.2 License: Permissive (BSD-3-Clause)
If you do not have Scikit-learn that is required to run this code, you can install it by clicking on the above link and copying the pip Install command from the Scikit-learn page in kandi.
You can search for any dependent library on kandi like Scikit-learn.
- For any support on kandi solution kits, please use the chat
- For further learning resources, visit the Open Weaver Community learning page.