Please enable JavaScript to use CodeHS

Machine Learning Tutorial

How do computers learn?

Computers do not have brains, but they can learn patterns from examples. Train a tiny shape classifier yourself and see what pattern it finds.

AI ~10 min Beginner friendly
The Big Idea

Learning from examples

When you learn to ride a bike, you do not memorize a rule book. You try, fall, adjust, and try again. Over time, your brain figures out the pattern of how to stay balanced.

Machine learning works in a similar way. Instead of a human writing out every single rule, the computer studies many examples and finds patterns on its own. Then it uses those patterns to make predictions about new things.

Regular code

A human writes exact rules. The computer follows them step by step.

Machine learning

A human gives examples. The computer finds the rules itself.


Interactive

Train a shape classifier

Below are some little creatures. Each one is happy or sad. Your job is to show the computer one example at a time and see if it can figure out the secret rule.

Click Show next example to add training data. Watch the progress bar grow as the computer sees more examples.

Training examples 0 of 6 shown
happy
happy
happy
sad
sad
sad

The computer noticed a pattern

Every happy creature has a smile. Every sad creature has a frown. Color and shape did not matter.

What rule did the computer learn?


Vocabulary

Features and labels

In machine learning, each example has two parts:

  • Features: the parts we can see or measure. For the creatures, the features were color, shape, and mouth.
  • Label: the answer we want the computer to predict. For the creatures, the label was happy or sad.

During training, the computer looks at the features and tries to figure out which ones help predict the label. In the creature game, the mouth was the useful feature. Color and shape did not help at all.

Think about it: a feature is just a clue. Some clues are helpful, and some are distractions. A good machine learning model learns to ignore the distractions.


Data Matters

Why more data usually helps

Imagine the computer had only seen two creatures: a yellow smiling circle and a yellow frowning square. It might think color matters, because one was yellow and happy and one was yellow and sad. That would be a mistake.

The more examples the computer sees, the better it can tell which clues are real and which are just coincidences. This is why AI companies train their models on millions or even billions of examples.

Watch out: if the examples are bad, the model will learn bad patterns. If every picture labeled “dog" in the training data is a golden retriever on grass, the model might learn that grass means dog.


Code

A tiny classifier in Python

This code takes the same six creatures and tries each feature to see if one perfectly separates happy from sad. It finds the rule that matches the interactive demo.


Your Turn

Check yourself

Think of something a computer might learn to recognize, like spam emails, handwriting, or songs. List two features it could measure, and one label it could predict.

Example: For spam emails, features could be “contains the word free” and “has suspicious link.” The label is “spam” or “not spam.”

Interactive

Test on a bigger grid

The computer has learned the rule: smile = happy, frown = sad. Now try it on a larger set of creatures with new shapes and colors. Click a creature to see the prediction, or press Predict all to reveal every answer at once.


Check Yourself

Quick quiz

1. In machine learning, what is a label?

2. Why does more training data usually help?

3. Which of these is the best example of machine learning?

Keep learning about AI

You have seen how computers learn from examples. Next, learn about a special kind of AI that learns from language: the large language model.

Part of the “Are LLMs smarter than a 5th grader?” tutorial series.