Regression and classification
แนะนำใหม่ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| Linear regression/ˈlɪnɪə rɪˈɡreʃn/ | fitting a straight line (or plane) to predict a number |
| least squares/liːst skweəz/ | choosing a model's parameters to minimise the sum of squared errors |
| Logistic regression/ləˈdʒɪstɪk rɪˈɡreʃn/ | a model that predicts the probability of a class using the logistic function |
| decision boundary/dɪˈsɪʒn ˈbaʊndəri/ | ขอบเขตการตัดสินใจ |
| margin/ˈmɑːdʒɪn/ | |
| overfitting/ˌəʊvəˈfɪtɪŋ/ | a model fitting the training data's noise, so it does worse on new data |
| confounder/kənˈfaʊndə/ | a variable that affects both the cause and the effect, creating a misleading link |
| Ridge regression/rɪdʒ rɪˈɡreʃn/ | least squares with a penalty on large weights, which reduces overfitting |
| k-nearest neighbours/keɪ ˈnɪərɪst ˈneɪbəz/ | predicting from the k most similar training examples |
| cross-entropy/krɒs ˈentrəpi/ | a classification loss equal to minus the log of the probability the model gave the true class |
| Standardise/ˈstændədaɪz/ | rescale a feature to mean 0 and standard deviation 1 |
Lines and boundaries
- Linear regression 线性回归 fits $\hat{y} = wx + b$ by least squares 最小二乘法. Logistic regression 逻辑回归 turns a score into a probability with the sigmoid function.
- The decision boundary 决策边界 is where the model changes its answer.

The decision boundary is where —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The score crosses zero: the answer flips.
Least squares, closed form
- Course points $(1,2), (2,3), (3,5), (4,4), (5,6)$: $\bar{x} = 3$, $\bar{y} = 4$, $w = 9/10 = 0.9$, $b = 1.3$. The residuals are $-0.2, -0.1, 1.0, -0.9, 0.2$, and the sum of squared errors (SSE) is $1.9$.
- A logistic boundary with $w_1 = 2, w_2 = -1, b = -1$ is the line $x_2 = 2x_1 - 1$. A parcel at $(2, 2)$ scores $+1$ (late) and one at $(2, 5)$ scores $-2$ (on time). The size of the score is the margin 间隔.
The course five points fit the line —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
w = 9/10, b = 4 - 0.9 x 3 = 1.3; SSE 1.9.
Overfitting, measured
- A 4th-degree polynomial reaches SSE $0$ on the five training points but SSE $\approx 42.7$ on the five held-out points, against $0.55$ for the line. This is overfitting 过拟合: training error only advertises the model; held-out error measures it.
- Choose models by held-out error. Fitted weights are not causes: a variable that affects both $x$ and $y$ (a confounder 混杂因素) can create a large weight on its own.
The 4th-degree polynomial on held-out data scores SSE —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Training error 0, but held-out error 42.7 against 0.55 for the line: the polynomial overfits.
The 4th-degree polynomial that fits the five training points exactly is the better model.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Zero training error came from fitting the noise: on held-out points its SSE is about $42.7$, against $0.55$ for the line. Choose models by held-out error.
Fit quality and regularisation
- $R^2$ compares the fit with simply using the mean. For the course points, the total sum of squares about $\bar{y} = 4$ is $4 + 1 + 1 + 0 + 4 = 10$, and the line leaves SSE $1.9$, so $R^2 = 1 - 1.9/10 = 0.81$.
- Ridge regression 岭回归 adds $\lambda \lVert w \rVert^2$ to the loss. This pulls the weights toward zero and calms the wild polynomial; $\lambda$ is chosen on validation data.
- k-nearest neighbours k近邻 votes among the $k$ closest training points: $k = 1$ overfits, and a very large $k$ underfits.
A model leaves SSE 6 on data whose total sum of squares is 24. What is R^2?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
1 - 6 / 24 = 0.75.
Raising the ridge penalty λ makes the fitted weights —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The penalty charges for large weights, so a bigger λ buys smaller ones at some cost in fit.
From score to probability and loss
- Logistic regression turns a score $z$ into a probability $\sigma(z) = 1/(1 + e^{-z})$: $\sigma(0) = 0.5$ and $\sigma(2) = 0.881$.
- It is trained on cross-entropy 交叉熵 loss, $-\ln p$ for the probability given to the true class. A late parcel given 0.881 costs $-\ln 0.881 = \mathbf{0.127}$; if it had been on time, the loss would be $-\ln 0.119 = 2.13$. Confident mistakes cost the most.
The sigmoid of a score of 0?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
$1/(1 + e^0) = 1/2$.
Cross-entropy loss when the true class gets probability 0.881 (three decimals)?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
$-\ln 0.881 = 0.127$.
Worked example: standardise first
- Distance in km and parcel weight in g have very different scales. So the size of a fitted weight says nothing about importance, and k-nearest neighbours would measure almost only grams.
- Standardise 标准化: $z = (x - \mu)/\sigma$. With $\mu = 120$ and $\sigma = 30$, a value of 180 becomes $\mathbf{2.0}$. Compute $\mu$ and $\sigma$ on the training data only, then apply them to the validation and test data.
Standardise 180 with training mean 120 and standard deviation 30.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
$(180 - 120)/30 = 2$.
Marks that slip away
- Extrapolating the line. The fit holds near the data, $x$ from $1$ to $5$, not far outside it.
- Weights as causes. A large weight shows an association in this data, not that changing $x$ changes $y$.
- Regularising unscaled features. The penalty depends on each feature's units; standardise first.
Recap
- Least squares on the course points: $w = 0.9$, $b = 1.3$, SSE $1.9$.
- A logistic boundary is where the score is $0$; the margin is the size of the score.
- Choose models by held-out error, never training error:
| Model | Training SSE | Held-out SSE |
|---|---|---|
| Straight line | 1.9 | 0.55 |
| 4th-degree polynomial | 0 | 42.7: overfits |
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้