TExcelTAD
Home › ML Foundations: AI, ML, Deep Learning & the ML Lifecycle › Traditional Programming vs Machine Learning

Traditional Programming vs Machine Learning

As a programmer you are used to writing the logic yourself: if this, then that. Machine learning turns that around. You supply examples of inputs and correct outputs, and an algorithm produces the logic. Knowing when each approach fits is one of the most practical skills in the field.

Two ways to get a program

Traditional programming rulesdata programanswers Machine learning dataanswers learningrules (model)
  • In traditional programming, a person studies the problem, writes the rules as code, and the program applies them to data to produce answers.
  • In machine learning, a person collects data together with the right answers (labels). A learning algorithm produces the rules, stored as a model: a set of learned numbers inside a fixed structure. The model is then used like any other function: new data in, answers out.

The learned model is still a program. It is just written by an optimiser instead of a person, and in a form (hundreds or millions of numbers) that is usually not meant to be read.

When rules work, and when they break

Handwritten digits make the contrast concrete. The load_digits data set has 1797 small 8×88 \times 8 grey-scale images of the digits 0 to 9. Telling a 0 from a 1 has an obvious rule: a 0 is a ring with an empty centre, a 1 is a stroke through the middle. Telling all ten digits apart has no such rule that anyone could write.

python · try it yourself
import numpy as np
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

digits = load_digits()
X, y = digits.data, digits.target            # 8x8 images, pixel values 0..16

# Traditional programming: a person writes the rule. "A 0 has an empty centre, a 1 does not."
zero_or_one = (y == 0) | (y == 1)
centre = X[zero_or_one].reshape(-1, 8, 8)[:, 2:6, 3:5].mean(axis=(1, 2))
rule = np.where(centre < 6, 0, 1)
print("hand-written rule, 0 vs 1:", round(np.mean(rule == y[zero_or_one]), 3))

# Machine learning: the same kind of model learns all ten digits from examples.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0, stratify=y)
model = LogisticRegression(max_iter=5000).fit(X_train, y_train)
print("learned model, all 10 digits:", round(model.score(X_test, y_test), 3))
print("numbers the model learned:", model.coef_.size + model.intercept_.size)
hand-written rule, 0 vs 1: 0.975
learned model, all 10 digits: 0.961
numbers the model learned: 650

The rule, "average brightness of the 4×24 \times 2 centre block below 6 means 0", gets 97.5 % of the zeros and ones right. Extending it to ten digits would need dozens of fragile special cases. The learned model handles all ten digits at 96.1 % on 540 test images, using 650 learned numbers: one weight per pixel per digit (64×1064 \times 10) and one bias per digit. Nobody chose those numbers by hand.

Learned rules can be readable

A learned model is not always a black box. A small decision tree prints as nested if-then rules, which makes it a good way to see what "learning the rules" means. Here a bank's approval decisions follow a hidden policy: approve if income is at least 40 (thousand) and the debt ratio is below 0.5. We give the tree only the past decisions.

python · try it yourself
import numpy as np
from sklearn.tree import DecisionTreeClassifier, export_text

# A bank's secret approval policy that we want to recover from past decisions.
rng = np.random.default_rng(4)
income = rng.integers(10, 120, size=300)        # thousand dollars per year
debt_ratio = rng.uniform(0, 1, size=300).round(2)
approved = ((income >= 40) & (debt_ratio < 0.5)).astype(int)

X = np.column_stack([income, debt_ratio])
tree = DecisionTreeClassifier(max_depth=2, random_state=0).fit(X, approved)
print(export_text(tree, feature_names=["income", "debt_ratio"], decimals=2))
print("training accuracy:", tree.score(X, approved))
|--- debt_ratio <= 0.50
|   |--- income <= 39.50
|   |   |--- class: 0
|   |--- income >  39.50
|   |   |--- class: 1
|--- debt_ratio >  0.50
|   |--- class: 0

training accuracy: 1.0

The tree recovered the policy from 300 examples: approve when the debt ratio is low and the income above 39.5. Each threshold sits halfway between two neighbouring values in the data: 39.5 between incomes 39 and 40, and for the debt ratio 0.495 between 0.49 and 0.50, which decimals=2 prints as 0.50. The learned rule matches the true one only as closely as the data allows. Real decisions are noisy and depend on many more columns, and then the learned tree is an approximation, not a copy.

Comparing the two approaches

Traditional programming Machine learning
Input you provide the rules examples with correct answers
Works best when the logic is known, exact and stable the logic is unknown or too complex to write, and examples are plentiful
Correctness provable for all inputs, testable case by case statistical: right most of the time, measured on held-out data
When the world changes a person edits the rules retrain on new data
Failure mode a missing rule (a case nobody thought of) wrong or unrepresentative data; confident mistakes on unusual inputs
Testing unit tests with exact expected outputs metrics on a test set, plus monitoring in production

ML is still software engineering

An ML system is mostly ordinary code: loading and validating data, building features, training scripts, serving APIs, tests and monitoring. A well-known observation from industry is that the model code is a small part of a production ML system; the surrounding pipeline is the bulk of the work. The lifecycle lesson walks through all of it.

Pitfalls

  • Using ML where a rule is known. Learning the Celsius formula from data, as in the introduction, is a demonstration, not good engineering.
  • Expecting exact behaviour. A model with 96 % accuracy is wrong on about 1 in 25 inputs. The system around it must handle mistakes: a fallback, a human review, a confidence threshold.
  • Testing a model like a function. "Input A gives output B" tests break when the model is retrained. Test the pipeline with exact tests, and the model with metrics and thresholds.

Practice

  1. Give one task where you would write rules and one where you would use ML in the same application, for example an online shop.
Answer

Rules: calculating the shipping cost and VAT, which follow fixed tables. ML: ranking products in search results or recommending items, where "relevant" is learned from click and purchase data.

  1. In the digits example, what does the threshold 6 in the rule correspond to in the ML vocabulary of the previous lesson? Who chose it?
Answer

It plays the role of a parameter, but it was chosen by a person looking at the data, not by an optimiser. In ML the same threshold would be learned, as in the driving-test example of What Is Machine Learning?.

Test yourself

In machine learning, what does the developer provide instead of the rules?

  1. The answers only
  2. Examples of inputs with their correct outputs
  3. A list of if-then statements
  4. Nothing; the model needs no input

The logistic-regression digit model learned 650 numbers. Where do they come from?

  1. 64 weights for each of 10 digits plus 10 biases
  2. 650 training images
  3. 650 pixels per image
  4. They are default values

What is a typical failure mode of a learned model that hand-written rules do not have in the same way?

  1. Syntax errors
  2. Confident mistakes on inputs unlike the training data
  3. It cannot be run
  4. It needs a compiler