0
0
Computer Visionml~5 mins

Evaluation and confusion matrix in Computer Vision

Choose your learning style9 modes available
Introduction

We use evaluation and confusion matrix to check how well a model is doing. It helps us see where the model is right or wrong.

After training a model to see how accurate it is.
When comparing two models to pick the better one.
To understand which classes the model confuses the most.
When tuning model settings to improve performance.
To explain model results to others in a simple way.
Syntax
Computer Vision
from sklearn.metrics import confusion_matrix, accuracy_score

# y_true: true labels
# y_pred: predicted labels
cm = confusion_matrix(y_true, y_pred)
accuracy = accuracy_score(y_true, y_pred)

confusion_matrix shows counts of correct and wrong predictions for each class.

accuracy_score gives the overall percentage of correct predictions.

Examples
This example shows a confusion matrix for two classes (0 and 1).
Computer Vision
y_true = [0, 1, 0, 1]
y_pred = [0, 0, 0, 1]
cm = confusion_matrix(y_true, y_pred)
print(cm)
This calculates accuracy as the fraction of correct predictions.
Computer Vision
accuracy = accuracy_score([1, 0, 1, 1], [1, 0, 0, 1])
print(f"Accuracy: {accuracy:.2f}")
Sample Model

This program shows how to compute and print the confusion matrix and accuracy for a small example.

Computer Vision
from sklearn.metrics import confusion_matrix, accuracy_score

# True labels for 6 samples
y_true = [0, 1, 2, 2, 0, 1]
# Model predictions
y_pred = [0, 2, 2, 2, 0, 0]

# Calculate confusion matrix
cm = confusion_matrix(y_true, y_pred)
# Calculate accuracy
accuracy = accuracy_score(y_true, y_pred)

print("Confusion Matrix:")
print(cm)
print(f"Accuracy: {accuracy:.2f}")
OutputSuccess
Important Notes

The confusion matrix rows represent true classes, columns represent predicted classes.

Diagonal values show correct predictions; off-diagonal values show mistakes.

Accuracy alone may not be enough if classes are imbalanced.

Summary

Evaluation helps us understand model performance clearly.

Confusion matrix breaks down predictions by class.

Accuracy gives a simple overall correctness score.