Skip to content

Overfitting in Machine Learning: Definition, Signs, Fixes

Quick answer

What overfitting in machine learning is, how to spot it from train and test scores, and the standard ways to reduce it.

Overfitting in Machine Learning: Definition, Signs, Fixes

Overfitting in machine learning happens when a model fits its training data so closely that it fails to make correct predictions on new data. It has effectively memorised noise and quirks of the training set instead of learning the pattern, so it scores well in training and poorly in the real world.

Key takeaways

  • An overfit model performs well on training data and badly on unseen data.
  • The classic signal is training loss that keeps falling while validation loss rises.
  • Generalization is the opposite of overfitting.
  • Remedies include more clean data, early stopping, regularization and ensembles.

On this page:

ModelTrain accuracyTest accuracyVerdict
A (illustrative)98%71%Overfits: large gap
B (illustrative)84%82%Generalizes better: small gap
Underfit (illustrative)60%59%Too simple: poor on both

What does overfitting mean?

The Google Machine Learning Crash Course describes overfitting as a model that fits the training data so closely that it fails to make correct predictions on new data. It calls generalization the opposite of overfitting: a model that generalizes well makes good predictions on new data.

IBM (15 October 2021) says overfitting occurs when a model fits too closely or even exactly to its training data.

How do you spot overfitting?

Compare error on training data with error on data the model has not seen. IBM says that if the training data has a low error rate and the test data has a high error rate, it signals overfitting.

On loss curves, Google says loss declines or holds steady for the training set but increases for the validation set, which suggests overfitting. Picture training loss falling every epoch while validation loss bottoms out around epoch 8 and then climbs. Epoch 8 would be the early-stopping point (illustrative).

The code below fits the same synthetic noisy data (fixed seed) with a degree 1 and a degree 15 polynomial, then prints train and test R-squared. Exact values vary slightly by library version, but degree 15 should score high in training and far lower on the test set.

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.linear_model import LinearRegression

rng = np.random.default_rng(42)
x = rng.uniform(-3, 3, size=(40, 1))
y = np.sin(x).ravel() + rng.normal(scale=0.3, size=40)
x_train, x_test, y_train, y_test = train_test_split(
    x, y, test_size=0.5, random_state=0)

for degree in (1, 15):
    model = make_pipeline(PolynomialFeatures(degree), StandardScaler(),
                          LinearRegression())
    model.fit(x_train, y_train)
    print(degree, round(model.score(x_train, y_train), 2),
          round(model.score(x_test, y_test), 2))

Where does overfitting show up in India?

It shows up wherever a model is trained on narrow data. An illustrative scenario: a model predicts exam pass or fail for 300 students using data from one college. It looks accurate in testing, then fails on students from another state because it learned that college's patterns.

The IBM page lists ways to reduce it: early stopping, more clean training data, data augmentation, feature selection, regularization (lasso, ridge, dropout) and ensemble methods such as bagging and boosting.

What are the common mistakes?

  • Judging only on training score. Always hold out data the model never trains on.
  • Using a more complex model than the data supports. Complexity invites memorising.
  • Confusing it with underfitting. An underfit model is poor on both training and test data.

Generalization, regularization and validation sets are the closest terms. See machine learning interview questions for freshers, types of machine learning and LLM evaluation explained.

Next step

Run the code, then try degrees 3 and 5. Practise on a project from 15 machine learning project ideas for beginners, and always keep a test split aside.

Frequently asked questions

What is overfitting in machine learning?

Overfitting is when a model fits its training data so closely that it fails to predict correctly on new data. It has learned noise instead of the underlying pattern.

How do I know if my model is overfitting?

Compare training and test error. A low training error with a high test error signals overfitting. On loss curves, training loss falls while validation loss rises.

What is the difference between overfitting and underfitting?

An overfit model does well on training data and poorly on new data. An underfit model is too simple and does poorly on both.

How do I prevent overfitting?

IBM lists early stopping, more clean training data, data augmentation, feature selection, regularization such as lasso, ridge and dropout, and ensemble methods.

What is regularization?

Regularization adds a penalty or constraint that discourages a model from becoming too complex. Lasso, ridge and dropout are common forms.

Share LinkedIn X WhatsApp
×

Our Courses

Practice-Based Learning Tracks, Supercharged By A.I.