CS / data / stats students
Students preparing for ML internships and new-grad roles who want the practical path, not only theory.
Software engineers
Backend and full-stack developers moving into ML engineering, who already write good code and want the ML on top.
Data analysts
Analysts who know SQL and spreadsheets and want to build and ship models, not just report on data.
QA & test engineers
Test automation engineers heading into ML: evaluation, data checks and model testing use the same mindset.
Data engineers
Pipeline builders who want to own the models their data feeds, and the MLOps around them.
Career switchers
Engineers from other fields (EE, mechanical, finance) with some Python, aiming for an ML engineer job.
Prerequisites
| You need | Level | Where to get it |
|---|---|---|
| Data prep and leak-free pipelines | Comfortable | Area 03 |
| Gradient descent, sigmoid, log-loss | Basic | Area 02 |
| pandas | Comfortable | Area 01 |
Where this area sits in the ML lifecycle — and the roles that use it
| Role | How you use this area on the job | Typical first title |
|---|---|---|
| ML engineer | Train, compare and ship tabular models with reproducible pipelines | ML Engineer I / Machine Learning Engineer |
| Data scientist | Choose and explain models; read coefficients and importances | Data Scientist / Applied Scientist |
| AI / LLM engineer | Use classic models as cheap baselines and routers around LLMs | AI Engineer / LLM Engineer |
| MLOps / platform | Package fitted pipelines with joblib and track hyperparameters | MLOps Engineer / ML Platform Engineer |
| Data engineer | Provide features in the shape models need; understand training cost | Data Engineer |
- Build a baseline every model must beat.
- Fit and interpret linear and logistic regression.
- Explain how a decision tree picks splits with Gini impurity.
- Diagnose overfitting from the train–test gap and control tree depth.
- Train random forests and gradient boosting and say how they differ.
- Measure feature importance with permutation importance.
- Use fit / predict / predict_proba and chain steps in a Pipeline.
- Tune hyperparameters with GridSearchCV and save a model with joblib.
| Tool | What it does | Used in this area for | Cost |
|---|---|---|---|
| Python 3.10 – 3.13 | The language of ML: every library on this page is a Python package | Every example and the lab | Free |
| venv + pip | An isolated environment per project and the package installer | Installing the area's packages | Free |
| pandas | Tables (DataFrames): load, clean, group and join data | Loading data and results tables | Free |
| NumPy | Fast n-dimensional arrays and maths | Arrays for features and predictions | Free |
| scikit-learn | Classic ML: preprocessing, models, metrics, pipelines | Models, Pipeline, GridSearchCV | Free |
| Matplotlib | Charts: histograms, scatter plots, learning curves | Plotting fits and depth curves | Free |
| JupyterLab | Notebooks: run code cell by cell and see tables and plots inline | Comparing models cell by cell | Free |
| Google Colab | Hosted notebooks in the browser, with a free GPU on request | No-install option | Free |
Platforms you will meet at work
| Platform | Vendor | What teams use it for |
|---|---|---|
| SageMaker | AWS | Managed notebooks, training jobs, model registry and endpoints |
| Vertex AI | Google Cloud | Training, pipelines, feature store, model serving and Gemini APIs |
| Azure Machine Learning | Microsoft | Workspaces, MLflow tracking, managed endpoints |
| Databricks | Databricks | Spark + notebooks + MLflow on a lakehouse |
| Weights & Biases | W&B | Experiment tracking and model evaluation dashboards |
XGBoost and LightGBM follow the same fit/predict API; scikit-learn's HistGradientBoostingClassifier is built in.
Recommended: WSL 2 (Ubuntu inside Windows) — the same commands as Linux, cloud servers and most ML teams.
- Turn on WSL and install UbuntuOpen PowerShell as Administrator, run the command, then restart.
wsl --install
After the restart an Ubuntu window opens: choose a user name and password. - Install Python's venv support
sudo apt update sudo apt install -y python3 python3-venv python3-pip git make
- Create an environment and install the packages
python3 -m venv ~/ml source ~/ml/bin/activate pip install --upgrade pip pip install pandas numpy scikit-learn matplotlib joblib jupyterlab
Runsource ~/ml/bin/activatein every new terminal. - EditorInstall VS Code, the WSL, Python and Jupyter extensions, then type
code .in Ubuntu. - Check it works
python -c "import sklearn; print(sklearn.__version__)"
Install Python 3.12 from python.org (tick Add python.exe to PATH), then in PowerShell: py -m venv %USERPROFILE%\ml, %USERPROFILE%\ml\Scripts\activate and the same pip install line. Every lab runs the same.
- Install Apple's command-line tools
xcode-select --install
- Install Homebrew and PythonFollow the one-line command on brew.sh, then:
brew install python@3.12 git make
- Create an environment and install the packages
python3 -m venv ~/ml source ~/ml/bin/activate pip install --upgrade pip pip install pandas numpy scikit-learn matplotlib joblib jupyterlab
- EditorInstall VS Code with the Python and Jupyter extensions. Apple-silicon Macs run every lab natively.
- Check it works
python -c "import sklearn; print(sklearn.__version__)"
- Ubuntu / Debian
sudo apt update sudo apt install -y python3 python3-venv python3-pip git make
- Fedora
sudo dnf install -y python3 python3-pip git make
- Arch
sudo pacman -S python python-pip git make
- Create an environment and install the packages
python3 -m venv ~/ml source ~/ml/bin/activate pip install --upgrade pip pip install pandas numpy scikit-learn matplotlib joblib jupyterlab
- Check it works
python -c "import sklearn; print(sklearn.__version__)"
- Open Google ColabGo to colab.research.google.com and sign in with a Google account.
- New notebookFile → New notebook. NumPy, pandas, scikit-learn, Matplotlib and PyTorch are already installed.
- Upload the labClick the folder icon, upload the lab zip, then run
!unzip lab.zip && cd lab && python check.pyin a cell (prefix shell commands with!). - Check the versions
python -c "import sklearn; print(sklearn.__version__)"
- Get the kitDownload the lab kit (it contains
prep.pyandmake_data.py) and runpython make_data.py. - Create
first.pynext to themimport warnings; warnings.filterwarnings("ignore") from prep import load_split, preprocessor X_tr, X_te, y_tr, y_te = load_split() from sklearn.linear_model import LogisticRegression from sklearn.pipeline import Pipeline model = Pipeline([("pre", preprocessor()), ("clf", LogisticRegression(max_iter=1000))]) model.fit(X_tr, y_tr) print("test accuracy:", round(model.score(X_te, y_te), 3)) print("P(churn), first 5 customers:", model.predict_proba(X_te.head())[:, 1].round(2))
- Run it
python first.py
Output (captured from a real run):test accuracy: 0.743 P(churn), first 5 customers: [0.06 0.26 0.35 0.27 0.17]
- What to noticeAccuracy looks fine, but always guessing “stays” already scores 0.70. The probabilities are the useful output, which is why Module 1 starts with a baseline.
| Session | Do this | Time |
|---|---|---|
| Day 1 | Install · first run · Module 1 (linear & logistic) + line-fit widget | 4 h |
| Day 2 | Module 2 (trees, Gini, overfitting) + tree widget | 4 h |
| Day 3 | Module 3 (forests, boosting, importance) | 4 h |
| Day 4 | Module 4 (API, Pipeline, GridSearchCV, joblib) | 3.5 h |
| Day 5 | Lab: churn models · area test | 2.5 h |
Start from zero
New to this? Work through these steps first, in order. Already comfortable? Skim them and jump to the reference below.
Learning from answered examples
Show the computer many rows where you already know the answer. It learns a rule that maps inputs to the answer, then applies it to new rows.
Learning to price used cars by looking at hundreds of past sales.
| tenure | calls | churned? |
|---|---|---|
| 2 | 4 | yes |
| 30 | 0 | no |
| 5 | 3 | ? |
Most business ML — churn, fraud, demand, pricing — is supervised.
Beat the dumbest guess first
Before building a model, see how well “always guess the most common answer” does.
A weather forecaster who always says “same as yesterday” — surprisingly hard to beat.
- 70% stay, 30% churn
- always guess “stay” → 70% accuracy
- a model scoring 72% adds very little
It tells you whether a model's score means anything.
A straight line, then a squashed line
Linear regression draws the best straight line through points. Logistic regression bends that line's output into a 0–1 probability.
A dimmer switch: the linear part turns the knob, the sigmoid keeps the light between off and full.
import numpy as np z = np.array([-3, 0, 3]) print((1 / (1 + np.exp(-z))).round(3))
# output
[0.047 0.5 0.953]They're fast, explainable and often all you need.
Going further: the reference
The same ideas at the next level: precise definitions, tables, rules and interview questions.
Key definitions
- Supervised learning
- Learning a mapping from inputs X to a known answer y, from labelled examples.
- Regression
- Predicting a number: price, demand, minutes.
- Classification
- Predicting a category: churn yes/no, spam, digit 0–9.
- Linear regression
- ŷ = w·x + b, with w and b chosen to minimise squared error.
- Coefficient
- A learned weight; on standardized features its size compares importance and its sign gives direction.
- Logistic regression
- A linear score passed through the sigmoid to give a probability; a classifier.
- Regularization
- A penalty on large weights (L2 ridge, L1 lasso) that reduces overfitting; C = 1/strength in scikit-learn.
- Baseline
- The simplest honest model — predict the mean, or the most frequent class. Every model must beat it.
Try it: fit a line by hand
import numpy as np from sklearn.linear_model import LinearRegression x = np.array([1, 2, 3, 4, 5, 6, 7, 8]).reshape(-1, 1) # X must be 2-D: (n_samples, n_features) y = np.array([2.1, 2.9, 4.2, 4.8, 6.3, 6.8, 8.4, 8.7]) reg = LinearRegression().fit(x, y) print("w =", reg.coef_.round(3), " b =", round(reg.intercept_, 3)) print("prediction for x = 10:", reg.predict([[10]]).round(2)) print("R² =", round(reg.score(x, y), 4))
# output
w = [0.988] b = 1.079
prediction for x = 10: [10.96]
R² = 0.9887import warnings; warnings.filterwarnings("ignore") from prep import load_split, preprocessor X_tr, X_te, y_tr, y_te = load_split() from sklearn.dummy import DummyClassifier from sklearn.linear_model import LogisticRegression from sklearn.pipeline import Pipeline base = DummyClassifier(strategy="most_frequent").fit(X_tr, y_tr) print("baseline accuracy:", round(base.score(X_te, y_te), 3)) logreg = Pipeline([("pre", preprocessor()), ("clf", LogisticRegression(max_iter=1000))]).fit(X_tr, y_tr) print("logreg accuracy :", round(logreg.score(X_te, y_te), 3)) names = logreg.named_steps["pre"].get_feature_names_out() coefs = logreg.named_steps["clf"].coef_[0] for n, c in sorted(zip(names, coefs), key=lambda t: -abs(t[1]))[:5]: print(f" {n:24} {c:+.3f}") print("P(churn) first 3 test rows:", logreg.predict_proba(X_te.head(3))[:, 1].round(3))
# output
baseline accuracy: 0.7
logreg accuracy : 0.743
num__tenure_months -0.693
num__support_calls +0.692
cat__plan_basic +0.461
cat__plan_pro -0.273
cat__plan_plus -0.187
P(churn) first 3 test rows: [0.055 0.258 0.351]Positive coefficients raise churn probability; on standardized inputs bigger magnitude = bigger effect. More support calls → more churn; longer tenure → less. A model you can explain is easier to ship.
| Model | Predicts | Loss | scikit-learn |
|---|---|---|---|
| Linear regression | a number | squared error | LinearRegression, Ridge, Lasso |
| Logistic regression | a probability → class | log-loss | LogisticRegression(C=1.0) |
| Baseline (regression) | the training mean | — | DummyRegressor() |
| Baseline (classification) | the majority class | — | DummyClassifier(strategy="most_frequent") |
Skipping the baseline (planted bug 1 makes it look worse than it is) · passing a 1-D array as X · reading coefficients of unscaled features as importance · treating logistic regression as a regression model · forgetting max_iter and ignoring convergence warnings.
Interview corner
Q. Why is logistic regression called regression? A. It regresses the log-odds linearly on the features; the sigmoid turns that into a probability for classification.
Q. What does regularization do? A. Penalises large weights so the model can't fit noise; L1 can also zero out features.
Key takeaways
- Always start with a baseline.
- Linear regression: numbers; logistic regression: probabilities.
- Coefficients explain the model — on scaled inputs.
Start from zero
New to this? Work through these steps first, in order. Already comfortable? Skim them and jump to the reference below.
Twenty questions with data
A tree asks yes/no questions about the features, one after another, until it reaches an answer.
A doctor's checklist: fever? → cough? → rash? → likely diagnosis.
- support_calls ≥ 3? yes → high churn risk
- no → tenure < 6 months? yes → medium risk
- no → low risk
Trees handle mixed data with no scaling and produce rules people can read.
The best question makes the purest groups
The tree tries many questions and keeps the one that sorts churners and stayers into the cleanest groups.
Sorting laundry: “is it white?” separates the pile better than “does it have buttons?”.
| group | churners / rows | Gini |
|---|---|---|
| before | 30 / 100 | 0.42 |
| calls < 3 | 10 / 70 | 0.24 |
| calls ≥ 3 | 20 / 30 | 0.44 |
Impurity decrease is how every tree-based model picks its splits.
A tree scores 100% on training data and 60% on test data. What happened?
It overfit — memorised training rows. Limit its depth.
Going further: the reference
The same ideas at the next level: precise definitions, tables, rules and interview questions.
Key definitions
- Decision tree
- A flowchart of yes/no questions on features ending in leaves that hold a prediction.
- Split
- A question like tenure_months < 6 that divides rows into two groups.
- Gini impurity
- 1 − Σ pₖ²: 0 when a node is pure; 0.5 for a 50/50 binary node.
- Information gain
- How much a split lowers impurity, weighted by group sizes.
- Leaf
- A final node; it predicts the majority class (or the class share as probability).
- max_depth
- How many questions deep the tree may go — the main overfitting control.
- min_samples_leaf
- The fewest training rows a leaf may hold; bigger = smoother.
- Overfitting
- Memorising training noise: great on training data, worse on new data.
How a split is chosen
def gini(pos, n): p = pos / n return 1 - p ** 2 - (1 - p) ** 2 # parent: 100 rows, 30 churners. Candidate split: calls >= 3 left_n, left_pos = 70, 10 # calls < 3 right_n, right_pos = 30, 20 # calls >= 3 parent = gini(30, 100) child = left_n / 100 * gini(left_pos, left_n) + right_n / 100 * gini(right_pos, right_n) print(f"parent gini {parent:.3f} after split {child:.3f} gain {parent - child:.3f}")
# output
parent gini 0.420 after split 0.305 gain 0.115Try it: grow a tree
import warnings; warnings.filterwarnings("ignore") from prep import load_split, preprocessor X_tr, X_te, y_tr, y_te = load_split() from sklearn.metrics import roc_auc_score from sklearn.pipeline import Pipeline from sklearn.tree import DecisionTreeClassifier, export_text for depth in (2, 4, None): tree = Pipeline([("pre", preprocessor()), ("clf", DecisionTreeClassifier(max_depth=depth, random_state=42))]).fit(X_tr, y_tr) tr = roc_auc_score(y_tr, tree.predict_proba(X_tr)[:, 1]) te = roc_auc_score(y_te, tree.predict_proba(X_te)[:, 1]) print(f"max_depth={str(depth):4} train AUC {tr:.3f} test AUC {te:.3f} leaves {tree[-1].get_n_leaves()}") small = Pipeline([("pre", preprocessor()), ("clf", DecisionTreeClassifier(max_depth=2, random_state=42))]).fit(X_tr, y_tr) print(export_text(small[-1], feature_names=list(small[0].get_feature_names_out()), decimals=2))
# output
max_depth=2 train AUC 0.683 test AUC 0.702 leaves 4
max_depth=4 train AUC 0.751 test AUC 0.673 leaves 16
max_depth=None train AUC 1.000 test AUC 0.563 leaves 393
|--- num__support_calls <= -0.13
| |--- num__tenure_months <= 0.13
| | |--- class: 0
| |--- num__tenure_months > 0.13
| | |--- class: 0
|--- num__support_calls > -0.13
| |--- num__tenure_months <= -0.48
| | |--- class: 1
| |--- num__tenure_months > -0.48
| | |--- class: 0Always print train and test scores side by side. A big gap means overfitting (limit depth, raise min_samples_leaf, add data); both low means underfitting (a richer model or better features). The lab's forest bug is exactly this.
Leaving max_depth=None on small data · judging a tree by its training score · believing deep-tree rules generalise · scaling features for trees (harmless but pointless).
Interview corner
Q. How does a decision tree choose a split? A. It tries candidate thresholds on every feature and picks the one with the largest drop in impurity (Gini or entropy).
Q. How do you stop a tree overfitting? A. max_depth, min_samples_leaf, min_samples_split, pruning (ccp_alpha) — or use an ensemble.
Key takeaways
- Trees split on the purest question, recursively.
- Depth trades underfitting for overfitting.
- Compare train vs test to see which side you're on.
Start from zero
New to this? Work through these steps first, in order. Already comfortable? Skim them and jump to the reference below.
Ask a crowd, not one expert
Train many different trees and average their answers. Individual mistakes cancel out.
Guessing the number of sweets in a jar: the crowd's average beats most individuals.
| tree | P(churn) |
|---|---|
| 1 | 0.8 |
| 2 | 0.5 |
| 3 | 0.6 |
| average | 0.63 |
A robust, low-tuning default for tabular data.
Each new tree fixes the last one's mistakes
Build small trees one after another; each focuses on what the previous trees still get wrong.
Editing a draft in passes: each pass fixes what the last one missed.
- tree 1 predicts 0.5 for everyone
- tree 2 learns the errors: +0.3 for frequent callers
- sum: 0.5 + 0.1 × 0.3 = 0.53 … repeat 200×
Boosting (XGBoost, LightGBM) wins most tabular competitions.
Going further: the reference
The same ideas at the next level: precise definitions, tables, rules and interview questions.
Key definitions
- Ensemble
- Many models combined so their errors partly cancel.
- Bagging
- Bootstrap aggregating: train each model on a random resample of rows, then average.
- Random forest
- Bagged trees that also consider a random subset of features at each split.
- Boosting
- Train models in sequence, each one focusing on the errors left by the previous ones.
- Gradient boosting
- Boosting where each new tree fits the gradient of the loss (the residuals for MSE).
- Learning rate (boosting)
- Shrinks each tree's contribution; smaller needs more trees but generalises better.
- Feature importance
- How much each feature reduced impurity across the trees (or, better, permutation importance).
- Hyperparameter
- A setting you choose before training: n_estimators, max_depth, learning_rate.
import warnings; warnings.filterwarnings("ignore") from prep import load_split, preprocessor X_tr, X_te, y_tr, y_te = load_split() from sklearn.ensemble import HistGradientBoostingClassifier, RandomForestClassifier from sklearn.metrics import roc_auc_score from sklearn.pipeline import Pipeline models = { "random forest": RandomForestClassifier(n_estimators=300, max_depth=4, random_state=42), "grad. boosting": HistGradientBoostingClassifier(max_depth=3, learning_rate=0.05, max_iter=200, random_state=42), } for name, clf in models.items(): m = Pipeline([("pre", preprocessor()), ("clf", clf)]).fit(X_tr, y_tr) tr = roc_auc_score(y_tr, m.predict_proba(X_tr)[:, 1]) te = roc_auc_score(y_te, m.predict_proba(X_te)[:, 1]) print(f"{name:15} train AUC {tr:.3f} test AUC {te:.3f}")
# output
random forest train AUC 0.793 test AUC 0.758
grad. boosting train AUC 0.850 test AUC 0.728import warnings; warnings.filterwarnings("ignore") from prep import load_split, preprocessor X_tr, X_te, y_tr, y_te = load_split() from sklearn.ensemble import RandomForestClassifier from sklearn.inspection import permutation_importance from sklearn.pipeline import Pipeline forest = Pipeline([("pre", preprocessor()), ("clf", RandomForestClassifier(n_estimators=300, max_depth=4, random_state=42))]).fit(X_tr, y_tr) imp = permutation_importance(forest, X_te, y_te, scoring="roc_auc", n_repeats=10, random_state=0) for col, m in sorted(zip(X_te.columns, imp.importances_mean), key=lambda t: -t[1]): print(f"{col:15} AUC drop when shuffled: {m:.3f}")
# output
support_calls AUC drop when shuffled: 0.159
tenure_months AUC drop when shuffled: 0.049
plan AUC drop when shuffled: 0.030
monthly_fee AUC drop when shuffled: 0.007
age AUC drop when shuffled: -0.002
region AUC drop when shuffled: -0.005| Model | Strength | Watch out for | Key hyperparameters |
|---|---|---|---|
| Logistic regression | fast, explainable, strong baseline | needs scaling; linear boundary | C, penalty |
| Decision tree | readable rules, no scaling | overfits | max_depth, min_samples_leaf |
| Random forest | robust default, little tuning | large, slower to predict | n_estimators, max_depth, max_features |
| Gradient boosting | best accuracy on most tables | overfits without early stopping; needs tuning | learning_rate, max_iter, max_depth |
On this churn data logistic regression ties or beats the ensembles because the true pattern is nearly linear. Gradient boosting usually wins on bigger, messier tables. Always compare against the simple model before shipping the complex one.
Trusting impurity-based feature_importances_ for high-cardinality features (use permutation importance) · boosting with a high learning rate and many trees (overfits) · comparing models on different splits · tuning on the test set.
Interview corner
Q. Random forest vs gradient boosting? A. Forest: parallel deep trees averaged — lowers variance, robust. Boosting: sequential shallow trees fitting residuals — lowers bias, usually more accurate but needs tuning.
Q. Why do random forests sample features at each split? A. To decorrelate the trees, so averaging cancels more error.
Key takeaways
- Forests average; boosting corrects.
- Permutation importance beats impurity importance.
- The fanciest model doesn't always win — measure.
Start from zero
New to this? Work through these steps first, in order. Already comfortable? Skim them and jump to the reference below.
Every model has the same buttons
In scikit-learn every model is created, then fit on training data, then asked to predict.
Every car has a key, a pedal and a wheel — learn them once, drive any car.
from sklearn.tree import DecisionTreeClassifier m = DecisionTreeClassifier(max_depth=2) m.fit([[0], [1], [2], [3]], [0, 0, 1, 1]) print(m.predict([[0.4], [2.6]]), m.predict_proba([[2.6]]))
# output
[0 1] [[0. 1.]]Swapping models becomes a one-line change.
Settings you choose vs things it learns
You pick settings like tree depth. The model learns the actual splits or weights during fit.
You choose the oven temperature; the cake does the rising.
| you set | it learns |
|---|---|
| max_depth=4 | the split questions |
| C=1.0 | coef_ weights |
Tuning hyperparameters with cross-validation is how you get the best honest model.
Going further: the reference
The same ideas at the next level: precise definitions, tables, rules and interview questions.
Key definitions
- Estimator
- Any scikit-learn object with
fit; models also havepredict. - Transformer
- An estimator with
fitandtransform: scalers, encoders, imputers. - Parameters vs hyperparameters
- Parameters are learned in fit (coef_, tree splits); hyperparameters are set by you.
- Pipeline
- Chains transformers and a model into one estimator.
- GridSearchCV
- Tries every hyperparameter combination with cross-validation and keeps the best.
- RandomizedSearchCV
- Samples combinations — cheaper for large search spaces.
- Persisting a model
- Saving the fitted Pipeline with joblib so serving uses the exact same recipe.
- random_state
- Seed that makes random steps reproducible.
import warnings; warnings.filterwarnings("ignore") from prep import load_split, preprocessor X_tr, X_te, y_tr, y_te = load_split() from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import GridSearchCV from sklearn.pipeline import Pipeline pipe = Pipeline([("pre", preprocessor()), ("clf", RandomForestClassifier(n_estimators=150, random_state=42))]) grid = GridSearchCV(pipe, {"clf__max_depth": [2, 4, 8, None], "clf__min_samples_leaf": [1, 20]}, cv=5, scoring="roc_auc", n_jobs=1) grid.fit(X_tr, y_tr) print("best params:", grid.best_params_) print("best CV AUC:", round(grid.best_score_, 3)) print("test AUC :", round(grid.score(X_te, y_te), 3))
# output
best params: {'clf__max_depth': 4, 'clf__min_samples_leaf': 1}
best CV AUC: 0.724
test AUC : 0.757import warnings; warnings.filterwarnings("ignore") from prep import load_split, preprocessor X_tr, X_te, y_tr, y_te = load_split() import joblib from sklearn.linear_model import LogisticRegression from sklearn.pipeline import Pipeline model = Pipeline([("pre", preprocessor()), ("clf", LogisticRegression(max_iter=1000))]).fit(X_tr, y_tr) joblib.dump(model, "churn_model.joblib") loaded = joblib.load("churn_model.joblib") new_customer = X_te.head(1).assign(region="central") # unseen region is fine print(new_customer.to_dict("records")[0]) print("P(churn) =", loaded.predict_proba(new_customer)[0, 1].round(3))
# output
{'tenure_months': 26.6, 'monthly_fee': 39.48, 'support_calls': 0, 'age': 34.9, 'plan': 'plus', 'region': 'central'}
P(churn) = 0.06Inside a Pipeline, parameters are addressed as stepname__param: clf__max_depth, pre__num__impute__strategy. A typo raises “Invalid parameter”.
The lab: an honest model comparison
Calling fit on test data · using predict where a metric needs scores (AUC, log-loss) · pickling only the model and re-implementing preprocessing in serving · reporting the best CV score as the final test score.
Interview corner
Q. What's the difference between fit, transform and predict? A. fit learns from data; transform applies a learned transformation; predict applies a learned model to produce outputs.
Q. How would you tune hyperparameters without leaking? A. Cross-validated search on the training data only, with preprocessing inside the Pipeline; touch the test set once at the end.
Key takeaways
- fit on train; predict / predict_proba on new data.
- Search hyperparameters with CV on training data.
- Save and serve the whole Pipeline.
70% of customers stay. What accuracy does the most-frequent baseline get?
Is logistic regression a regression or a classification model?
A node has 50 churners out of 100. Its Gini impurity?
Train AUC 1.00, test AUC 0.59. Diagnosis and fix?
Forest or boosting: which trains trees in parallel?
Why compute AUC from predict_proba, not predict?
What's the hyperparameter name for max_depth of step “clf” in a Pipeline?
You saved only the model, not the scaler. What breaks in serving?
Explain the bias–variance trade-off.
When would you choose logistic regression over gradient boosting?
How does a random forest reduce overfitting compared with one tree?
What is the difference between bagging and boosting?
How do you interpret feature importance safely?
What does C control in LogisticRegression?
Why might a model with higher AUC have lower accuracy?
How do you make results reproducible?
| You see | Why | Fix |
|---|---|---|
| Expected 2D array, got 1D array instead | X must be (n_samples, n_features) | x.reshape(-1, 1) or pass a DataFrame |
| ConvergenceWarning: lbfgs failed to converge | Unscaled features or too few iterations | Scale numbers; max_iter=1000 |
| Invalid parameter 'max_depth' for estimator Pipeline | Missing the step prefix | Use clf__max_depth |
| Train score ≫ test score | Overfitting | Limit depth, raise min_samples_leaf, regularize, more data |
| Model predicts only one class | Imbalance + 0.5 threshold, or a broken baseline | Check predict_proba; tune threshold; class_weight="balanced" |
| NotFittedError | predict called before fit (or on a fresh clone) | Call fit first; keep the fitted object |
| Resource | Why read it |
|---|---|
| James, Witten, Hastie & Tibshirani, An Introduction to Statistical Learning (with Python) (free at statlearning.com) | Linear models, trees, forests and boosting, clearly explained |
| Géron, Hands-On Machine Learning with Scikit-Learn, Keras and TensorFlow (O'Reilly) | Practical scikit-learn workflows, chapters 2–7 |
| scikit-learn User Guide, “Supervised learning” and “Ensembles” (scikit-learn.org) | Official reference for every estimator used here |
| Breiman, “Random Forests” (Machine Learning, 2001); Friedman, “Greedy Function Approximation: A Gradient Boosting Machine” (Annals of Statistics, 2001) | The original papers |
Lab 04 · Churn models: baseline, logistic regression, random forest
models.py trains three churn models on the Lab 03 data and compares them. The table it prints is misleading: the baseline looks worse than it is, AUC is computed from the wrong output, and the forest memorises its training rows. prep.py is correct.
pip install -r requirements.txt, thenpython check.py.- Check 1: what should a “most frequent class” baseline predict? Fix
make_models. - Check 2: AUC needs a score for each row. Which method returns the churn probability?
- Check 3: the forest's train AUC is 1.000. Which hyperparameter in the spec limits it?
- Re-run until all four checks pass and compare accuracy with AUC in the table.
Done when you see PASS: all 4 checks passed — best model logreg, test AUC = 0.766.