Last updated

Measuring Accuracy

Definition: Accuracy is the fraction of predictions a model gets right. It is the simplest way to score a classifier: correct predictions divided by total predictions.

Example — score predictions

from sklearn.metrics import accuracy_score

actual    = [1, 0, 1, 1, 0, 1, 0, 0]
predicted = [1, 0, 1, 0, 0, 1, 0, 1]

score = accuracy_score(actual, predicted)
print("Correct:", sum(a == p for a, p in zip(actual, predicted)), "out of", len(actual))
print("Accuracy:", round(score * 100, 1), "%")

Accuracy is not the whole story

If 99% of emails are not spam, a lazy model that says "never spam" is 99% accurate but useless. That is why data scientists also use:

  • Precision — of the items flagged, how many were right
  • Recall — of the real cases, how many were caught
  • Confusion matrix — a table of correct vs wrong by category

💡 Tip: always compare your model against a simple baseline before trusting a high accuracy number.

Try it Yourself
Output

          
Ad · responsive