Single logo
  • Courses
  • Articles
  • About
Join for freeJoin free
  1. Blog
  2. /
  3. S26L04 -Confusion Matrix 3D

Machine Leaning Articles
1 views

S26L04 -Confusion Matrix 3D


May 15, 2025

Mastering Confusion Matrices: A Comprehensive Guide for Machine Learning Practitioners

Table of Contents

  1. What is a Confusion Matrix?
  2. Components of a Confusion Matrix
    • True Positive (TP)
    • True Negative (TN)
    • False Positive (FP)
    • False Negative (FN)
  3. Understanding Confusion Matrix with Multiple Classes
  4. Building a Confusion Matrix Using Scikit-Learn
  5. Visualizing the Confusion Matrix
  6. Interpreting Model Performance Metrics
    • Accuracy
    • Precision
    • Recall
    • F1 Score
    • Specificity
  7. Advanced: Handling Multi-Class Confusion Matrices
  8. Practical Implementation with Weather Prediction Dataset
  9. Conclusion

What is a Confusion Matrix?

A confusion matrix is a tabular representation of the performance of a classification model. It allows you to visualize how well your model is performing by comparing the actual target values against those predicted by the model. Each row of the matrix represents the instances in an actual class, while each column represents the instances in a predicted class, or vice versa. This structure makes it easy to identify not only the types of errors your model is making but also their frequency.

Confusion Matrix Overview

Figure 1: Basic structure of a confusion matrix.

Components of a Confusion Matrix

Understanding the individual components of a confusion matrix is crucial for interpreting the results effectively. The matrix consists of four key metrics:

True Positive (TP)

  • Definition: The number of instances correctly classified as positive.
  • Example: If the model predicts that it will rain tomorrow and it actually rains, it's a True Positive.

True Negative (TN)

  • Definition: The number of instances correctly classified as negative.
  • Example: If the model predicts that it will not rain tomorrow and it indeed does not rain, it's a True Negative.

False Positive (FP)

  • Definition: The number of instances incorrectly classified as positive.
  • Example: If the model predicts that it will rain tomorrow but it does not, it's a False Positive. This is also known as a Type I error.

False Negative (FN)

  • Definition: The number of instances incorrectly classified as negative.
  • Example: If the model predicts that it will not rain tomorrow but it actually does, it's a False Negative. This is also known as a Type II error.
Confusion Matrix Components

Figure 2: Breakdown of TP, TN, FP, and FN within a confusion matrix.

Understanding Confusion Matrix with Multiple Classes

While binary classification involves two classes (positive and negative), multi-class classification extends the confusion matrix to accommodate more classes. For instance, in a dataset with three classes—setosa, versicolor, and virginica—the confusion matrix becomes a 3x3 grid. Each row represents the actual class, and each column represents the predicted class. The diagonal elements still represent correct predictions, while off-diagonal elements indicate various types of misclassifications.

Multi-Class Confusion Matrix

Figure 3: Example of a multi-class confusion matrix.

Building a Confusion Matrix Using Scikit-Learn

Python's scikit-learn library offers robust tools for generating and analyzing confusion matrices. Below is a step-by-step guide to building a confusion matrix using scikit-learn, complemented by a practical example.

Step 1: Import Necessary Libraries

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, OneHotEncoder, StandardScaler
from sklearn.metrics import confusion_matrix, plot_confusion_matrix, accuracy_score
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt

Step 2: Load and Prepare the Dataset

For demonstration, we'll use the Weather Australia dataset.

# Load the dataset
data = pd.read_csv('weatherAUS.csv')

# Define features and target variable
X = data.iloc[:, :-1]
y = data.iloc[:, -1]

# Handle missing data
from sklearn.impute import SimpleImputer
import numpy as np

# Numeric features
numerical_cols = X.select_dtypes(include=['int64', 'float64']).columns
imp_mean = SimpleImputer(missing_values=np.nan, strategy='mean')
X[numerical_cols] = imp_mean.fit_transform(X[numerical_cols])

# Categorical features
string_cols = X.select_dtypes(include=['object']).columns
imp_mode = SimpleImputer(missing_values=np.nan, strategy='most_frequent')
X[string_cols] = imp_mode.fit_transform(X[string_cols])

# Encoding categorical variables
X = pd.get_dummies(X, drop_first=True)

# Encode target variable
le = LabelEncoder()
y = le.fit_transform(y)

Step 3: Split the Dataset

# Split into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.20, random_state=1
)

Step 4: Feature Scaling

# Standardize the features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

Step 5: Train a Classification Model

We'll use Logistic Regression for this example.

# Initialize and train the model
model = LogisticRegression(random_state=0, max_iter=200)
model.fit(X_train, y_train)

# Make predictions
y_pred = model.predict(X_test)

Step 6: Generate the Confusion Matrix

# Calculate accuracy
accuracy = accuracy_score(y_pred, y_test)
print(f'Accuracy: {accuracy:.4f}')

# Generate confusion matrix
cm = confusion_matrix(y_test, y_pred)
print('Confusion Matrix:')
print(cm)

Output:

Accuracy: 0.8297
Confusion Matrix:
[[21087  1058]
 [ 3786  2508]]

Visualizing the Confusion Matrix

Visualization aids in the intuitive understanding of model performance. Scikit-learn provides built-in functions to plot confusion matrices effortlessly.

# Plot confusion matrix
plot_confusion_matrix(model, X_test, y_test, display_labels=le.classes_)
plt.title('Confusion Matrix')
plt.show()
Confusion Matrix Plot

Figure 4: Confusion matrix visualization using scikit-learn.

Interpreting Model Performance Metrics

Beyond accuracy, the confusion matrix allows for the calculation of several other performance metrics:

Accuracy

  • Definition: The proportion of correctly classified instances out of the total instances.
  • Formula: \[ \text{Accuracy} = \frac{TP + TN}{TP + TN + FP + FN} \]
  • Interpretation: While useful, accuracy can be misleading, especially in imbalanced datasets.

Precision

  • Definition: The ratio of correctly predicted positive observations to the total predicted positives.
  • Formula: \[ \text{Precision} = \frac{TP}{TP + FP} \]
  • Interpretation: High precision indicates that an algorithm returned substantially more relevant results than irrelevant ones.

Recall (Sensitivity)

  • Definition: The ratio of correctly predicted positive observations to all observations in the actual class.
  • Formula: \[ \text{Recall} = \frac{TP}{TP + FN} \]
  • Interpretation: High recall indicates that an algorithm returned most of the relevant results.

F1 Score

  • Definition: The weighted average of Precision and Recall.
  • Formula: \[ F1 = 2 \times \frac{\text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}} \]
  • Interpretation: The F1 score conveys the balance between Precision and Recall.

Specificity

  • Definition: The ratio of correctly predicted negative observations to all actual negatives.
  • Formula: \[ \text{Specificity} = \frac{TN}{TN + FP} \]
  • Interpretation: High specificity indicates that the model effectively identifies negative cases.

Advanced: Handling Multi-Class Confusion Matrices

In scenarios with more than two classes, the confusion matrix expands to a multi-dimensional grid. Each diagonal element represents the correctly classified instances for each class, while off-diagonal elements indicate various misclassifications.

Example: Consider a three-class classification problem with classes A, B, and C.

               Predicted
               A    B    C
Actual A      50     2     3
       B       5    45     5
       C       2     3    48
  • True Positives for Class A: 50
  • False Positives for Class A: 5 (from B) + 2 (from C) = 7
  • False Negatives for Class A: 2 (to B) + 3 (to C) = 5
  • True Negatives for Class A: Total - (TP + FP + FN) = 100 - (50 + 7 + 5) = 38

Scikit-learn's confusion_matrix function seamlessly handles multi-class scenarios, providing a clear matrix that facilitates detailed performance analysis.

Practical Implementation with Weather Prediction Dataset

To solidify the concepts, let's walk through a practical example using the Weather Australia dataset. This dataset involves predicting whether it will rain the next day based on various weather attributes.

Step-by-Step Implementation

  1. Data Preprocessing:
    • Handle missing values using SimpleImputer.
    • Encode categorical variables using one-hot encoding.
    • Encode the target variable using LabelEncoder.
  2. Feature Scaling:
    • Standardize the features to ensure that each contributes equally to the model performance.
  3. Model Training:
    • Train multiple classification models such as K-Nearest Neighbors, Logistic Regression, Gaussian Naive Bayes, Support Vector Machines, Decision Trees, Random Forests, AdaBoost, and XGBoost.
  4. Evaluation:
    • Compute accuracy scores for each model.
    • Generate and visualize confusion matrices to understand the distribution of predictions.

Sample Code Snippets

Training a Logistic Regression Model:

from sklearn.linear_model import LogisticRegression

# Initialize the model
LRM = LogisticRegression(random_state=0, max_iter=200)

# Train the model
LRM.fit(X_train, y_train)

# Predict on test data
y_pred = LRM.predict(X_test)

# Evaluate accuracy
print(accuracy_score(y_pred, y_test))

Output:

0.8296705228735187

Generating Confusion Matrix:

from sklearn.metrics import confusion_matrix, plot_confusion_matrix

# Compute confusion matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)

# Plot confusion matrix
plot_confusion_matrix(LRM, X_test, y_test, display_labels=le.classes_)
plt.title('Logistic Regression Confusion Matrix')
plt.show()

Output:

[[21087  1058]
 [ 3786  2508]]
Logistic Regression Confusion Matrix

Figure 5: Confusion matrix for Logistic Regression model.

Comparative Accuracy of Multiple Models:

from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
import xgboost as xgb

# Initialize models
models = {
    'KNN': KNeighborsClassifier(n_neighbors=3),
    'Logistic Regression': LogisticRegression(random_state=0, max_iter=200),
    'GaussianNB': GaussianNB(),
    'SVC': SVC(),
    'Decision Tree': DecisionTreeClassifier(),
    'Random Forest': RandomForestClassifier(n_estimators=500, max_depth=5),
    'AdaBoost': AdaBoostClassifier(),
    'XGBoost': xgb.XGBClassifier(use_label_encoder=False, eval_metric='mlogloss')
}

# Train and evaluate models
for name, model in models.items():
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)
    accuracy = accuracy_score(y_pred, y_test)
    print(f'{name} Accuracy: {accuracy:.4f}')

Sample Output:

KNN Accuracy: 0.8003
Logistic Regression Accuracy: 0.8297
GaussianNB Accuracy: 0.7960
SVC Accuracy: 0.8282
Decision Tree Accuracy: 0.8302
Random Forest Accuracy: 0.8302
AdaBoost Accuracy: 0.8299
XGBoost Accuracy: 0.8302

From the output, it's evident that Decision Tree, Random Forest, and XGBoost models exhibit the highest accuracy, closely followed by Logistic Regression and AdaBoost.

Conclusion

Confusion matrices are indispensable for evaluating the performance of classification models. They provide a granular view of how models perform across different classes, highlighting both strengths and areas needing improvement. By mastering the construction and interpretation of confusion matrices, along with complementary metrics like precision, recall, and F1 score, machine learning practitioners can develop more robust and reliable models. Leveraging tools like scikit-learn simplifies this process, allowing for efficient model evaluation and iterative improvement. As you continue to explore and implement machine learning models, integrating confusion matrices into your evaluation pipeline will undoubtedly enhance your analytical capabilities and model efficacy.


For more detailed examples and advanced techniques, refer to the scikit-learn documentation on Confusion Matrices.

Share Article

Back to Blog

Single logo

Empowering Education, One Course at a Time

About
About usContact usFAQs
Legal
Terms of ServicePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.