DS701 Session 13 — Mon Oct 19, 2026
Write down the logistic regression model: what linear function does it fit, what quantity is that linear function equal to, and how do you get from it to a probability \(p(x)\)? Then say how the coefficients are chosen (which principle) and why, unlike linear regression, they cannot be found in closed form.
When two columns of the design matrix are nearly linearly dependent (multicollinearity), the least-squares coefficients tend to become very large in magnitude. Explain in a sentence or two why that happens, how ridge regression’s penalty \(c\Vert\boldsymbol{\beta}\Vert_2^2\) addresses it, and what LASSO’s \(c\Vert\boldsymbol{\beta}\Vert_1\) penalty does that ridge cannot.
To turn logistic regression into a classifier the lecture used the rule “say yes if \(p(x) > t\)” and found on the admissions data that F1 peaked near \(t \approx 0.3\), not \(0.5\). Explain what lowering the threshold does to precision and to recall, why \(0.5\) is not automatically the right choice, and give one real setting where you would deliberately pick a low threshold and one where you would pick a high one.
Last week: a continuous \(y\) as a linear function of \(x\). This week: a 0/1 \(y\), and we want a probability.

One observation: \(\;\text{P}(y_i \mid x_i) = p_i^{\,y_i}\,(1-p_i)^{1-y_i}, \qquad p_i = \sigma(\boldsymbol{\beta}^\top\mathbf{x}_i).\)
statsmodels.Logit / sklearn.LogisticRegression do the climb for you and give a coefficient per feature (plus the column of ones for \(\beta_0\)).Today’s activity, Part 1: you write \(\sigma\), \(\ell\), and one gradient step by hand — then check that sklearn lands in the same place.

Admissions: F1 peaks near \(t \approx 0.3\) — the model rarely says “admit” at 0.5 because admits are the minority.

ROC: TPR vs FPR, one point per threshold; diagonal = random; AUC summarizes ranking quality without choosing a threshold (0.69 here).
AUC tells you how good the scores are. It does not tell you where to cut — that depends on the costs of FN vs FP and on the base rate. Today’s stretch: a dataset where 95% accuracy is worthless.
Lecture 07: flexible fits chase noise → high variance. Regularization is the knob:
\[\min_{\boldsymbol{\beta}} \Vert X\boldsymbol{\beta} - \mathbf{y}\Vert_2^2 + c\,R(\boldsymbol{\beta})\]

Longley, ridge: held-out \(R^2\) rises with \(c\), then collapses — the sweet spot is found by cross-validation.

Same contours, different penalty balls. The \(\ell_1\) diamond has corners on the axes — the contours usually hit a corner first, and a corner means a coefficient is exactly zero.

Longley, LASSO: as \(c\) grows, ARMED, UNEMP, POP go to zero; GNP and GNPDEFL remain — the near-dependent variables are the ones dropped.
Ridge never zeroes anything (until \(c \to \infty\), all at once). LASSO gives you model selection for free — instead of searching \(2^n - 1\) subsets — at similar prediction error, with a much more interpretable answer.
Today’s activity, Part 2: coefficient paths for both penalties on data where you know which features matter, CV to pick \(\alpha\), and one fit where you “forget” to standardize and watch the lasso throw away a real feature.
sklearn’s C is \(1/c\)) and for neural networks (weight decay).Goal: build logistic regression from its likelihood and check it against sklearn; trace ridge and lasso paths on data with correlated and irrelevant features, pick \(\alpha\) by CV, and see what forgetting to standardize costs; then take a 90/10 (A1) or 95/5 (B1) imbalanced problem, watch accuracy lie, sweep the threshold, plot ROC and precision–recall, choose a threshold for a stated cost, and try class_weight="balanced".
numpy, pandas, matplotlib, scikit-learn.The activity notebook goes live on the day of the lecture. Colab is optional — you can also open it on GitHub and run it locally.
Full lecture notes: Logistic Regression and Regularization