![]() |
VOOZH | about |
A confusion matrix is a table that helps evaluate how well a classification model performs. It compares the actual values with the predicted ones, providing a detailed view of correct and incorrect classifications. The structure consists of rows for actual classes and columns for predicted classes.
The key components of a confusion matrix include:
These values help us derive performance metrics like accuracy, precision, recall and specificity.
R provides various packages for working with confusion matrices, such as caret, MLmetrics and yardstick. Below we explore creating and interpreting confusion matrices using the caret package.
We create a confusion matrix from actual and predicted binary class labels and evaluate model performance.
Output:
The model achieved perfect classification with an accuracy of 1. All predicted labels matched the actual ones, resulting in no false positives or false negatives.
We build a classification model on the Iris dataset and evaluate its performance using a confusion matrix.
Output:
The model performs well with an accuracy of 93.33%. It perfectly classifies Setosa and Virginica but misclassifies two Versicolor instances as Virginica.
From the matrix, we calculate key performance metrics:
These metrics help assess how well the model generalizes and where it may need improvement.