🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #180

Accuracy

Accuracy is the simplest classification metric — the fraction of predictions that were correct overall — and also the most commonly misused, since it can look excellent while a model completely fails at the one thing that actually matters.

Formula

\[ \text{Accuracy} = \frac{TP+TN}{TP+TN+FP+FN} \]

Worked Example

Using the confusion matrix from Confusion Matrix: \(TP=15, TN=70, FP=10, FN=5\).

\[ \text{Accuracy} = \frac{15+70}{15+70+10+5} = \frac{85}{100} = 0.85 \]
from sklearn.metrics import accuracy_score

y_true = [1]*20 + [0]*80
y_pred = [1]*15 + [0]*5 + [1]*10 + [0]*70

print(accuracy_score(y_true, y_pred))   # 0.85

Why Accuracy Can Be Dangerously Misleading

Imagine a fraud dataset where only 1% of transactions are actually fraudulent. A model that always predicts "not fraud," never catching a single real case, still achieves 99% accuracy — a number that looks excellent while the model is completely useless for its actual purpose. This is exactly why accuracy is a poor default choice on imbalanced data.

from sklearn.metrics import accuracy_score

# 99 legitimate, 1 fraudulent -- model predicts "not fraud" for everyone
y_true = [0]*99 + [1]
y_pred = [0]*100

print(accuracy_score(y_true, y_pred))   # 0.99 -- looks great, catches ZERO fraud

When Accuracy Is Actually a Reasonable Choice

  • Roughly balanced classes, where no single class dominates
  • Both error types (false positives and false negatives) are similarly costly
  • A simple, quick sanity check alongside — never instead of — more nuanced metrics

Practical Use Cases

  • Balanced classification problems as a first-pass, easy-to-communicate summary
  • Multi-class problems with roughly even class sizes

Common Mistakes

  • Using accuracy as the primary or only metric on an imbalanced dataset — the single most common evaluation mistake in classification.
  • Reporting accuracy without ever checking the underlying confusion matrix or class distribution first.

Interview Relevance

Q: "A model achieves 99% accuracy on a fraud detection task. Is that good?" Not necessarily — check the class balance first; if fraud is rare (say 1%), a model that never predicts fraud at all would already hit 99% accuracy while being completely useless. Precision, recall and F1 tell a much more honest story on imbalanced problems.

Practice Question

Given \(TP=8, TN=180, FP=2, FN=10\), compute accuracy by hand, then explain why this number alone doesn't tell you how well the model catches the positive class.

Want to go beyond the notes?

Join CodingNow 2.0's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Accuracy – FAQs

Quick answers about learning Accuracy in Machine Learning.

This free note from CodingNow 2.0 explains Accuracy in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine Learning topic on CodingNow 2.0, including Accuracy, is 100% free with no signup required.
With focused practice, most students grasp Accuracy in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now