4.4Machine learning & AI: scikit-learn, TensorFlow
The essentials in 30 seconds
- ML = learning from examples:
fit(X, y)learns,predictpredicts, and we evaluate on never-seen data (train_test_split). - 5-line workflow: split →
LogisticRegressionorRandomForest→fit→score→ confusion matrix; simple baseline first. - Nabil's reflex: 100% on train and 60% in prod = evaluated on train — the split is not optional, and the model also learns biases.
Running mini-case — Nabil predicts unpaid bills
Management asks Nabil, in Algiers: “can we spot clients at risk of non-payment?” Rather than hand-made rules, Nabil trains a RandomForest with scikit-learn on history: 80/20 split with random_state for reproducibility, fit on train, score on never-seen test. First evaluation on train: 100% — illusory; on test: 82% — honest. He delivers a useful, documented baseline, with a bias audit before any decision. Fourth milestone of the running thread: from the analyst who describes to the model that predicts — cautiously.
Sequel to cases 4.1-4.3 — Nabil predicts from his historical data.
1. Learning from examples + the sacred split
Machine learning replaces hand-coded rules with a rule learned from labelled examples: fit(X, y) trains, predict predicts on new data. The golden rule: evaluate on never-seen data with train_test_split — otherwise you measure memory, not learning. Nabil learns it the hard way: 100% on train, 82% on test — only the second figure counts. Fixed random_state = reproducible result.
2. scikit-learn first, deep learning next
scikit-learn covers 80% of tabular business needs with no GPU: LogisticRegression, RandomForestClassifier, same fit/predict/score API, confusion matrix to read errors. Simple baseline first, complicate only if the simple fails. Deep learning (TensorFlow, PyTorch, neural networks, GPU) comes into play for images, text and audio — with the golden rule: start from pre-trained models (Hugging Face) and fine-tune them, never from scratch. And ethics: the model learns data prejudices — Nabil audits biases before any automated decision.
🔎 Concrete example — 🔎 Concrete example — Nabil's baseline
The complete workflow, with the real scikit-learn APIs:
Xtr, Xte, ytr, yte = train_test_split(X, y, test_size=0.2, random_state=0)
modele = RandomForestClassifier().fit(Xtr, ytr) # learns
print(modele.score(Xte, yte)) # ~0.96: evaluated on never-seen
Rule → 100% on train + 60% in prod = split leak
Reading: split, fit, score — three lines, zero magic.
Lesson: random_state = reproducible, debatable result.
Section vocabulary
- Machine learning
- Learning a rule from labelled examples (fit) then predicting on new data (predict), instead of hand-coding rules.
Simple example: Nabil predicts unpaid bills from history rather than hand-made thresholds.
What it is not: if/else rules coded one by one.
Reflex: labelled data in quantity? consider ML.
- Train/test split
- Splitting data: learn on train, evaluate on never-seen test — otherwise you measure memory, not learning.
Simple example: Nabil: 100% on train (memory), 82% on test (reality).
What it is not: evaluating the model on its own training examples.
Reflex: systematic split, fixed random_state.
- scikit-learn (fit/predict)
- Ideal tabular ML library to start: single fit/predict/score API, no GPU, baseline in 5 lines.
Simple example: Nabil's RandomForest in three lines: split, fit, score.
What it is not: a GPU neural network for a clients table.
Reflex: simple baseline first, complicate if needed.
- Deep learning / pre-trained
- Neural networks (TensorFlow, PyTorch, GPU) for images/text/audio; we start from pre-trained models we fine-tune, never from scratch.
Simple example: Nabil will add a pre-trained model to read client reviews, not a home-made network.
What it is not: training a giant from zero on his laptop.
Reflex: image/text/audio? pre-trained + fine-tuning.
In practice — In practice — Train a baseline
- Load a labelled tabular dataset (e.g. load_iris or your own cleaned historical data).
- Split with train_test_split (test_size=0.2, fixed random_state): the test stays untouched until evaluation.
- Train a baseline (LogisticRegression or RandomForestClassifier): fit on train only.
- Evaluate on test (score + confusion matrix), compare to the train score, and note biases and limits before any decision.
Ready-to-say script — presenting your model (30 s)
“This is Nabil: RandomForest baseline, 82% on never-seen data — not 100% on train, that is memory. Confusion matrix read, biases audited: the model assists the decision, it does not replace it.”
Say in committee; show the matrix, not just the score.
Copy-paste template — scikit-learn baseline (tested code)
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix
X, y = load_iris(return_X_y=True)
Xtr, Xte, ytr, yte = train_test_split(X, y, test_size=0.2, random_state=0)
modele = RandomForestClassifier(random_state=0)
modele.fit(Xtr, ytr) # learns on train ALONE
print("train :", modele.score(Xtr, ytr)) # memory (reference)
print("test :", modele.score(Xte, yte)) # learning (true figure)
print(confusion_matrix(yte, modele.predict(Xte)))
First model
One simple dataset (iris), one baseline, one split: understand fit/predict before touching your data.
Tabular data
Like Nabil: scikit-learn suffices in 80% of business cases — with no GPU or deep learning.
Image or text
Move to TensorFlow/PyTorch via pre-trained models to fine-tune, never trained from zero.
Sensitive decision
Credit, hiring, health: audit biases, keep the human as decider, document limits.
✓ Best practices
- Always split: evaluate on never-seen data.
- Simple baseline first, complicate if needed.
- Audit biases and limits before deciding.
✗ Common pitfalls
- Evaluating on train (illusory 100%).
- Bringing out deep learning for a table.
- Ignoring biases learned from data.
Key takeaways
- ML = PATTERNS learned from examples (fit → predict), not coded rules.
- SKLEARN API: fit()/predict()/score(); MANDATORY train/test SPLIT.
- Simple BASELINE first (Logit, RandomForest): 80% of business with no GPU.
- DEEP = neural networks (images/text) via PRE-TRAINED; biases to audit.
48h challenge — apply tomorrow
Train 1 baseline on a tabular dataset: 80/20 split, fit, test score + read, commented confusion matrix.
- I can name fit/predict and the train/test split.
- I can explain the 100%-on-train trap.
- I have 1 baseline evaluated on never-seen data.
Frequently asked questions
Do I need maths before doing ML?
No for a first scikit-learn baseline: the workflow (split, fit, score) is learned by practice. Yes afterwards: statistics and algebra to choose a model, tune its parameters and understand its errors. Nabil advances step by step: useful baseline first, theory as limits arise.
scikit-learn or TensorFlow to start?
scikit-learn without hesitation: tabular, CPU, single API, results in 5 lines — 80% of business needs. TensorFlow or PyTorch next, for images, text and audio via pre-trained models to fine-tune. Deep learning for a clients table is a truck to deliver a letter.
Which library to start machine learning?