Handbooks / Machine Learning Basics / Chapter 2

Linear and Logistic Regression

7 pages · ~5 min✓ Reviewed

Builds on ML Foundations. Next up: Model Evaluation.

Machine Learning BasicsPage 1

Regression Mental Model

1. Linear regression

  • Predicts a number: ŷ = w·x + b
  • A weighted sum of features plus a bias
  • Learning = choose w and b that minimize a loss over the training data
  • Residual = y − ŷ; the fit line has the smallest Σ residual²

2. Logistic regression

  1. z = w·x + blinear score
  2. p = σ(z)probability
  3. p ≥ 0.5 ?threshold
  4. Class0 or 1
LinearLogistic
Predictsa numbera class probability
Formulaŷ = w·x + bp = σ(w·x + b)
Last stepnonethreshold (default 0.5)

3. Both are linear models

  • Boundary is where p = 0.5, i.e. w·x + b = 0
  • A straight line in 2D, a hyperplane in higher dimensions
  • Class flips as w·x + b changes sign
Remember

The sigmoid bends the probability, not the boundary. Logistic regression still separates classes with a flat line or plane.

Machine Learning BasicsPage 2

Least Squares and Gradient Descent

1. Least Squares and the Normal Equation

  • Least squares minimizes Σ(y − ŷ)², the sum of squared residuals
  • Closed form: w = (XᵀX)⁻¹Xᵀy
  • Cost about O(n·d²+d³): fine for small d
  • Fails when XᵀX is singular (collinear features)

2. Gradient Descent

  1. Predict ŷcurrent w
  2. Gradient∂L/∂w
  3. Updatew ← w − α·∂L/∂w
  4. Loss falling?repeat until it stops
α (learning rate)Effect
Too largediverges
Too smallcrawls
Just right + standardized featuresconverges fast

3. Batch, SGD, Mini-batch

VariantRows per step
Batchall rows
SGDone row
Mini-batcha small chunk (default in practice)
Remember

Always standardize features before gradient descent. Use the normal equation only for small d.

Machine Learning BasicsPage 3

Sigmoid and Logistic Regression

1. Sigmoid and logit

  • σ(z) = 1 / (1 + e^−z) squashes any real z into (0, 1)
  • σ(0) = 0.5
  • Logit = ln(p / (1 − p)) = w·x + b: linear in log-odds

2. Reading a weight

  • w = change in log-odds per one-unit increase in a feature
  • e^w = odds ratio for that one-unit increase
we^wEffect
01none
0.692odds double
−0.690.5odds halve

3. Training and threshold

  • Log loss = −(1/n)·Σ[y·ln p + (1−y)·ln(1−p)]
  • Gradient w.r.t. w: Xᵀ(p − y)/n, same form as linear regression
ThresholdFavorsCost
Below 0.5recall: positives caughtmore false positives
Above 0.5precision: positives correctmore misses
Separable data

Weights grow without bound unless regularized.

Machine Learning BasicsPage 4

Loss Functions: MSE and Log Loss

1. MSE and Its Cousins

  • MSE = (1/n)·Σ(y − ŷ)²
  • Convex for linear regression
  • Big errors cost quadratically; outliers dominate
MSEMAEHuber
Penaltysquaredabsolutesquared small, linear large
Outlierssensitiverobustrobust

2. Log Loss

  • Log loss = binary cross-entropy: −(1/n)·Σ[y·ln p + (1−y)·ln(1−p)]
  • y=1 keeps ln p; y=0 keeps ln(1−p)
  • Confident and wrong costs a lot: p = 0.01, y = 1 → −ln 0.01 ≈ 4.6
python
from math import log
def log_loss(ys, ps, e=1e-15):
    ps = [min(max(p, e), 1 - e) for p in ps]
    return -sum(y*log(p) + (1-y)*log(1-p) for y, p in zip(ys, ps))/len(ys)
print(round(log_loss([1], [0.01]), 2))

clipping to [ε, 1−ε] avoids ln 0 = −∞

output
4.61
  • Interview Point: why not MSE for classification? Non-convex with sigmoid, weak gradients.
  • Which loss for outliers? MAE or Huber.
Machine Learning BasicsPage 5

Regularization: L1 and L2

1. Penalized Loss

  • Penalty on weights added to the loss to fight overfitting
  • L = loss + λ·penalty
L2 (Ridge)L1 (Lasso)
Penaltyλ·Σw²λ·Σ|w|
Effectshrinks smoothly, rarely exactly 0drives some weights exactly 0
Constraint shapecircle: misses the axesdiamond: hits the axes (sparse)
Bonusnonebuilt-in feature selection

2. Tuning λ

  • Larger λ → more bias, less variance
  • Choose λ by cross-validation
  • scikit-learn logistic regression uses C = 1/λ: small C = strong penalty
python
from sklearn.linear_model import Ridge, Lasso, LogisticRegression
Ridge(alpha=1.0)  # L2, alpha = λ
Lasso(alpha=0.1)  # L1
LogisticRegression(C=0.1)  # C = 1/λ

alpha is λ; C is its inverse

3. Rules and Elastic Net

Elastic Net

Mixes L1 + L2. Sparse like L1, and handles correlated features better than L1 alone.

Machine Learning BasicsPage 6

Regression Cheat Sheet

1. Models and Losses

LinearLogistic
Predictsŷ = Xw + bp = σ(Xw + b)
LossMSElog loss
Outliers dominateMAE or Hubern/a

2. Fitting

  • Both models: w ← w − α·Xᵀ(ŷ − y)/n, with ŷ = p for logistic
  • Normal equation w = (XᵀX)⁻¹Xᵀy: small d only

3. Regularize and Check

PenaltyEffect
L2 (Ridge)λ·Σw²shrinks
L1 (Lasso)λ·Σ|w|selects
Elastic Netbothboth
  • Standardize features
  • Check the learning-rate curve
  • Hold out a validation set
  • Pick the threshold from precision vs recall
Tune λ

Choose λ by cross-validation. Larger λ means more bias, less variance.

Machine Learning BasicsPage 7

Check yourself

A logistic model outputs p = 0.01 for a row whose true label is 1. What is that row's log loss, and why does it matter?
  • −ln 0.01 ≈ 4.6
  • A hedged p = 0.4 costs only about 0.9
  • Confident wrong predictions dominate the loss
Gradient descent loss rises every step, then becomes NaN. What is wrong and what fixes it?
  • Learning rate α is too large, so the steps overshoot and diverge
  • Lower α and standardize the features
Two models use the same λ. One ends with several weights exactly 0. Which penalty was it?
  • L1: its diamond constraint hits the axes
  • L2 shrinks weights but rarely zeroes them
Unregularized logistic regression is fit on perfectly separable data. What happens to the weights?
  • They grow without bound, because larger weights keep lowering the loss
  • Add regularization such as L2, or lower C