Plotting multiple confusion matrices is useful for evaluating and comparing the performance of different machine-learning models.
In scikit-learn, you can plot multiple confusion matrices using the plot_confusion_matrix function. This function takes a trained classifier object, and the test data as inputs, and it plots a confusion matrix that shows the classifier's performance on the test data. Plotting multiple confusion matrices can be useful in several ways:
- Comparison of models: By plotting the confusion matrices of multiple models, you can compare their performance on the same dataset.
- Model selection: If you are still determining which model to choose for your problem, you can compare multiple models by plotting their confusion matrices. This can give you a visual representation of their performance and help you decide which model to use.
- Model evaluation: Confusion matrices provide a simple and effective way to evaluate the performance of a model. By plotting multiple confusion matrices, you can get a clearer picture of the strengths and weaknesses of different models and see how they compare in terms of overall accuracy, false positives, false negatives, and other metrics.
- Model improvement: By comparing the confusion matrices of different models, you can identify areas where models can be improved.
This code uses various machine learning algorithms from the sci-kit-learn library to classify the iris dataset. The iris dataset is loaded using the load_iris() function from sklearn.datasets module. plt.subplots is a function in the matplotlib library that creates a figure and multiple subplots within it. The function returns a figure and an array of subplots, which can be used to create multiple plots within the same figure.
Fig1.Preview of the output that you will get on running this code from your IDE
from sklearn.ensemble import AdaBoostClassifier, GradientBoostingClassifier from sklearn.svm import SVC from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from matplotlib import pyplot as plt from sklearn.datasets import load_iris from sklearn.metrics import plot_confusion_matrix data = load_iris() X = data.data y = data.target X_train, X_test, y_train, y_test = train_test_split(X, y) classifiers = [LogisticRegression(solver='lbfgs'), AdaBoostClassifier(), GradientBoostingClassifier(), SVC()] for cls in classifiers: cls.fit(X_train, y_train) fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(15,10)) for cls, ax in zip(classifiers, axes.flatten()): plot_confusion_matrix(cls, X_test, y_test, ax=ax, cmap='Blues', display_labels=data.target_names) ax.title.set_text(type(cls).__name__) plt.tight_layout() plt.show()
- Copy the code using the "Copy" button above, and paste it in a Python file in your IDE.
- Run the file to plot multiple confusion matrix in single page
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 "Plot multiple confusion matrices with plot_confusion_matrix" in kandi. You can try any such use case!
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.
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 multiple 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.