4.4 · Machine learning & AI: scikit-learn, TensorFlow

Level 4 · Specializations (Web, Data, AI, Automation)

4.4Machine learning & AI: scikit-learn, TensorFlow

Goal: Train and evaluate a first scikit-learn model (fit/predict, train/test split) — to predict from examples instead of coding rules, without falling into the classic traps.
Estimated time: 15 min · self-paced reading · If you only have 5 min: read “The essentials in 30s” + the diagram.

The essentials in 30 seconds

  • ML = learning from examples: fit(X, y) learns, predict predicts, and we evaluate on never-seen data (train_test_split).
  • 5-line workflow: split → LogisticRegression or RandomForest → 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.

Common misinterpretation: “100% accuracy, my model is perfect.” No. Evaluated on training data, 100% measures memory — the model learned the examples by heart. Only the score on never-seen data (the test) tells whether it learned a rule. A 100% on train is an alarm signal, not a victory.
The 5-step ML workflow Split → model → fit → score → audit Split 80/20 Never-seen test,fixed random_stateSimple model Baseline:RandomForest, LogitFit Learns ontrain onlyScore Evaluate on test+ confusionAudit Biases, limits,human decision
Split → model → fit → score → audit
Text version of the diagram (SEO / screen readers / translation): 1) Nabil splits his data: 80% to learn, 20% never seen to evaluate. 2) He chooses a simple baseline (RandomForest). 3) fit learns on train only. 4) score measures on test + confusion matrix. 5) He audits biases and limits before deciding. Lesson: baseline first, always split, pre-trained for deep.

🔎 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

  1. Load a labelled tabular dataset (e.g. load_iris or your own cleaned historical data).
  2. Split with train_test_split (test_size=0.2, fixed random_state): the test stays untouched until evaluation.
  3. Train a baseline (LogisticRegression or RandomForestClassifier): fit on train only.
  4. Evaluate on test (score + confusion matrix), compare to the train score, and note biases and limits before any decision.
You honestly train and evaluate a baseline: one figure on never-seen data, read errors, assumed limits.

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.

Caution: A model also learns the prejudices of its data: audit biases before any automated decision and keep personal data out of shared datasets. Educational content, examples with fictitious data.

✓ 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.

Section exit checklist
  • 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.
Self-rating 1-5: is my evaluation on never-seen data?did I read my errors instead of the score alone?

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.

Check your understanding

Which library to start machine learning?

Additional resources (optional, to go further)

Level 4 examFresh, harder questions to test what you've learned