Machine learning / Model evaluation and validation
Confusion Matrix and Performance Metrics in Python
Contents
- Introduction to Confusion Matrix
- Components of a Confusion Matrix
- Accuracy: A Basic Performance Metric
- Precision: Focusing on Positive Predictions
- Recall: Capturing All Positive Cases
- F1 Score: Balancing Precision and Recall
- Specificity: Handling True Negatives
- ROC Curve: Visualizing Model Performance
- Precision-Recall Curve: An Alternative Visualization
- Error Analysis: Understanding Model Mistakes
- Bias-Variance Tradeoff
- Cross-Validation: Assessing Model Generalization
- Real-Life Example: Image Classification
- Real-Life Example: Sentiment Analysis
- Additional Resources
Introduction to Confusion Matrix
A confusion matrix is a fundamental tool in machine learning for evaluating classification models. It provides a tabular summary of the model's predictions compared to the actual outcomes, allowing us to assess various performance metrics and identify areas for improvement.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
# Sample data
y_true = [0, 1, 0, 1, 0, 1, 0, 1, 1, 0]
y_pred = [0, 1, 0, 1, 1, 1, 0, 0, 1, 0]
# Create confusion matrix
cm = confusion_matrix(y_true, y_pred)
# Plot confusion matrix
plt.imshow(cm, cmap='Blues')
plt.title('Confusion Matrix')
plt.colorbar()
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.xticks([0, 1], ['Class 0', 'Class 1'])
plt.yticks([0, 1], ['Class 0', 'Class 1'])
# Add text annotations
for i in range(2):
for j in range(2):
plt.text(j, i, str(cm[i, j]), ha='center', va='center')
plt.show()
Components of a Confusion Matrix
A confusion matrix consists of four key components: True Positives (TP), True Negatives (TN), False Positives (FP), and False Negatives (FN). These components help us understand how well our model is performing across different classes.
import pandas as pd
# Create a sample confusion matrix
data = {
'Actual Positive': [50, 10],
'Actual Negative': [5, 35]
}
index = ['Predicted Positive', 'Predicted Negative']
cm_df = pd.DataFrame(data, index=index)
print("Confusion Matrix:")
print(cm_df)
print("\nComponents:")
print(f"True Positives (TP): {cm_df.loc['Predicted Positive', 'Actual Positive']}")
print(f"True Negatives (TN): {cm_df.loc['Predicted Negative', 'Actual Negative']}")
print(f"False Positives (FP): {cm_df.loc['Predicted Positive', 'Actual Negative']}")
print(f"False Negatives (FN): {cm_df.loc['Predicted Negative', 'Actual Positive']}")
Accuracy: A Basic Performance Metric
Accuracy is the simplest and most intuitive performance metric derived from a confusion matrix. It represents the proportion of correct predictions (both true positives and true negatives) among the total number of cases examined.
def calculate_accuracy(cm):
total = cm.sum()
correct = cm.trace()
return correct / total
# Using the confusion matrix from the previous slide
accuracy = calculate_accuracy(cm_df.values)
print(f"Accuracy: {accuracy:.2f}")
# Visualize accuracy
plt.figure(figsize=(6, 4))
plt.bar(['Incorrect', 'Correct'], [1 - accuracy, accuracy])
plt.title('Model Accuracy')
plt.ylabel('Proportion')
plt.ylim(0, 1)
for i, v in enumerate([1 - accuracy, accuracy]):
plt.text(i, v, f'{v:.2f}', ha='center', va='bottom')
plt.show()
Precision: Focusing on Positive Predictions
Precision measures the accuracy of positive predictions. It is particularly useful when the cost of false positives is high. Precision is calculated as the ratio of true positives to the total number of positive predictions.
def calculate_precision(cm):
tp = cm[0, 0]
fp = cm[0, 1]
return tp / (tp + fp)
precision = calculate_precision(cm_df.values)
print(f"Precision: {precision:.2f}")
# Visualize precision
plt.figure(figsize=(6, 4))
plt.pie([precision, 1 - precision], labels=['True Positive', 'False Positive'],
autopct='%1.1f%%', startangle=90)
plt.title('Precision: Composition of Positive Predictions')
plt.axis('equal')
plt.show()
Recall: Capturing All Positive Cases
Recall, also known as sensitivity or true positive rate, measures the proportion of actual positive cases that were correctly identified. It is crucial in scenarios where missing a positive case can have severe consequences, such as in medical diagnostics.
def calculate_recall(cm):
tp = cm[0, 0]
fn = cm[1, 0]
return tp / (tp + fn)
recall = calculate_recall(cm_df.values)
print(f"Recall: {recall:.2f}")
# Visualize recall
plt.figure(figsize=(6, 4))
plt.pie([recall, 1 - recall], labels=['True Positive', 'False Negative'],
autopct='%1.1f%%', startangle=90)
plt.title('Recall: Proportion of Actual Positives Correctly Identified')
plt.axis('equal')
plt.show()
F1 Score: Balancing Precision and Recall
The F1 score is the harmonic mean of precision and recall, providing a single score that balances both metrics. It is particularly useful when you have an uneven class distribution and need to find an optimal balance between precision and recall.
def calculate_f1_score(precision, recall):
return 2 * (precision * recall) / (precision + recall)
f1 = calculate_f1_score(precision, recall)
print(f"F1 Score: {f1:.2f}")
# Visualize F1 score
metrics = ['Precision', 'Recall', 'F1 Score']
values = [precision, recall, f1]
plt.figure(figsize=(8, 4))
plt.bar(metrics, values)
plt.title('Comparison of Precision, Recall, and F1 Score')
plt.ylim(0, 1)
for i, v in enumerate(values):
plt.text(i, v, f'{v:.2f}', ha='center', va='bottom')
plt.show()
Specificity: Handling True Negatives
Specificity, also known as the true negative rate, measures the proportion of actual negative cases that were correctly identified. It is particularly important in scenarios where correctly identifying negative cases is crucial.
def calculate_specificity(cm):
tn = cm[1, 1]
fp = cm[0, 1]
return tn / (tn + fp)
specificity = calculate_specificity(cm_df.values)
print(f"Specificity: {specificity:.2f}")
# Visualize specificity
plt.figure(figsize=(6, 4))
plt.pie([specificity, 1 - specificity], labels=['True Negative', 'False Positive'],
autopct='%1.1f%%', startangle=90)
plt.title('Specificity: Proportion of Actual Negatives Correctly Identified')
plt.axis('equal')
plt.show()
ROC Curve: Visualizing Model Performance
The Receiver Operating Characteristic (ROC) curve is a graphical representation of a classification model's performance across various threshold settings. It plots the True Positive Rate (Recall) against the False Positive Rate (1 - Specificity).
from sklearn.metrics import roc_curve, auc
import numpy as np
# Generate sample data
np.random.seed(42)
y_true = np.random.randint(0, 2, 1000)
y_scores = np.random.rand(1000)
fpr, tpr, thresholds = roc_curve(y_true, y_scores)
roc_auc = auc(fpr, tpr)
plt.figure(figsize=(8, 6))
plt.plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (AUC = {roc_auc:.2f})')
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic (ROC) Curve')
plt.legend(loc="lower right")
plt.show()
Precision-Recall Curve: An Alternative Visualization
The Precision-Recall curve is another way to visualize a model's performance, particularly useful for imbalanced datasets. It plots Precision against Recall for different threshold values.
from sklearn.metrics import precision_recall_curve
precision, recall, _ = precision_recall_curve(y_true, y_scores)
plt.figure(figsize=(8, 6))
plt.plot(recall, precision, color='blue', lw=2)
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title('Precision-Recall Curve')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.show()
Error Analysis: Understanding Model Mistakes
Error analysis involves examining the specific instances where the model makes mistakes. This process helps identify patterns in errors and guides improvements in the model or data collection.
import pandas as pd
import numpy as np
# Create sample data
np.random.seed(42)
features = np.random.rand(100, 3)
true_labels = np.random.choice(['A', 'B', 'C'], 100)
predicted_labels = np.random.choice(['A', 'B', 'C'], 100)
# Create DataFrame
df = pd.DataFrame(features, columns=['Feature 1', 'Feature 2', 'Feature 3'])
df['True Label'] = true_labels
df['Predicted Label'] = predicted_labels
df['Error'] = df['True Label'] != df['Predicted Label']
# Display errors
errors = df[df['Error']].sample(5)
print("Sample of errors:")
print(errors)
# Visualize errors
plt.figure(figsize=(10, 6))
plt.scatter(df[~df['Error']]['Feature 1'], df[~df['Error']]['Feature 2'],
c='green', label='Correct')
plt.scatter(df[df['Error']]['Feature 1'], df[df['Error']]['Feature 2'],
c='red', label='Error')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('Error Distribution in Feature Space')
plt.legend()
plt.show()
Bias-Variance Tradeoff
The bias-variance tradeoff is a fundamental concept in machine learning that helps us understand the sources of prediction errors. Bias refers to the error introduced by approximating a real-world problem with a simplified model, while variance is the error from sensitivity to small fluctuations in the training set.
import numpy as np
import matplotlib.pyplot as plt
# Generate sample data
np.random.seed(42)
X = np.linspace(0, 10, 100)
y = 3*X + 2 + np.random.normal(0, 2, 100)
# Fit models of different complexities
degrees = [1, 3, 15]
plt.figure(figsize=(12, 4))
for i, degree in enumerate(degrees):
plt.subplot(1, 3, i+1)
coeffs = np.polyfit(X, y, degree)
y_pred = np.polyval(coeffs, X)
plt.scatter(X, y, alpha=0.6)
plt.plot(X, y_pred, color='r')
plt.title(f'Degree {degree} Polynomial')
plt.xlabel('X')
plt.ylabel('y')
plt.tight_layout()
plt.show()
Cross-Validation: Assessing Model Generalization
Cross-validation is a technique used to assess how well a model will generalize to an independent dataset. It helps in detecting overfitting and provides a more robust estimate of model performance.
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
# Generate a random classification dataset
X, y = make_classification(n_samples=1000, n_features=20, n_classes=2, random_state=42)
# Create a logistic regression model
model = LogisticRegression()
# Perform 5-fold cross-validation
cv_scores = cross_val_score(model, X, y, cv=5)
print("Cross-validation scores:", cv_scores)
print("Mean CV score:", cv_scores.mean())
# Visualize cross-validation results
plt.figure(figsize=(8, 6))
plt.boxplot(cv_scores)
plt.title('5-Fold Cross-Validation Results')
plt.ylabel('Accuracy')
plt.show()
Real-Life Example: Image Classification
In this example, we'll use a pre-trained ResNet50 model to classify images. We'll demonstrate how to use a confusion matrix and various performance metrics in a real-world scenario.
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions
from tensorflow.keras.preprocessing import image
import numpy as np
import matplotlib.pyplot as plt
# Load pre-trained ResNet50 model
model = ResNet50(weights='imagenet')
# Function to predict image class
def predict_image(img_path):
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
return decode_predictions(preds, top=1)[0][0]
# Sample predictions (you would typically have more)
true_labels = ['cat', 'dog', 'bird', 'fish']
predictions = [
predict_image('path_to_cat_image.jpg'),
predict_image('path_to_dog_image.jpg'),
predict_image('path_to_bird_image.jpg'),
predict_image('path_to_fish_image.jpg')
]
# Create confusion matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(true_labels, [pred[1] for pred in predictions])
# Plot confusion matrix
plt.figure(figsize=(8, 6))
plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
plt.title('Confusion Matrix: Image Classification')
plt.colorbar()
tick_marks = np.arange(len(true_labels))
plt.xticks(tick_marks, true_labels, rotation=45)
plt.yticks(tick_marks, true_labels)
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()
Real-Life Example: Sentiment Analysis
In this example, we'll perform sentiment analysis on movie reviews using a simple logistic regression model. We'll demonstrate how to use various performance metrics in a natural language processing task.
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, classification_report
import numpy as np
# Sample movie reviews and sentiments
reviews = [
"This movie was excellent! I loved every minute of it.",
"Terrible film, waste of time and money.",
"Average movie, nothing special but not bad either.",
"I was pleasantly surprised by how good this movie was!",
"Boring plot, poor acting, overall disappointing."
]
sentiments = [1, 0, 1, 1, 0] # 1 for positive, 0 for negative
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(reviews, sentiments, test_size=0.2, random_state=42)
# Vectorize the text data
vectorizer = CountVectorizer()
X_train_vectorized = vectorizer.fit_transform(X_train)
X_test_vectorized = vectorizer.transform(X_test)
# Train the model
model = LogisticRegression()
model.fit(X_train_vectorized, y_train)
# Make predictions
y_pred = model.predict(X_test_vectorized)
# Print classification report
print(classification_report(y_test, y_pred, target_names=['Negative', 'Positive']))
# Create and plot confusion matrix
cm = confusion_matrix(y_test, y_pred)
import matplotlib.pyplot as plt
plt.figure(figsize=(8, 6))
plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
plt.title('Confusion Matrix: Sentiment Analysis')
plt.colorbar()
plt.xlabel('Predicted label')
plt.ylabel('True label')
plt.xticks([0, 1], ['Negative', 'Positive'])
plt.yticks([0, 1], ['Negative', 'Positive'])
plt.tight_layout()
plt.show()
Additional Resources
For those interested in diving deeper into confusion matrices, performance metrics, and error analysis, here are some valuable resources:
- "Introduction to Machine Learning with Python" by Andreas C. Müller and Sarah Guido - A comprehensive guide that covers these topics in depth.
- "Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow" by Aurélien Géron - Offers practical examples and in-depth explanations of these concepts.
- ArXiv paper: "A Survey of Deep Learning Techniques for Neural Machine Translation" by Shuohang Wang and Jing Jiang (arXiv:1905.13302) - Provides insights into error analysis in the context of neural machine translation.
- Scikit-learn documentation (https://scikit-learn.org/stable/modules/model_evaluation.html) - Offers detailed explanations and examples of various performance metrics and techniques.
- Towards Data Science blog (https://towardsdatascience.com/) - Regularly publishes articles on machine learning topics, including confusion matrices and performance metrics.
These resources provide a mix of theoretical understanding and practical applications to help deepen your knowledge of these crucial machine learning concepts.