Demystifying Confusion Matrix in Machine Learning [Astonishing]
Updated on Dec 30, 2024 | 5 min read | 5.4k views
Share:
For working professionals
For fresh graduates
More
Updated on Dec 30, 2024 | 5 min read | 5.4k views
Share:
Table of Contents
Machine Learning is an umbrella term under which it accumulates Data processing, deriving meaningful insights from the Data, and Data modeling. After being done with Data modeling, the model needs to be measured for its performance evaluation, and other industry-based parameters and one of the most common metrics is the Confusion Matrix.
It is a performance metric used in classification problems where the output classes maybe two or more and this matrix represents all the combinations of predicted values by a model with actual values associated with that input. It’s called ‘confusion matrix’ because going by the definitions it seems easy, but as we move forward to derive more valuable parameters, confusion arises regarding which parameter is best suited at a particular place.
It is used in places where the classification problem is highly imbalanced and one class dominates over other classes. In such scenarios, you may be surprised to see the accuracy of the model peaking at 99% but in reality, the model is highly biased towards the dominant class. There is very little possibility that you will get predictions for minority classes. Therefore, to test such an imbalanced dataset, we consider the confusion matrix.
Get Machine Learning Certification from the World’s top Universities. Earn Masters, Executive PGP, or Advanced Certificate Programs to fast-track your career.
The size of the matrix is directly proportional to the number of output classes. It is a square matrix where we assume the column headers as actual values and the row headers as model predictions. The values which are true and predicted true by the model are True Positives (TP), correct negative value predictions are True Negatives (TN), values which were negative but predicted as true are False Positives (FP) and positive values predicted as negative are False Negatives (FN). Have a look at this image:
Read: Types of classification algorithm in machine learning
A valid question arises that what we can do with this matrix. There are some important terminologies based on this:
Out of all the terms, precision and recall are most widely used. Their tradeoff is a useful measure of the success of a prediction. The desired model is supposed to have high precision and high recall, but this is only in perfectly separable data. In practical use cases, the data is highly unorganized and imbalanced.
The sklearn library provides a variety of functionalities to perform all the machine learning tasks with utmost accuracy and almost everything has been implemented here. Consider the famous Iris dataset with all import statements already done, the code for confusion matrix would be:
iris = datasets.load_iris()
X = iris.data
y = iris.target
class_names = iris.target_names
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
classifier = svm.SVC(kernel=‘linear’, C=0.01).fit(X_train, y_train)
plot_confusion_matrix(classifier, X_test, y_test,display_labels=class_names,cmap=plt.cm.Blues)
Note: The matrix returned by this has reversed sides, here on the left we have actual values and on the top, we have predicted values. If you want to avoid confusion, execute this function to get a detailed summary (classification report) instead of calculating it manually:
print(classification_report(y_true=y_test, y_pred=y_pred, target_names=class_names))
Also Read: Deep Learning Algorithm
This is the most common question that arises while modeling the Data and the solution lies in the problem’s statement domain. Consider these two cases:
The confusion matrix is a great method to evaluate a classification model. It gives the actual insight into how accurately the model has classified the classes based upon the inputs provided and how this class can be misclassified.
If you’re interested to learn more about machine learning, check out IIIT-B & upGrad’s PG Diploma in Machine Learning & AI which is designed for working professionals and offers 450+ hours of rigorous training, 30+ case studies & assignments, IIIT-B Alumni status, 5+ practical hands-on capstone projects & job assistance with top firms.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
Top Resources