Evaluate & ImproveCore· 35 min read

Overfitting vs Underfitting

Overfitting memorises the training data; underfitting is too simple to learn it at all.

What you will learn

  • Define overfitting and underfitting
  • Spot them by comparing train and test scores
  • Know simple ways to fix each

The two ways learning goes wrong

A good model learns the real pattern and works on new data. Two failures are common:

  • Underfitting — the model is too simple. It misses the pattern and does badly on everything, even the training data.
  • Overfitting — the model is too complex. It memorises the training data (including noise) and does badly on new data.

A study analogy

Underfitting is barely studying — you fail the practice and the exam. Overfitting is memorising the practice answers word for word — you ace the practice but flop on the real exam with new questions.

How to spot them: compare two scores

The trick is to look at the training score and the test score together.

Train scoreTest scoreDiagnosis
LowLowUnderfitting (too simple)
HighLowOverfitting (memorised)
HighHighJust right — it generalises

Let us see the “High, Low” row happen for real. The code below loads a built-in flower dataset (load_iris), splits it, then trains a decision tree with no depth limit so it is free to memorise — and prints its score on the training data and on the hidden test data so we can compare the two.

A deep tree: near-perfect on training, weaker on test
# An overfitting decision tree, shown by its two scores
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris

X, y = load_iris(return_X_y=True)
Xtr, Xte, ytr, yte = train_test_split(X, y, test_size=0.3, random_state=0)

deep = DecisionTreeClassifier()          # no depth limit -> can memorise
deep.fit(Xtr, ytr)
print('Train score:', round(deep.score(Xtr, ytr), 2))
print('Test score: ', round(deep.score(Xte, yte), 2))

Note: Output: Train score: 1.0 Test score: 0.93 The gap is the warning sign: 100% on data it has seen, but lower on new data. A big train-test gap means the model is overfitting — it memorised rather than learned.

Simple fixes

  • Overfitting: get more data, use a simpler model (e.g. limit tree depth), or remove noisy features.
  • Underfitting: use a more powerful model, add better features, or train longer.

Tip: A small gap between train and test scores is normal and healthy. A large gap (great on train, poor on test) is the classic fingerprint of overfitting.

Q. A model scores 100% on training data but only 60% on test data. What is happening?

Answer: A high training score with a low test score means the model memorised the training data and fails to generalise — that is overfitting.

✍️ Practice

  1. Re-run the code with DecisionTreeClassifier(max_depth=2) and compare the train/test gap.
  2. Give a real example of underfitting and a real example of overfitting in everyday studying.

🏠 Homework

  1. Explain, using the “practice exam” analogy, how you would detect and reduce overfitting in your own words.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →