What is machine learning?

ml basics

This is the first post in a series where I explain machine learning the way I wish someone had explained it to me: plain words, examples from payments and finance, and small code you can run.

Let’s 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.

Traditional programming

Rules

Program

Data

Answers

Machine learning

Data

Learning

Answers

Rules

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:

Two charts of card transactions by amount and hour. The hand-written rule catches the big frauds and misses the small night-time ones. The learned rules catch both.

ApproachFrauds caughtFalse alarms
Hand-written rule14 of 360
Rules learned from data36 of 362

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.

And here is the nice part. The rules it learned are readable:

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

One honest note: 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

WordPlain meaningIn the fraud example
FeatureA fact you know about each exampleAmount, hour of day
LabelThe answer you want to predictFraud or not fraud
TrainingLetting the computer learn from past examplesmodel.fit(...)
ModelThe rules it learnedThe three rules above
PredictionUsing the model on something newFlagging 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

KindWhat you give itFinance example
SupervisedExamples with answersFraud or not; will this loan be repaid?
UnsupervisedExamples without answersGrouping customers by how they spend
ReinforcementA goal and a rewardA 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.

That last point has a human side too. People tend to trust an answer more when a computer gives it. A model’s 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), with examples from lending and payments.

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