Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Confusion Matrix in Python

Confusion Matrix in Python

? Confusion Matrix in Python

A Confusion Matrix is a tool used to evaluate the performance of a classification model. It shows the number of correct and incorrect predictions, broken down by class. It provides insight into how well the model is performing and where it may be making errors.

The confusion matrix is especially useful for multi-class classification problems.


? Confusion Matrix Components

For a binary classification problem (e.g., classifying positive vs. negative):

Predicted PositivePredicted Negative
Actual PositiveTrue Positive (TP)False Negative (FN)
Actual NegativeFalse Positive (FP)True Negative (TN)
  • True Positive (TP): Correctly predicted positive class.

  • False Positive (FP): Incorrectly predicted positive class.

  • True Negative (TN): Correctly predicted negative class.

  • False Negative (FN): Incorrectly predicted negative class.

For multi-class classification, the confusion matrix can be extended to show the relationships between each class.


? Confusion Matrix in Python with sklearn

The sklearn library provides a convenient function confusion_matrix() to compute the confusion matrix for classification problems.

1?? Example: Binary Classification

import numpy as npfrom sklearn.metrics import confusion_matrix# True labels (actual values)y_true = [0, 1, 1, 0, 1, 1, 0, 0, 1, 0]# Predicted labels by the modely_pred = [0, 1, 1, 0, 0, 1, 0, 1, 1, 0]# Compute the confusion matrixcm = confusion_matrix(y_true, y_pred)print(cm)

? Output:

[[3 1] [1 5]]

This means:

  • 3 True Negatives (TN)

  • 1 False Positive (FP)

  • 1 False Negative (FN)

  • 5 True Positives (TP)


2?? Example: Multi-Class Classification

from sklearn.metrics import confusion_matrix# True labels (actual values)y_true = [0, 1, 2, 2, 0, 1, 1, 0, 2, 1]# Predicted labels by the modely_pred = [0, 0, 2, 2, 0, 2, 1, 0, 1, 1]# Compute the confusion matrixcm = confusion_matrix(y_true, y_pred)print(cm)

? Output:

[[3 0 0] [1 2 1] [0 1 2]]

This means:

  • Class 0: 3 correct predictions, 0 misclassifications.

  • Class 1: 2 correct predictions, 1 misclassification as Class 2.

  • Class 2: 2 correct predictions, 1 misclassification as Class 1.


? Visualizing the Confusion Matrix

It's often helpful to visualize the confusion matrix, especially for multi-class problems. You can use seaborn to create a heatmap for better interpretation.

Example with Heatmap:

import seaborn as snsimport matplotlib.pyplot as plt# Compute the confusion matrixcm = confusion_matrix(y_true, y_pred)# Plot heatmapsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=['Class 0', 'Class 1', 'Class 2'], yticklabels=['Class 0', 'Class 1', 'Class 2'])plt.xlabel('Predicted')plt.ylabel('Actual')plt.title('Confusion Matrix')plt.show()

This will display a heatmap with the values of the confusion matrix.


? Metrics Derived from the Confusion Matrix

From the confusion matrix, you can derive several important performance metrics:

  1. Accuracy: The percentage of correct predictions.

    Accuracy=TP+TNTP+TN+FP+FN\text{Accuracy} = \frac{TP + TN}{TP + TN + FP + FN}
  2. Precision: The ability of the classifier to avoid false positives.

    Precision=TPTP+FP\text{Precision} = \frac{TP}{TP + FP}
  3. Recall (Sensitivity): The ability of the classifier to avoid false negatives.

    Recall=TPTP+FN\text{Recall} = \frac{TP}{TP + FN}
  4. F1-Score: The harmonic mean of precision and recall.

    F1=2×Precision×RecallPrecision+RecallF1 = 2 \times \frac{\text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}}
  5. Specificity: The ability of the classifier to correctly identify negative instances.

    Specificity=TNTN+FP\text{Specificity} = \frac{TN}{TN + FP}

Example of Calculating Accuracy:

accuracy = (cm[0][0] + cm[1][1]) / cm.sum()print("Accuracy:", accuracy)

? Summary

  • Confusion Matrix helps to evaluate the performance of classification models.

  • Metrics derived from the confusion matrix include Accuracy, Precision, Recall, F1-Score, and Specificity.

  • Visualization with a heatmap makes it easier to interpret the confusion matrix.

  • sklearn.metrics.confusion_matrix() helps to compute the confusion matrix easily.


Let me know if you need further details on confusion matrices or related metrics! ?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql