What machine learning is: rules learned from examples
On this page
This is the first post in a series that explains machine learning in plain words, with examples from payments and finance, and small code you can run.
Start with the basic question.
The old way: write the rules yourself
Imagine you work on a card fraud team. Your job is to stop fraudulent payments.
The traditional way is to write rules. You look at past fraud, notice that stolen cards are often used for big purchases, and write:
If the amount is over $1,000, flag it as fraud.
It works, for a while. Then fraudsters change their behaviour, you write more rules, and a few years later you have thousands of them. Nobody remembers why half of them exist.
The machine learning way: show examples, let it find the rules
Machine learning flips this around. Instead of writing the rules, you give the computer a large set of past transactions where you already know the answer, fraud or not fraud. The computer looks for the patterns that separate the two, and writes the rules for you.
In the old way, a person writes the rules. In machine learning, the data plus the known answers produce the rules.
A small example
I made up 4,120 card transactions. Each one has two facts: the amount and the hour of day. Most are normal daytime purchases. Some are fraud, and the fraud comes in two shapes:
- Big purchases, at any hour.
- Tiny charges at night, a few dollars each. Fraudsters often do this to test whether a stolen card works before they use it for something bigger.
I tested the hand-written rule (“over $1,000 is fraud”) and a simple machine learning model on the same set of transactions:
| Approach | Frauds caught | False alarms |
|---|---|---|
| Hand-written rule | 14 of 36 | 0 |
| Rules learned from data | 36 of 36 | 2 |
The hand-written rule caught every big fraud and missed every small night-time one. Nobody told the model about card testing. It found that pattern in the data on its own.
The rules it learned are readable, which I did not expect:
If the amount is over $1,257, it’s fraud. Otherwise, if it’s before 5:24 am and the amount is under $29, it’s fraud. Everything else is normal.
That’s very close to what an experienced fraud analyst would write after studying the data for a week. The computer did it in under a second.
The code is short. This uses scikit-learn, the most common Python library for machine learning:
from sklearn.tree import DecisionTreeClassifier, export_text
# X: one row per transaction -> [amount, hour]
# y: the known answer -> 1 = fraud, 0 = normal
model = DecisionTreeClassifier(max_depth=3)
model.fit(X_train, y_train) # learn the rules from past examples
predictions = model.predict(X_test) # use them on transactions it has never seen
print(export_text(model, feature_names=["amount", "hour"])) # show the rules
My data is made up and clean, so the model looks perfect. Real fraud is much harder. Fraud is a tiny fraction of all payments, the patterns overlap with normal behaviour, and fraudsters keep changing what they do.
The words you’ll keep hearing
| Word | Plain meaning | In the fraud example |
|---|---|---|
| Feature | A fact you know about each example | Amount, hour of day |
| Label | The answer you want to predict | Fraud or not fraud |
| Training | Letting the computer learn from past examples | model.fit(...) |
| Model | The rules it learned | The three rules above |
| Prediction | Using the model on something new | Flagging tonight’s transactions |
A one-line definition
Tom Mitchell’s classic textbook defines it like this, in my own words:
A program learns when it gets better at a task, measured in some way, as it sees more experience.
For our fraud team: the task is flagging fraud, the measure is how many frauds we catch, and the experience is the history of past transactions with known answers. More history, better rules.
Three kinds of machine learning
| Kind | What you give it | Finance example |
|---|---|---|
| Supervised | Examples with answers | Fraud or not; will this loan be repaid? |
| Unsupervised | Examples without answers | Grouping customers by how they spend |
| Reinforcement | A goal and a reward | A trading program that learns from profit and loss |
This series is mostly about the first kind, supervised learning. It’s the one you’ll meet most in payments and banking.
What machine learning can’t do
- It only knows the past. If a new kind of fraud has never appeared in the data, the model has never seen it.
- The world changes. Fraudsters adapt to whatever you block. A model that worked last year needs new data and retraining this year. It’s a game against people who are watching your rules.
- Bad data in, bad rules out. If past transactions were labelled wrongly, the model learns the wrong lesson, very confidently.
People also tend to trust an answer more when a computer gives it. The answer is only as good as the examples behind it.
Next
In the next post: the two main types of problems machine learning solves. “How much?” (regression) and “which one?” (classification), asked of the stock market: how rough will next month be, and will it be a stress month? From there on, the series uses five years of real daily prices as its running example.
References
- Tom M. Mitchell, Machine Learning (McGraw-Hill, 1997), chapter 1, section 1.1: the definition (task, performance measure, experience)
- Kevin P. Murphy, Probabilistic Machine Learning: An Introduction (MIT Press, 2022), chapter 1: supervised, unsupervised and reinforcement learning
- Mike Chen and Weili Zhou, “On Machine Learning Applications in Investments”, in Handbook of Artificial Intelligence and Big Data Applications in Investments (CFA Institute Research Foundation, 2023), chapter 1
- scikit-learn: decision trees