Course Content
Model Evaluation
Evaluating and validating machine learning models
Introduction
After building your machine learning model, it is crucial to evaluate its performance effectively to ensure it generalizes well to new data.
This tutorial will teach you:
β
Why evaluation is important
β
Key metrics: accuracy, precision, recall, F1-score
β
Confusion matrices
β
ROC curves and AUC
Why is Model Evaluation Important?
- To understand how well your model performs on unseen data.
- To detect overfitting or underfitting.
- To choose the best model for your use case.
Key Metrics for Classification
1οΈβ£ Accuracy
The ratio of correctly predicted observations to the total observations.
2οΈβ£ Confusion Matrix
Shows True Positives (TP), False Positives (FP), True Negatives (TN), and False Negatives (FN) for detailed analysis.
3οΈβ£ Precision, Recall, F1-Score
- Precision: TP / (TP + FP)
- Recall: TP / (TP + FN)
- F1-Score: Harmonic mean of precision and recall.
4οΈβ£ ROC-AUC
ROC curve plots True Positive Rate vs. False Positive Rate, and AUC measures the area under this curve to evaluate model performance across thresholds.
Example: Model Evaluation in Python
Import Libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report, roc_curve, auc
import matplotlib.pyplot as plt
Prepare Sample Data
data = {
'Feature': [5, 10, 15, 20, 25, 30, 35, 40],
'Label': [0, 0, 0, 1, 1, 1, 1, 1]
}
df = pd.DataFrame(data)
X = df[['Feature']]
y = df['Label']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
Train and Predict
model = LogisticRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
y_prob = model.predict_proba(X_test)[:, 1]
Calculate Metrics
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
print("Classification Report:\n", classification_report(y_test, y_pred))
Plot ROC Curve
fpr, tpr, thresholds = roc_curve(y_test, y_prob)
roc_auc = auc(fpr, tpr)
plt.figure()
plt.plot(fpr, tpr, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], 'k--')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic')
plt.legend(loc="lower right")
plt.show()
Conclusion
π You now understand how to:
β
Calculate accuracy, precision, recall, and F1-score
β
Interpret confusion matrices
β
Visualize and interpret ROC curves
These techniques help you evaluate your model comprehensively to ensure it performs well in real-world scenarios.
Whatβs Next?
β
Practice evaluating models on real-world datasets.
β
Learn about cross-validation for robust evaluation.
β
Explore advanced evaluation for regression models (MAE, RMSE, RΒ²).
Join our SuperML Community to share your results, ask questions, and continue your machine learning journey!