Lecture 3 - Introduction to Big Data for Lean
Werkwijze
Vier mini-projecten
Zie ugain.naert.net voor meer uitleg!
JupyterLite
Geen installatie.
Goed voor snel starten, maar beperkt in packages en rekenkracht.
Google Colab
Cloud-notebooks.
Handig voor TensorFlow, maar afhankelijk van account, netwerk en sessies.
Lokaal met VS Code
Aanbevolen op lange termijn.
Gebruik Python 3.12 of 3.13; Python 3.14 is voorlopig minder geschikt voor TensorFlow.
Tip
Als je zelf Python ervaring hebt: gebruik eigen, lokale omgeving met uv (of conda). Zo niet: kies liefst voor Colab met Jupyter Lite als backup.
AI helpt
Jij beslist
Tip
Vraag eerst om een plan. Laat daarna stap per stap code maken en uitvoeren.
Online taalmodellen
Werken via de browser en zijn genoeg om met de startprompts uit deze les te werken.
Code-assistenten
Handig in een editor of terminal, vooral als je vaker programmeert.
Tip
Voor deze les volstaat een online taalmodel. Gebruik het als sparringpartner: lees het plan, voer code in kleine stappen uit en blijf zelf controleren.
Wanneer wordt een flexibeler model beter, en wanneer begint het ruis te leren?
Dataset: California Housing.
Taak: voorspel mediane huiswaarde uit mediaan inkomen.
California Housing
MedHouseValMedIncModelcomplexiteit
Goed
Niet goed
Signaal
Graad 1 is stabiel, maar beperkt.
Een kleine extra kromming helpt.
Waarschuwing
Hoge graden kunnen numeriek en statistisch instabiel worden.
De validatiefout verraadt dat. De trainingsfout niet.
Note
De y-as van de foutgrafiek is logaritmisch omdat de slechtste graden zo hard ontsporen.
Use scikit-learn and the California Housing dataset to investigate model
complexity in polynomial regression. Predict MedHouseVal from MedInc. Reserve
20% of the complete dataset as an untouched test set, then draw a reproducible
sample of 400 observations from the remaining development data.
Compare polynomial degrees 1 through 12 using five-fold cross-validation. Keep
all scaling and polynomial transformations inside a Pipeline. Use training and
validation MSE to select the degree; do not inspect the test set.
Make the fitted curves and training-versus-validation error curve understandable.
After selecting the degree, fit the final model on the 400 development
observations and use the test set once to report MSE and R2. First propose a
short plan. Do not write code yet.
Hoe zie je dat een groot netwerk blijft leren op training, maar niet meer op nieuwe data?
Dataset: Fashion-MNIST.
Taak: classificeer kleine afbeeldingen van kledingstukken.
Fashion-MNIST
Leren doorheen de tijd
Compact model
Langzamer, maar stabiel.
Validatieverlies blijft rustig dalen.
Groter model
Leert sneller.
Na enkele epochs daalt training verder terwijl validatie slechter wordt.


Use Keras to compare a compact dense neural network with a substantially larger
one on Fashion-MNIST. The goal is to investigate how model capacity affects
training and validation loss and when overfitting becomes visible.
Use a limited, reproducible and stratified sample from the official development
data so the exercise remains quick. Keep the official test set completely
separate until all choices have been made. Choose and motivate suitable
architectures, preprocessing, loss function, optimizer and number of epochs.
Visualize training and validation loss per epoch for both models. Then evaluate
only the selected model once on the test set. Show predictions and a confusion
matrix. Do not claim overfitting unless the learning curves provide evidence.
Wat win je en verlies je wanneer je van een kleine boom naar een forest gaat?
Dataset: Titanic.
Taak: voorspel overleving uit klasse, geslacht, leeftijd, familie en tarief.
Titanic
survivedageConfusion matrix
Train
Leer imputatie, encoding en modelparameters.
Validatie
Vergelijk boom en forest via cross-validatie binnen de trainingdata.
Test
Een keer gebruiken nadat de instellingen gekozen zijn.
Boom
Direct uitlegbaar.
Maar een kleine boom mist veel werkelijke overlevenden.
Forest
Iets betere recall voor overleven.
Maar geen enkele boom vat het hele model samen.
Write Python code that trains a decision tree and a random forest on the Titanic
dataset to predict passenger survival. Use pclass, sex, age, sibsp, parch and
fare.
Start with a stratified train/test split and keep the test set untouched until
the final evaluation. On the training data only, use cross-validation to compare
a shallow decision tree with a random forest and, if needed, tune hyperparameters.
Impute missing numerical values using medians learned exclusively inside each
training fold, and encode categorical features inside the same workflow. Prefer
a scikit-learn Pipeline. Do not remove observations merely because age is
missing.
After choosing settings, fit both models on the full training data. Only then
evaluate them once on the test set. Report accuracy, precision, recall and F1
for the survived class; show test-set confusion matrices and a readable tree.
Kunnen we niet alleen het gemiddelde voorspellen, maar ook de onzekerheid laten meegroeien?
Dataset: restaurantfooien.
Taak: voorspel fooi uit het totale bedrag.
Tips
total_billtipHeteroscedasticiteit
De spreiding kan toenemen naarmate de rekening hoger wordt.
We modelleren dus gemiddelde en standaardafwijking.
\[ y \sim \mathcal{N}(\mu(x), \sigma(x)) \]
\[ \mu(x) = \alpha + \beta x,\qquad \sigma(x) = \sigma_0 + \sigma_1 x \]
Note
Omdat \(\sigma_1 \geq 0\) nemen we vooraf aan dat de spreiding gelijk blijft of stijgt.
Modelcontrole
Rekencontrole
Use PyMC to fit a heteroscedastic Bayesian regression model to the seaborn Tips
dataset. Predict tip from total_bill and rescale total_bill to [0, 1].
tip ~ Normal(mu(x), sigma(x))
mu(x) = alpha + beta * x_scaled
sigma(x) = sigma_0 + sigma_1 * x_scaled
Use sensible normal priors for alpha and beta and half-normal priors for sigma_0
and sigma_1. Use prior-predictive simulation before fitting the model.
Sample the posterior using MCMC. Summarize and interpret all four parameters,
and check convergence using R-hat, effective sample size, trace plots and the
number of divergences. Explain what sigma_1 says about changing variability,
while recognizing that the half-normal prior assumes variability cannot decrease.
Use posterior-predictive draws to visualize the fitted relationship and its
uncertainty. Do not construct a posterior-predictive interval merely by plugging
posterior mean parameters into mu +/- sigma.
Niet te kennen voor het examen