Skip to content
AI Engineering
How We Got Here

Machine learning basics

Machine learning means fitting a function to examples instead of writing the rules yourself. A few ideas cover most of it.

Chapter 1: How We Got HereLesson 2 of 7Level: StarterAbout 15 min
Words on this page (14)
reinforcement learning
: Learning by trial and error: take actions, get rewards, and favor what earned reward.
token
: A piece of text the model reads and writes, usually a word or part of a word. About 4 characters of English on average.
loss
: A number measuring how wrong the model's output is. Training tries to reduce it.
parameter
: One learned number inside a model. Modern models have billions of them. Training adjusts them.
training
: Adjusting a model's parameters on data so its predictions improve.
validation set
: Data held out from training and used to tune choices and check that the model generalizes.
overfitting
: When a model memorizes training data and does worse on new data.
recommender
: A system that suggests items such as videos or products to each user.
anomaly detection
: Finding cases that look unlike normal ones, often without labeled examples of the bad kind.
forecasting
: Predicting a future number, such as sales, from past values.
LLM
: Large language model: a neural network trained on huge amounts of text to predict the next token.
weights
: The learned numbers that define a trained model. A model whose weights are published is called open-weight.
gradient
: The direction and rate at which the loss changes when each parameter changes. Training steps against it.
leakage
: When a feature or test example contains information that would not be known at prediction time, so scores look better than real use.

Three ways to learn

Supervised learning has inputs with the right answers attached: emails labeled spam or not spam, houses with sale prices. The model learns the mapping. Unsupervised learning has no answers: the model finds structure, such as grouping similar customers. learns from rewards after actions, as in game playing. Language models use a clever variant of supervised learning: the "answer" for each position is simply the next in the text, so no human labeling is needed.

Two kinds of prediction

Classification picks a category (spam or not). Regression predicts a number (a price). Both work the same way: compute a prediction, compare with the truth using a , adjust to reduce it.

Features, training and testing

A feature is an input measurement, such as a house's area or age. Data is split into a set that the model learns from, a used to tune choices, and a test set touched once at the end. If a model scores far better on training data than on validation data, it is : memorizing instead of learning. More data, simpler models and regularization help.

Classic machine learning still matters

Gradient-boosted trees often beat neural networks on tables of numbers. systems, fraud and , and ranking are mostly classic machine learning. A common mistake is reaching for an when a small model on your own data is cheaper, faster and easier to evaluate.

A worked example: predicting a house price

Say you have five houses. Area in square meters and price in thousands: (50, 150), (70, 210), (90, 270), (110, 330), (130, 390). A model learns a line, price = 3 × area. For a new 100 square meter house it predicts 300. That is all supervised learning is: find a function that fits the examples, then use it on new inputs. Real models have millions of inputs and curvy functions, but the loop is the same: guess, measure the error, adjust.

Why we split the data

Imagine studying for an exam by memorizing the exact practice questions. You would score 100 percent on the practice and fail the exam. A model can do the same. So we hide some data (the test set) and only look at it at the end. If the score on hidden data is far lower than on practice data, the model memorized instead of learned.

How training works, step by step

Training is a loop of guess, measure and adjust. Take the house example with one , so price = weight × area. Start with weight 1 and look at the 50 square meter house, which truly sells for 150.

  1. Guess: weight 1 predicts 50. The error is 100, and squared error (the error times itself, so big misses count more) is 10,000.
  2. Adjust: the price is too low, so raise the weight to 2. The prediction is 100, the error 50, the squared error 2,500.
  3. Adjust again: weight 3 predicts 150. The error is 0.

Real training does not try weights blindly. It computes the , which says which direction lowers the error, and takes a small step that way. Millions of weights get nudged at once, but each one follows this same loop.

What goes wrong: leakage and lopsided data

means information about the answer sneaks into the inputs or the training set. The symptom is a score that looks too good, then a collapse after launch. Say you predict loan defaults and one column records "sent to collections". That column is only filled in after a default, so it will not exist when you need the prediction.

To check, ask of every feature whether it is known at prediction time. Also look for duplicate rows that appear in both training and test data.

A lopsided dataset causes a second problem. If 99 of 100 transactions are honest, a model that always says "honest" scores 99 percent and catches no fraud. Count how often each answer appears before trusting accuracy.

A second example: nothing to learn

Suppose you try to predict tomorrow's coin flip from the date. A flexible model can memorize past flips and score 100 percent on training data. On held-out data it scores about 50 percent, because the date says nothing about the flip.

Learning also is not needed when you already know the formula. Converting Celsius to Fahrenheit is a fixed rule, so write it down. Fit a model only when the pattern is real but unwritten.

Try it yourself

Put 10 invented (area, price) pairs in a spreadsheet, with some noise so they do not sit on a perfect line. Hide 2 rows. Add a trend line using the other 8 and compare its error on the 8 rows against the 2 hidden ones. Hidden rows often have larger error, though with only 2 rows the result is noisy. That gap is what the test set measures.

Common mistakes

Testing on the same data you trained on, which gives a flattering and meaningless score. Using an LLM for a job where a small model on your own table of numbers would be cheaper, faster and easier to check.

Check yourself

A spam filter is trained on emails that people labelled as spam or not. What kind of learning is this?

Training accuracy is 99 percent and validation accuracy is 70 percent. What is happening?

You want to predict a house's sale price from its area and age. Which task is this?

Go deeper

Spotted a mistake or something unclear? Report it on GitHub.

On this page