How to Evaluate Accuracy, Precision, and Recall in Python
Compute accuracy, precision, and recall for a binary classification model using scikit-learn's metrics functions.
pip install scikit-learn
Python code
13 linesfrom sklearn.metrics import accuracy_score, precision_score, recall_score
if __name__ == "__main__":
y_true = [0, 1, 1, 0, 1, 0, 1, 1]
y_pred = [0, 1, 0, 0, 1, 0, 1, 1]
accuracy = accuracy_score(y_true, y_pred)
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)
print(f"Accuracy: {accuracy:.2f}")
print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")
Output
Accuracy: 0.88
Precision: 0.80
Recall: 1.00
How it works
This code uses scikit-learn's accuracy_score, precision_score, and recall_score functions to compare the true labels (y_true) with the predicted labels (y_pred). Accuracy measures the proportion of correct predictions out of all predictions. Precision measures the proportion of positive predictions that were actually correct (true positives divided by true positives plus false positives). Recall measures the proportion of actual positives that were correctly identified (true positives divided by true positives plus false negatives). Note that the default precision_score and recall_score treat class 1 as the positive class, which is appropriate for binary classification.
Common mistakes
- Forgetting to set `pos_label` if the positive class is not 1
- Using `accuracy_score` on imbalanced datasets without additional metrics
- Assuming precision and recall are equal without checking the confusion matrix
- Mixing the order of arguments: `y_true` first, then `y_pred`
Variations
- Use `classification_report` from sklearn.metrics to get precision, recall, and F1-score for all classes at once.
- Compute micro- or macro-averaged metrics with `average='micro'` or `average='macro'` for multi-class problems.
Real-world use cases
- Evaluating a spam detection model to balance catching true spam (recall) with avoiding false positives (precision).
- Comparing candidate classifiers during model selection by monitoring both precision and recall on a validation set.
- Running CI tests for ML pipelines that assert accuracy thresholds before promoting a model to production.
Sponsored
More from ML engineering pipelines
- Bayesian Optimization in Python: A Simplified Mock Implementation medium
- Build a Data Helper Class in Python for ML Pipelines easy
- Build a Mock Random Forest Classifier in Python easy
- Champion Challenger Deployment Mock in Python easy
- Compare Model A vs Model B Metrics in Python easy
- Create a Minimal Great Expectations Suite Mock in Python easy
Keep learning
Related tutorials and quizzes for this topic.