How to plot a confusion matrix using scikit-learn and matplotlib in Python.
by vigneshchennai74 Updated: Feb 27, 2023
Solution Kit
A confusion matrix is a table that outlines the performance of a classification algorithm by comparing the predicted class labels to the true class labels. It provides a quick and intuitive way to assess the accuracy of a classification model and identify any patterns in the errors made by the model.
The scikit-learn library provides a function called confusion_matrix that can generate a confusion matrix from a set of true and predicted class labels. The plot_confusion_matrix function defined in the script takes this confusion matrix and plots it using the matplotlib library, making it easier to visualize and interpret.
This script can be helpful in many machine learning tasks, particularly in cases where we need to evaluate the performance of a classification model. It can also be useful in explaining the model's performance to non-technical stakeholders, as the visual representation of the confusion matrix is easy to understand. By plotting the confusion matrix, we can easily identify which classes the model is misclassifying and adjust the model or data accordingly.
Here is an example of how to plot a confusion matrix using scikit-learn and matplotlib in Python.
Preview of the output that you will get on running this code from your IDE
Code
In this solution we have used Sklearn library.
- 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 "sklearn: Plot confusion matrix combined across training test sets" in kandi. You can try any such use case!
note
At the end of the code type plt.show(), it display your confusion matrix.
Environment Tested
I tested this solution in the following versions. Be mindful of changes when working with other versions.
- The solution is created and tested in Vscode 1.75.1 version
- 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 combine the test and train accuracy in 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 combine the accuracyin confusion matrix in Python.
Dependent Library
scikit-learnby scikit-learn
scikit-learn: machine learning in Python
scikit-learnby scikit-learn
Python 54584 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.
Support
- For any support on kandi solution kits, please use the chat
- For further learning resources, visit the Open Weaver Community learning page.