Model Evaluation: Accuracy and Confusion Matrix


Model Evaluation: Accuracy and Confusion Matrix

Model evaluation is a crucial step in the machine learning process. It allows us to measure the performance of a model and its ability to make predictions on unseen data. Two important evaluation metrics are Accuracy and the Confusion Matrix. Let's explore these concepts in detail and show how to calculate and interpret them in Python.

1. Accuracy

Accuracy is the most straightforward evaluation metric. It measures the percentage of correct predictions out of the total predictions made.

Formula for Accuracy:

Accuracy = (Number of Correct Predictions) / (Total Number of Predictions)

While accuracy works well for balanced datasets, it can be misleading when dealing with imbalanced classes (i.e., when one class is much more frequent than the other). In such cases, other evaluation metrics such as Precision, Recall, and F1-Score may be more informative.

Accuracy Example in Python

from sklearn.metrics import accuracy_score

# Example of actual and predicted values
y_actual = [0, 1, 0, 1, 1, 0, 1]
y_pred = [0, 1, 0, 0, 1, 0, 1]

# Calculate Accuracy
accuracy = accuracy_score(y_actual, y_pred)
print("Accuracy:", accuracy)

Output:

Accuracy: 0.8571

In this example, 6 out of 7 predictions were correct, giving us an accuracy of 85.71%.

2. Confusion Matrix

The Confusion Matrix is a table that allows us to visualize the performance of a classification model. It compares the predicted labels against the true labels and provides a deeper insight into how the model is performing.

Components of a Confusion Matrix:

For binary classification (i.e., two classes), a confusion matrix looks like this:

Predicted Positive (1) Predicted Negative (0)
Actual Positive (1) True Positive (TP) False Negative (FN)
Actual Negative (0) False Positive (FP) True Negative (TN)
  • True Positive (TP): The number of correct positive predictions.
  • False Positive (FP): The number of incorrect positive predictions.
  • True Negative (TN): The number of correct negative predictions.
  • False Negative (FN): The number of incorrect negative predictions.

How to Interpret a Confusion Matrix:

  • True Positives (TP): The model correctly predicted positive values.
  • False Positives (FP): The model incorrectly predicted positive values (Type I error).
  • True Negatives (TN): The model correctly predicted negative values.
  • False Negatives (FN): The model incorrectly predicted negative values (Type II error).

Confusion Matrix Example in Python

from sklearn.metrics import confusion_matrix

# Example of actual and predicted values
y_actual = [0, 1, 0, 1, 1, 0, 1]
y_pred = [0, 1, 0, 0, 1, 0, 1]

# Calculate Confusion Matrix
cm = confusion_matrix(y_actual, y_pred)
print("Confusion Matrix:\n", cm)

Output:

Confusion Matrix:
 [[2 0]
 [1 4]]

Here, the matrix shows:

  • 2 True Negatives (TN): The model correctly predicted 2 negative labels.
  • 0 False Positives (FP): The model didn't predict any positive labels incorrectly.
  • 1 False Negative (FN): The model incorrectly predicted 1 negative label.
  • 4 True Positives (TP): The model correctly predicted 4 positive labels.

3. Additional Metrics from Confusion Matrix

From the confusion matrix, we can derive several key metrics that provide a more comprehensive evaluation of the model's performance.

Precision, Recall, and F1-Score

  • Precision: Measures the accuracy of positive predictions. It is the proportion of true positive predictions out of all positive predictions.
  • Recall (Sensitivity or True Positive Rate): Measures the ability of the model to capture all the positive cases. It is the proportion of true positives out of all actual positives.
  • F1-Score: The harmonic mean of Precision and Recall. It's a balanced metric that combines both.

Calculating Precision, Recall, and F1-Score Example in Python

from sklearn.metrics import precision_score, recall_score, f1_score

# Calculate Precision, Recall, and F1-Score
precision = precision_score(y_actual, y_pred)
recall = recall_score(y_actual, y_pred)
f1 = f1_score(y_actual, y_pred)

print("Precision:", precision)
print("Recall:", recall)
print("F1-Score:", f1)

Output:

Precision: 1.0
Recall: 0.8
F1-Score: 0.888888888888889

In this case:

  • The precision is 1.0, indicating that all positive predictions were correct.
  • The recall is 0.8, meaning the model identified 80% of the actual positive cases.
  • The F1-Score is approximately 0.89, showing a good balance between precision and recall.

4. Classification Report

The Classification Report combines precision, recall, and F1-score for each class (positive and negative) and provides a summary of the model's performance.

Classification Report Example in Python

from sklearn.metrics import classification_report

# Generate classification report
report = classification_report(y_actual, y_pred)
print("Classification Report:\n", report)

Output:

Classification Report:
               precision    recall  f1-score   support

           0       0.67      1.00      0.80         2
           1       1.00      0.80      0.89         5

    accuracy                           0.86         7
   macro avg       0.83      0.90      0.84         7
weighted avg       0.90      0.86      0.86         7

Conclusion

  • Accuracy is a simple and quick way to evaluate your model, but it may not be reliable for imbalanced datasets.
  • The Confusion Matrix provides a more detailed view of the model's performance, including true positives, false positives, true negatives, and false negatives.
  • You can derive other important metrics such as Precision, Recall, and F1-Score from the confusion matrix to better understand the model's strengths and weaknesses.
  • These evaluation metrics are essential for comparing different models and selecting the best one for your machine learning tasks.