Please enable JavaScript to use CodeHS

AI & Machine Learning

What Is Overfitting?

When AI memorizes instead of learns

Picture a student who memorizes every answer on the practice test, then bombs the real test. AI models can make the same mistake. In this lab you will make it happen on purpose, so you can learn to spot it.

Machine Learning ~12 min 3 interactive labs
a model that memorized the dots the real pattern
01 / THE BIG IDEA

Memorizing is not learning

Two students get the same practice test. The real test has new questions on the same topic. Who does better?

The memorizer

Sam copies the answers

Sam learns that question 4 is "B" and question 7 is "C". Sam never asks why.

Practice test: perfectReal test: lost
The learner

Priya learns the idea

Priya works out why each answer is right. She misses a practice question or two.

Practice test: goodReal test: good

AI models face the same problem. A model learns from a pile of examples. If it learns the examples too closely, it gets great scores on those examples and fails on anything new. That mistake is called overfitting.

The goal of machine learning is not to remember the past. It is to do well on things the model has never seen. Scientists call that skill generalizing.

Why this matters. A model that is overfit can look amazing in a demo and then fail in the real world. Knowing how to catch it is a core skill for anyone who builds or uses AI.

02 / KEY WORDS

Seven words to know first

Here is the whole vocabulary for this page. Each word is used in the labs below.

Model
A rule or shape that turns an input into a guess. In the lab, the model is a curve.
Training data
The examples a model studies while it learns. Think of the practice test.
Test data
Fresh examples kept hidden until the end, used to check what the model really learned. Think of the real test.
Error
How far off the model's guesses are. Lower is better. We use "average miss": the typical distance between a guess and the true value.
Noise
Random bumps in real data that do not follow any pattern, like a measuring mistake or a lucky day. A good model should not copy noise.
Underfitting
The model is too simple to catch the real pattern. It does badly on training and test data.
Overfitting
The model is so flexible that it copies the noise too. It does great on training data and badly on test data.
03 / LAB 1

The curve-fitting lab

Each dot is one example. Its position left to right is the input. Its height is the right answer. The dots are noisy, like real data.

The purple line is the model. You choose how bendy it can be with the degree slider. Degree 1 is a straight line. A higher degree allows more bends. The computer finds the line that sits closest to the blue training dots using a method called least squares.

Your job: slide the degree up and watch the two error scores. Then try more training points.

Fig. 1 · Curve lab

Degree 1 = straight line. Higher = more wiggles.

How many examples the model studies.

How messy the data is.

Training errorAverage miss on the dots it studied0.00
Test errorAverage miss on new dots0.00

Training error Test error Noise floor
See the numbers for every degree
Training and test error for each degree
DegreeTraining errorTest error

What you should see

  • Training error only goes down. More bends always let the line hug the blue dots more tightly. At the top degrees it can pass through almost every dot.
  • Test error goes down, then up. That "U" shape is the signature of overfitting. The bottom of the U is the sweet spot.
  • The dashed noise floor is the best any model can do on noisy data. Beating it on training data means the model is copying noise.
  • More training points help. Try degree 10 with 40 points. The curve calms down because there is less room to chase noise.
04 / THREE OUTCOMES

Too simple, just right, too wiggly

Here are three models trained on the same dots. Same data, very different results.

Underfitting

Too simple. A straight line cannot follow the curve.

Train Test

Just right

Follows the pattern and ignores the noise.

Train Test

Overfitting

Too flexible. It chases every bump of noise.

Train Test

The tell: look at the gap. Underfitting is bad everywhere. Overfitting is great on training and bad on test. Just right is good on both, with a small gap.

05 / LAB 2

Memorizer vs learner: the quiz bots

Let us build two bots and test them. Their quiz is simple: "Is a + b even or odd?" The numbers a and b each go from 1 to 10, so there are 100 possible questions.

You decide how many questions go in the practice set (training data). The rest stay hidden as the test set. Both bots study the practice set first.

  • The memorizer writes every practice question and its answer in a notebook. If a question is not in the notebook, it guesses.
  • The learner tries a short list of possible rules, scores each rule on the practice set, and keeps the best one.
Fig. 2 · Quiz bots

20 practice, 80 test

Show grid for

Columns are a. Rows are b. Each square is one question.

The memorizer

Practice
Surprise test
?

The learner

Picks the rule that scored best on practice.

Practice
Surprise test
?

Rules it tried (says "even" when true), scored on practice:

    Press Give the surprise test to see how each bot does on questions it never studied.

    What just happened

    • The memorizer scores 100% on practice every time, because the answers are in its notebook. On the test it is only guessing, like flipping a coin.
    • The learner found a rule that works on new questions too. A rule can be reused. A memory cannot.
    • Try only 2 or 3 practice questions. Several rules score equally, so the learner may pick the wrong one. Too little data can fool a learner too.
    06 / LAB 3

    Spot the cheat: data leakage detective

    Sometimes a model looks great because the test was not fair. The test set was supposed to be a surprise, but a piece of it leaked into training. That is called data leakage.

    Read each case. Is the test fair, or did the data leak?

    Fig. 3 · Case files
    Case 1 of 7Score 0

    The rule of thumb: anything that helps the model make decisions must come from the training side only. The test set is for one final check, and nothing else.

    07 / FIXES

    How builders fight overfitting

    Nobody can remove overfitting forever. Builders keep it small with a few reliable habits.

    Use more, varied data

    More examples leave less room to chase noise. Variety matters too, so the model sees many kinds of cases.

    Pick a simpler model

    Fewer knobs means fewer ways to memorize. A trick called regularization adds a penalty for overly complicated answers.

    Stop training early

    Check a held-back set while training. When its score stops improving, stop. That is called early stopping.

    Lock away a test set

    Split data into train, validation, and test. Use validation data to tune settings. Open the test set once, at the very end.

    08 / CHECK YOURSELF

    Quick quiz

    1. A model gets a near-perfect score on its training data but does poorly on new data. What is this called?

    2. Which change is most likely to help a model that is overfitting?

    3. Which one is an example of data leakage?

    09 / QUICK ANSWERS

    Quick answers

    What is overfitting in simple words?

    Overfitting is when a model learns its training examples too closely, including random noise, so it does well on data it has already seen but poorly on new data. It is like memorizing the answers to a practice test instead of understanding the topic.

    What is the difference between overfitting and underfitting?

    An underfit model is too simple to catch the real pattern, so it does badly on both training data and test data. An overfit model is too flexible and copies the noise, so it does very well on training data but badly on test data. A model that fits just right does well on both.

    How can you tell if a model is overfitting?

    Compare the error on the training data with the error on test data the model has never seen. If the training error is very low and the test error is much higher, the model is probably overfitting. A big gap between the two scores is the warning sign.

    How do you prevent overfitting?

    Common fixes are to collect more varied training data, use a simpler model, add a penalty for overly complicated answers (called regularization), and stop training when the score on held-back data stops improving. You should also keep a test set locked away until the very end.

    What is data leakage?

    Data leakage is when information from the test set, or the answer itself, sneaks into training. Examples include test questions that also appear in the training set, clues that contain the answer, and tuning settings by repeatedly checking the test score. Leakage makes a model look better than it really is.

    Can big AI models like chatbots overfit too?

    Yes. Any model that learns from examples can memorize them. Builders train on large and varied data, and they check the model on examples it did not train on, to see whether it learned general patterns or just copied what it saw.

    Keep learning

    Now that you can tell memorizing from learning, see how researchers score AI, how models learn from data, and two kinds of models you can study next.

    Part of the CodeHS AI & Machine Learning tutorials.