7 min read
How machines learn
Learning from examples instead of rules
So far you have told the computer exactly what to do, step by step. Move the turtle forward, check a condition, repeat a loop. That works when you can write the rule down. But some problems are almost impossible to spell out in rules.
Think about telling apart a photo of a cat from a photo of a dog. You know the answer in a fraction of a second, yet you would struggle to write the exact rules that separate them. This is where AI and machine learning come in. Instead of writing the rule yourself, you show the machine many examples and let it work out the pattern.
Machine learning is programming a computer to get better at a task by using example data or past experience. It is useful exactly when a plain rule is hard to write: recognising speech, reading handwriting, spotting fraud, or steering a robot across ground no human has walked before.
And you do not have to build all of this from nothing. As one of our workshop guests put it: you do not need to write a book when you already have it in a library. In Python you just import the right library and use it. We will show a tiny taste of that at the end.
Three kinds of learning
Almost every machine learning method falls into one of three families.
Supervised learning learns from labelled examples. You give it data where the answer is already attached, many photos each marked "cat" or "dog", and it learns the pattern that connects the input to the label. Afterwards it can put a label on a new photo it has never seen. Most everyday predictions, from spam filters to price estimates, work this way.
Unsupervised learning gets data with no labels at all. Its job is to find structure on its own, for example grouping customers who behave alike into clusters. Nobody tells it the "right" groups; it discovers them from the data.
Reinforcement learning learns by trying, then getting a reward or a penalty. Imagine teaching a self-driving car through a series of moves like "left, straight, right, straight". A safe, smooth run earns a good score, say +18.5. A run that swerves and brakes hard earns a bad one, say -3. Over many attempts the machine learns which actions lead to the higher reward, the same way you learn a game by playing it.
A worked example: predicting from features
The pieces of information you feed a model are called its features. Choosing good features is often what decides whether a model works.
Here is an example from one of our data science workshops. Suppose you want to predict whether a person is a man or a woman, and the only feature you have is their height.
You read the heights from a file, plot them, and look for a boundary. Say you decide that anyone taller than 170 cm is predicted to be a man, and shorter is predicted to be a woman. This rule is right a lot of the time. But when you count the mistakes, you see plenty: short men and tall women get labelled wrong. The accuracy, the share of predictions the model gets right, is only so-so.
So you add a second feature: weight. Now each person is a pair, height and weight together, and the two features give the model much more to separate people by. The same kind of model, fed both features, predicts far more accurately than height alone.
That is the everyday rhythm of machine learning: pick features, measure accuracy, and add or change features until the predictions are good enough for the job.
Learn more
What a GAN is. Some of the most striking AI you have seen, faces of people who do not exist, a photo turned into a painting, comes from a design called a GAN (a generative adversarial network). "Generative" means it creates new data. "Adversarial" means two networks are set against each other.
The clearest way to picture it is a police sketch.
- One network is the Generator, like a sketch artist. It has never seen the suspect. It keeps producing drawings and tries to make them look real.
- The other network is the Discriminator, like an eyewitness. It has seen real faces, and its only job is to say "real" or "fake" about each drawing.
At first the sketch artist is hopeless and the eyewitness catches every fake. But every rejection is feedback. The Generator adjusts, tries again, and slowly its fakes get more convincing, until even the Discriminator can barely tell them apart. That back and forth is how a GAN learns to produce data that looks genuine.
When AI guesses your password. If a GAN can learn what real faces look like, it can also learn what real passwords look like. People do not choose passwords at random. They lean on patterns: a name and a year, a favourite team, a word with a number stuck on the end. A tool called PassGAN was trained on huge lists of leaked passwords and learned those patterns, then generated fresh guesses in the same style. It produced strings like love42743, s13trumpy, and bananabake, plausible passwords that a person really might pick, aimed straight at cracking accounts faster.
The lesson is not to fear the technology but to respect it. A password built from an obvious pattern is exactly what a machine expects, and the same idea that makes machine learning powerful, learning patterns from many examples, is what makes predictable passwords risky. A long, unusual passphrase that follows no common template is far harder for a trained model to guess, so choose one a pattern-hunting machine would not expect.
Finally, a short taste of the "just import it and use it" point. This tiny program uses a ready-made library to learn the link between years of experience and salary, then predicts a new value. You do not need to understand every line; notice how few lines it takes.
from sklearn.linear_model import LinearRegression
# years of experience and the matching salaries
X = [[1], [2], [3], [4], [5]]
y = [110000, 120000, 130000, 140000, 150000]
model = LinearRegression()
model.fit(X, y)
# predict the salary for 6 years of experience
print(model.predict([[6]]))
Lab
Pick one app you used today and name which kind of learning it most likely uses, and what example data it might have learned from.
Check yourself
- A spam filter is trained on thousands of emails already marked "spam" or "not spam". Which of the three kinds of learning is that, and why?
- In the height example, why did adding weight as a second feature make the predictions more accurate?
- In the police-sketch picture of a GAN, which network is the sketch artist and which is the eyewitness, and what does each one try to do?
Where this lesson comes from
Built from
- Workshop 1: Machine Learning (guest lecturer): why ML and the types of learning
- Workshop 2: Data Science (guest lecturer): features and accuracy (height then height and weight)
- Workshop 3: Introduction to GANs (guest lecturer): GANs and PassGAN
alphaPlan courses are built from taught programmes rather than invented for the web. Where a claim rests on an outside standard or a reported case, it is named above so you can check it rather than take our word for it.
This course was developed by alphaPlan Center from programs delivered in partnership with the American Corners network.
Found something unclear, outdated or improvable? Suggest an improvement