ML vs Traditional Programming
Traditional programming
In traditional programming, you create explicit rules.
- Input data + your rules → output
flowchart LR I[Input] --> R["Rules (code)"] --> O[Output]
Example: a tax calculator.
- You have a written policy.
- You implement the policy in code.
Here’s how you’d write a spam filter the traditional way, straight from the book’s playbook:
- Notice patterns — words like “4U,” “credit card,” “free,” and “amazing” show up a lot in spam subject lines.
- Write a detection rule for each pattern, and flag an email as spam if enough of them match.
- Test the program, then repeat steps 1–2 until it’s good enough to launch.
Since spam is a moving target, this list of rules keeps growing — and quickly becomes long, tangled, and hard to maintain.
Machine learning
In ML, the “rules” are learned from data.
- Input data + output labels → learning algorithm → model
- Later: input data + model → predicted output
flowchart LR
subgraph Training
D["Training Data (X)"] --> A[Learning Algorithm]
L["Labels (y)"] --> A
A --> M[Model]
end
subgraph Inference
N["New Input (X_new)"] --> M --> P[Prediction]
end
What changes in your workflow?
Traditional programming focus:
- getting the logic right
- writing tests for rule correctness
ML engineering focus:
- getting training data right
- choosing features
- preventing leakage and overfitting
- measuring with the right metrics
A simple example
Traditional approach (rule-based spam)
def is_spam(email_text: str) -> bool:
keywords = ["free", "winner", "click here"]
text = email_text.lower()
return any(k in text for k in keywords)def is_spam(email_text: str) -> bool:
keywords = ["free", "winner", "click here"]
text = email_text.lower()
return any(k in text for k in keywords)ML approach
You collect labeled examples:
- email text → spam / not spam
Then train a model on those examples — it automatically discovers which words and phrases are the strongest predictors of spam, so the resulting program is shorter, easier to maintain, and usually more accurate than hand-written rules.
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
emails = [
"win a free prize now",
"meeting moved to 3pm",
"click here for a free gift card",
"please review the attached report",
]
labels = [1, 0, 1, 0] # 1 = spam, 0 = ham
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(emails)
model = LogisticRegression()
model.fit(X, labels)
new_email = vectorizer.transform(["free meeting report"])
print("spam probability:", model.predict_proba(new_email)[0][1])from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
emails = [
"win a free prize now",
"meeting moved to 3pm",
"click here for a free gift card",
"please review the attached report",
]
labels = [1, 0, 1, 0] # 1 = spam, 0 = ham
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(emails)
model = LogisticRegression()
model.fit(X, labels)
new_email = vectorizer.transform(["free meeting report"])
print("spam probability:", model.predict_proba(new_email)[0][1])Notice nobody wrote if "free" in textif "free" in text. The model found that “free” and
“win” correlate with spam by looking at the data.
Key takeaway
- Traditional programming is rules-first.
- ML is data-first.
If you don’t have good data (or enough), ML usually disappoints.
🧪 Try It Yourself
Exercise 1 – The Rule-Based Approach
Exercise 2 – Learning the Rule Instead
Exercise 3 – Measuring Accuracy
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
