What Is Machine Learning? How Computers Learn From Data
Machine learning is a way to let a computer find rules in many examples, instead of having people write the rules for it. Inside, it is a calculation with adjustable numbers (parameters). Those numbers are corrected little by little so that the errors on the examples get smaller. Whether it learned well is checked on new data that was not used for learning.
What you'll learn
- How programs with hand-written rules differ from machine learning, which finds rules in data
- "Learning" means adjusting numbers: see it by fitting a line to three points
- The differences between supervised, unsupervised and reinforcement learning
- Overfitting, and why data is split into practice and test sets
- Why accuracy alone is not enough, and what precision and recall are
- Neural networks, where machine learning is used, and its limits and risks
Prerequisites: none. You can follow the worked examples with just multiplication and subtraction. This article is also for readers who want an intuitive picture of terms from IT certification syllabi.
In everyday life
Email apps move junk mail (spam) into a separate folder automatically. Video and shopping sites show lists of picks "for you." Search for "dog" in a phone's photo app, and it finds your photos of dogs.
Many of these use machine learning. It is also a topic in IT certification syllabi (for example Japan's IT Passport exam), which list terms such as supervised learning and overfitting.
Writing the rules by hand, or finding them in data
Say you want to build a program that spots spam. The usual way is for people to write the rules. For example: "If the subject contains 'winner,' it is spam." Or: "An email from an unknown sender with nothing but a link is spam."
But this approach soon hits a wall. Senders change their wording to slip past the rules. Just writing "w.inner" instead of "winner" is enough to beat the rule.
So you flip the idea around. You prepare many examples, each labeled with the right answer: "this is spam" or "this is normal email." Then you let the computer find, from the examples, which features make an email look like spam. This is machine learning. In 2002, programmer Paul Graham described a way to spot spam by learning how words appear in examples. His essay was widely read.
The "rules" made in ② are not sentences people can read. They are a set of numbers that say how much weight to give each of many features. They do not rely on a single word. So even if a word is changed a little, other features can still show that an email looks like spam. Add examples of new tricks and train again, and the rules are updated too.
The model: a calculation with adjustable numbers
In machine learning, the calculation that turns an input into an answer is called a "model." Inside a model are numbers whose values can be changed. These numbers are called "parameters." Depending on their values, the same input gives a different answer.
Take the simplest example. Suppose you want to predict how long it takes to carry packages, based on how many there are. You decide the model is "time = a × number of packages." Here a is the only parameter you can adjust. If a is 2, the model predicts 6 minutes for 3 packages.
The best value of a is decided by actual records, not by a person. Say there are three records: 3 minutes for 1 package, 5 minutes for 2, and 5 minutes for 3. Finding the a that fits these three points as well as possible is "learning" for this model.
Learning: adjusting numbers so the error gets smaller
First, express how far off the predictions are as a number. The difference between a prediction and reality is called the "error." Differences can be positive or negative, so they cancel out when added. So each difference is squared first (2 becomes 2 × 2 = 4), and then they are added up. That sum is the total error.
With a = 1, the predictions are 1, 2 and 3 minutes. The differences from the actual 3, 5 and 5 minutes are 2, 3 and 2. Squared and added, that gives 4 + 9 + 4 = 17. With a = 2, the predictions are 2, 4 and 6 minutes, and the differences are 1, 1 and −1. Squared and added, that gives 1 + 1 + 1 = 3, which is much smaller.
So how should a be corrected? A common method is gradient descent. A gradient is the "slope of a hill." You check how much the total error changes, and in which direction, when a changes slightly. Then you move a in the direction that lowers the error. The steeper the slope, the bigger the step.
Let's try it here, starting from a = 1. The slope is found with a branch of math called differentiation. We skip the calculation, but the slope at a = 1 is −28. The minus sign means "increasing a lowers the error." Multiplying 28 by a small number chosen by a person (here 0.02) gives 0.56. Increase a by that much, and a becomes 1.56, with a total error of about 5.7. Repeat the same step, and a moves to 1.81, then 1.91, getting closer to 2. The error falls to 3.5, then 3.1, getting closer to 3.
In this example, the best a is 2, and the total error is then 3. This model's line always passes through the point "0 packages, 0 minutes." The three points do not lie exactly on any such line, so the error never reaches 0.
Three ways to learn
Machine learning falls into three broad types, based on what it uses as clues while learning.
Learning from labeled examples: supervised learning
Learning from pairs of inputs and right answers, as in the spam and package examples, is called supervised learning. The name means there is a "supervisor," like a teacher, who supplies the right answers. Picking one of a fixed set of kinds, such as spam or not spam, is called "classification." Predicting the size of a number, such as the package time, is called "regression."
Finding groups without answers: unsupervised learning
Other methods find groups of similar items using only data with no answers attached. This is called unsupervised learning. For example, it can find groups of customers who shop in similar ways from purchase records. Grouping similar items is called "clustering," and k-means is a well-known method. People then decide what the groups it finds mean.
Learning from scores for actions: reinforcement learning
The third type learns by trying actions and using the score each result earns as a clue. This score is called a "reward," and this way of learning is called reinforcement learning. It is not told the right answer for each move. Instead, it uses results such as winning or losing to find, by trial and error, how to choose good actions. The Go program AlphaGo first learned from records of human games. It then grew stronger through reinforcement learning by playing against itself. In March 2016, it beat top professional Lee Sedol (9-dan) four games to one.
Splitting data for practice and testing
Making the error small during learning does not by itself make a "good model." What you really want to know is whether it gets new data right, data it never learned from (tomorrow's email, for example).
So the collected data is split into three parts in advance. The "training" set is used to correct the parameters. The "validation" set is used to compare model settings and choose among them. The "test" set measures performance just once, at the end. Settings include things like how complex the model is and when to stop training. If you keep tuning settings while looking at the test data, the test score comes out higher than the model's real ability.
One reason for splitting is overfitting. This is when a model fits the training data too closely and memorizes even its random variation. The error on the training data is very small, but predictions on new data are far off. It is more likely when a complex model with many parameters is trained on little data.
Measuring performance: accuracy is not enough
A classification model's performance is checked with a table that counts the cases for each pair of prediction and reality. This table is called a confusion matrix. Let's look at a spam filter tested on 1,000 test emails. Of those 1,000 emails, 100 are actually spam (the numbers are made up for illustration).
| Predicted spam | Predicted normal | |
|---|---|---|
| Actually spam (100) | 90 (correctly caught) | 10 (missed) |
| Actually normal (900) | 30 (wrongly marked spam) | 870 (correctly normal) |
The share of all cases that were right is called "accuracy." The filter got 90 + 870 = 960 emails right, so accuracy is 960 ÷ 1,000, or 96%. But this number does not tell you how the filter went wrong.
So we look at the mistakes in two ways. Of the 120 emails predicted as spam, the share that really was spam is 90 ÷ 120, or 75%. This is called precision. The other 25% are mistakes that send normal email to the spam folder.
The second measure is the share of the 100 actual spam emails that were caught. That is 90 ÷ 100, or 90%, and it is called recall. Making the rule for "spam" stricter tends to raise precision and lower recall. Which one matters more depends on the goal. For spam, precision matters most, so you don't lose important email. For a medical test, recall matters most, so cases of the disease aren't missed.
Neural networks and deep learning
The model in the package example was a straight line with one parameter. Handling complex things like images and text needs a more expressive model. The kind widely used today is the "neural network." The name comes from nerve cells in the brain, but inside it is just arithmetic on numbers.
The basic building block of a neural network multiplies each input by a weight (a parameter) and adds up the results. But stacking layers of plain multiply-and-add, however many, still works like a single multiply-and-add layer. So after each layer, a step that is not a straight line is inserted, such as "turn negative values into 0." The function that does this is called an activation function.
Stacking many layers of "multiply, add, then a non-straight step" is called deep learning. "Deep" refers to having many layers. With more layers, a model can pick up features in stages. For an image, that means lines and corners, then combinations of them, then parts of objects. It learns the same way as the package example: every weight is corrected little by little in the direction that lowers the error. A huge number of weights need slopes. The method that finds them all at once, working back from the output side, is called backpropagation.
This multiply-and-add work can be done in bulk as math on tables of numbers (matrices). The hardware that does it fast is explained in "Why Are GPUs Used for AI?" and "What Is an NPU?"
Where is it used?
- Recommendations: predicting what a person is likely to enjoy from the videos they watched and the products they bought.
- Image recognition: identifying objects and faces in photos. It is also used in factory inspection to find scratches on products.
- Translation: In September 2016, Google Translate switched its Chinese-to-English translation to a neural network approach. In November, it extended this to more languages, including Japanese.
- Fraud detection: spotting suspicious transactions that differ from the usual pattern in credit card records.
Limits and risks
First, bias in the data goes straight into the model. A 2018 study called "Gender Shades" examined three commercial systems that guess gender from face images. Error rates were at most 0.8% for lighter-skinned men, but up to 34.7% for darker-skinned women. A model may handle people poorly when they are rare in the data it learned from.
Second, it needs a lot of data. For example, the image recognition competition ILSVRC (2012–2017) used about 1.28 million images for training. People labeled each image with one of 1,000 categories. The effort and cost of collecting labeled data is a major hurdle for machine learning.
Third, it is hard to explain why a model gave a particular answer. The "rules" of a deep learning model are a set of millions of numbers or more. It is not easy for a person to read them and trace the reasons. The U.S. National Institute of Standards and Technology (NIST) lists explainability as a characteristic of trustworthy AI in its January 2023 framework. Extra care is needed for decisions about people, such as loans and hiring.
How it relates to large language models
Generative AI that writes text is built around a large language model, and that is a machine learning model too. Inside is a very large neural network, and some have hundreds of billions of parameters. It is built by repeating one step: predict what comes next in a text, and correct the parameters when the prediction is wrong.
The basic idea of "correcting numbers so the error gets smaller" is the same as in the package example. For details, see "How Do Large Language Models Work?"
Common myths
Myth: AI and machine learning are the same thing
In fact, AI (artificial intelligence) is a broad term for all technology that tries to make computers do the kind of thinking people do. Machine learning is one way to build it, and deep learning is one method within machine learning. For example, "expert systems," in which people write experts' knowledge in as rules, are AI but not machine learning. That approach spread in the 1980s. Most recent AI is built with machine learning, so the two are often treated as the same.
Myth: More data solves everything
In fact, more data makes overfitting less likely, but it does not solve everything. However much biased data you add, the bias remains. If many labels are wrong, the model learns those mistakes too. And adding data from situations unlike real use (say, only daytime photos) won't make it work on nighttime photos. Quality and content matter, not just quantity.
Myth: 99% accuracy means a great model
In fact, when the data is lopsided, accuracy can't be trusted. Suppose 100 of 10,000 card transactions (1%) are fraud. A model that blindly answers "all normal" gets 9,900 right, so its accuracy is 99%. But it finds no fraud at all, and its recall is 0%. If the goal is to find the rare cases, you need to check precision and recall.
By the numbers
| Item | Number | Notes |
|---|---|---|
| Accuracy in the spam example | 96% | Precision 75%, recall 90%. A made-up example in this article |
| Accuracy of fraud detection that answers "all normal" | 99% | When 1% is fraud. Recall is 0% |
| ILSVRC classification training images | About 1.28 million (1,000 categories) | Competitions in 2012–2017 |
| Error rates of gender classification from face images | Up to 34.7% and up to 0.8% | Darker-skinned women and lighter-skinned men. 2018 study |
The top two rows are made-up examples for illustration. The bottom two are figures you can check in research and records.
Learn more
- Compare in a table: Supervised vs Unsupervised vs Reinforcement Learning, AI Training vs Inference
- Try the numbers: Confusion matrix calculator (it also computes other metrics, such as the F1 score)
- Terms about evaluation and overfitting: Cross-validation (measuring performance while rotating which data is held out), Regularization (a technique to curb overfitting)
- Terms about models and their use: CNN (often used for images), Transfer learning, Inference (computing answers with a trained model)