Sub topic 2
Rules written by hand versus patterns learned from data
This chapter assumes you can read basic Python, work with NumPy arrays, and know what a function is. Earlier chapters defined machine learning. Here we compare it with ordinary programming.
Two ways to build the same function
Both approaches produce a function that maps an input to an output. They differ in where the logic comes from.
- Rule-based programming: a person studies the problem and writes the conditions directly. The inputs are data and rules, and the output is an answer.
- Machine learning: a person supplies example data, often with the correct answers. A training algorithm finds the rules. The inputs are data and answers, and the output is a model that can produce answers for new data.
A worked comparison
The iris dataset has four measurements per flower and three species. First we write the rules ourselves, based on petal sizes. Then we let a shallow decision tree find its own rules from the training data. Both are scored on the same held-out test set.
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=0, stratify=y
)
def rule_based(X):
# Column 2 is petal length, column 3 is petal width.
return np.where(X[:, 2] < 2.5, 0, np.where(X[:, 3] < 1.7, 1, 2))
print("hand-written:", accuracy_score(y_test, rule_based(X_test)))
tree = DecisionTreeClassifier(max_depth=2, random_state=0)
tree.fit(X_train, y_train)
print("learned:", tree.score(X_test, y_test))The two scores will be close. The learned tree also has the shape of two nested conditions, so on this small problem it finds something very similar to our rules. The difference is who chose the thresholds. We picked 2.5 and 1.7 by looking at the data ourselves. The tree picked its thresholds by searching the training set for splits that separate the species best.
When each approach fits
| Situation | Better fit |
|---|---|
| The logic is known and exact, such as tax rules or unit conversion | Hand-written rules |
| Mistakes must be fully explainable and auditable | Hand-written rules |
| The rules are too many or too subtle to write down, such as spotting spam or reading handwriting | Learning |
| The pattern changes over time and must be refreshed from new data | Learning |
| You have no labeled or representative data | Hand-written rules |
Common errors
- Using learning when a rule exists. If you can state the rule exactly, write it. A model only approximates it and adds data collection and evaluation costs.
- Treating the model as a rulebook. A learned model reflects its training data. If that data is biased or unrepresentative, the learned rules will be too, and no one reviewed them.
- Skipping the test set. A hand-written rule has been checked against the cases its author thought of. A learned model has been fitted to its training data, so its score on that data says little. Always measure on held-out examples.
- Assuming learning removes human work. People still choose the features, collect the data, define success, and check results.
Exercise
A company wants software that converts prices from euros to dollars at a published daily exchange rate. A second team wants software that flags suspicious card transactions, with millions of past transactions labeled as fraud or not fraud. Which task suits hand-written rules and which suits learning? Give one reason for each.
Show answer
The currency conversion suits hand-written rules, because the logic is exact and known (multiply by the published rate), so a learned approximation would only add error. Fraud detection suits learning, because the patterns are too many and too subtle to list by hand, and labeled examples are available for an algorithm to learn from.