Coding Hubs School of AI – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 8448811540
Back to Machine Learning Notes
Topic #1801

Overfitting

Overfitting happens when a model learns the training data's noise and quirks in addition to its genuine patterns — producing excellent training performance that fails to generalize to new data.

The Clearest Signal — The Train/Validation Gap

training time / model complexity → training error validation error overfitting begins here →

Training error keeps falling while validation error starts rising — the growing gap between the two curves is the textbook signature of overfitting.

Diagnosing It in Code

from sklearn.metrics import accuracy_score

train_acc = accuracy_score(y_train, model.predict(X_train))
val_acc = accuracy_score(y_val, model.predict(X_val))

print(f"Train: {train_acc:.3f}, Validation: {val_acc:.3f}, Gap: {train_acc - val_acc:.3f}")
# A large, positive gap (e.g. 0.99 vs 0.72) is the clearest overfitting signal

Common Causes

CauseWhy It Overfits
Model too complex for the amount of dataEnough capacity to memorize noise instead of learning the general pattern
Too many features relative to samplesMore opportunity to fit noise in any single feature
Training for too long (iterative models)Later iterations increasingly fit training-set-specific noise
Noisy or mislabeled training dataA flexible model can memorize the noise as if it were signal

The Fixes

Practical Use Cases

Every model-building project needs to actively check for overfitting — it's not an edge case, it's the default failure mode of any sufficiently flexible model trained without appropriate safeguards.

Common Mistakes

  • Only checking training accuracy and never comparing it against validation performance.
  • Assuming a complex model is automatically better — extra flexibility only helps if it's matched with enough data and appropriate regularization.

Interview Relevance

Q: "How do you detect overfitting during model development?" Compare training performance against validation/cross-validation performance — a small gap is healthy and expected; a large, growing gap (especially alongside near-perfect training scores) is the clearest evidence the model has started memorizing training-set-specific noise instead of general patterns.

Practice Question

A model achieves 98% training accuracy and 71% test accuracy. Name three concrete changes you'd try, in order of what you'd attempt first.

Want hands-on practice diagnosing and fixing overfitting? CodingNow's Data Science course covers this through live, project-based debugging.

Related ML Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →