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.

Easy Python 3.9+ Aug 9, 2026 ML engineering pipelines 13 views 0 copies

Requires third-party packages — install first
pip install scikit-learn

Python code

13 lines
Python 3.9+
from 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

stdout
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

  1. Use `classification_report` from sklearn.metrics to get precision, recall, and F1-score for all classes at once.
  2. 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

Run locally

This sample needs third-party packages, so it cannot run in the browser IDE. Copy the code above, install the packages shown at the top, then run it in your own Python environment.

More from ML engineering pipelines

Related tutorials and quizzes for this topic.