Final 计算题|A 第 1–3 讲
期末复习用的 10 道非官方自拟计算题,覆盖 KNN、线性回归、梯度下降、偏差方差与交叉验证、逻辑回归、softmax、朴素贝叶斯与贝叶斯法则,附逐步解答与课件页码。
手机上宽表格可左右滑动;自测答案默认收起,点击题目下方按钮查看。
使用边界:期末复习,非官方材料。课程信息写期末闭卷、3 小时、选择题加数学计算题;日期、地点与范围尚未公布,以讲师通知和课堂公告为准。本页是按第 1–3 讲课件自拟的计算题,数值重新设计,方法与约定照课件。
复习资料:考试形式与范围地图 · 闭卷公式默写表 · 算法横向对照
先做题再看解析:A 计算:第 1–3 讲 · B 选择:第 1–3 讲 · C 计算:第 4–7 讲 · D 选择:第 4–7 讲 · E 计算:第 8–10 讲 · F 选择:第 8–10 讲
怎么做:闭卷、限时,先把每一步写在纸上,再展开对照;算错的题回到回查里的讲义页重做一遍。
A 计算题
约定照课件:线性回归的 MSE 除以 N;交叉熵用自然对数 ln;KNN 的距离度量按题面,K 取奇数;朴素贝叶斯不做平滑,题目数据不含零计数。
A1 · 3-NN and 5-NN classification
Training set (2 features, label): P1 (1, 1) A; P2 (2, 3) B; P3 (4, 2) A; P4 (3, 5) B; P5 (5, 4) A; P6 (0, 2) A. Query q = (3, 3). Use Euclidean distance and majority vote. (a) Predict the label with K = 3. (b) Predict the label with K = 5.
A1 · 查看答案与解析
答案:(a) B;(b) A。
- 距离:P1 √8 ≈ 2.828;P2 1;P3 √2 ≈ 1.414;P4 2;P5 √5 ≈ 2.236;P6 √10 ≈ 3.162。
- 升序:P2 B (1) < P3 A (1.414) < P4 B (2) < P5 A (2.236) < P1 A (2.828) < P6 A (3.162)。
- K = 3:B, A, B → B。
- K = 5:再加 P5 A、P1 A,A 三票对 B 两票 → A。
English: Distances from q: P2 = 1, P3 = 1.414, P4 = 2, P5 = 2.236, P1 = 2.828, P6 = 3.162. With K = 3 the neighbours are P2 (B), P3 (A), P4 (B), so the majority label is B. With K = 5 we add P5 (A) and P1 (A), giving 3 A vs 2 B, so the prediction becomes A. The choice of K changes the result.
易错点:比大小可以只比距离平方,答卷里写距离要开方;K 变大预测可能翻转,K 是要调的超参数。
A2 · KNN regression with Manhattan distance
Training set (x₁, x₂) → y: (1, 4) → 10; (2, 2) → 14; (5, 1) → 20; (3, 3) → 12; (6, 5) → 30. Query q = (3, 2). Use Manhattan (L1) distance d = |x₁ − q₁| + |x₂ − q₂| and K = 3; the prediction is the mean of the K neighbours’ targets.
A2 · 查看答案与解析
答案:ŷ = 46/3 ≈ 15.33。
- L1 距离依次是 4、1、3、1、6。
- 升序:(2,2) 1,(3,3) 1,(5,1) 3,(1,4) 4,(6,5) 6。前两名同为 1,K = 3 时都入选,平票不影响结果。
- 前三个目标值 14、12、20,平均 46/3 ≈ 15.33。
English: L1 distances are 4, 1, 3, 1, 6. The three nearest neighbours are (2,2), (3,3) and (5,1) with targets 14, 12 and 20. KNN regression predicts their mean: (14 + 12 + 20)/3 = 15.33.
易错点:回归第 4 步取平均(或加权平均),不用多数投票;题面指定 L1 就别算成欧氏距离。
回查:L01 p.50 KNN 回归第 4 步与距离度量自定(L01c);p.49 课件示例用逐元素绝对误差之和(L01b)。
A3 · Closed-form least squares
Fit y = wx + b by least squares to (1, 1), (2, 4), (3, 4), (4, 7). (a) Find w and b using the closed-form solution. (b) Compute the training MSE (divide by N). (c) Predict y at x = 5.
A3 · 查看答案与解析
答案:w = 1.8,b = −0.5;MSE = 0.45;ŷ(5) = 8.5。
- x̄ = 2.5,ȳ = 4。
- 分子 Σ(xⁿ − x̄)(ȳⁿ − ȳ) = 4.5 + 0 + 0 + 4.5 = 9;分母 Σ(xⁿ − x̄)² = 2.25 + 0.25 + 0.25 + 2.25 = 5。
- w = 9/5 = 1.8;b = ȳ − w x̄ = 4 − 4.5 = −0.5。
- 预测 1.3、3.1、4.9、6.7,残差 −0.3、0.9、−0.9、0.3,平方和 1.8,MSE = 1.8/4 = 0.45。
- ŷ(5) = 9 − 0.5 = 8.5。
English: x̄ = 2.5, ȳ = 4. w = Σ(xⁿ − x̄)(ȳⁿ − ȳ) / Σ(xⁿ − x̄)² = 9/5 = 1.8, and b = ȳ − w·x̄ = 4 − 4.5 = −0.5. Residuals are −0.3, 0.9, −0.9, 0.3, so MSE = (0.09 + 0.81 + 0.81 + 0.09)/4 = 0.45. At x = 5, ŷ = 8.5.
易错点:b 要用算好的 w 回代;MSE 按课件除以 N,不是 N − 1;分子里 x、y 都要减各自均值。
回查:L02 p.17 MSE、p.18 闭式解(L02a)。
A4 · Two steps of gradient descent
Data (x, ȳ): (1, 3), (2, 5). Model y = wx + b, loss L = (1/N)Σ(ȳⁿ − wxⁿ − b)². Start from w = 0, b = 0 with learning rate η = 0.1. Perform two gradient-descent steps, updating w and b simultaneously, and report L before and after each step.
A4 · 查看答案与解析
答案:初始 L = 17;第一步后 w = 1.3,b = 0.8,L = 1.685;第二步后 w = 1.71,b = 1.05,L = 0.16925(约 0.169)。
- 初始残差 3、5,L₀ = (9 + 25)/2 = 17。
- 第一步:∂L/∂w = −(2/2)(1×3 + 2×5) = −13,∂L/∂b = −(3 + 5) = −8;w = 1.3,b = 0.8。残差 0.9、1.6,L₁ = (0.81 + 2.56)/2 = 1.685。
- 第二步:∂L/∂w = −(0.9 + 3.2) = −4.1,∂L/∂b = −2.5;w = 1.3 + 0.41 = 1.71,b = 0.8 + 0.25 = 1.05。残差 0.24、0.53,L₂ = (0.0576 + 0.2809)/2 = 0.16925。
English: Step 1: residuals are 3 and 5, so ∂L/∂w = −(2/N)Σxⁿrⁿ = −13 and ∂L/∂b = −(2/N)Σrⁿ = −8. Update: w = 1.3, b = 0.8, and the loss drops from 17 to 1.685. Step 2: residuals are 0.9 and 1.6, giving gradients −4.1 and −2.5; w = 1.71, b = 1.05, loss 0.169. The step shrinks as the gradient shrinks.
易错点:w、b 用同一组旧值算梯度后同时更新;负号来自对 −(wx + b) 求导;2/N 的 N 是样本数 2。
回查:L02 p.21 GD 四步、p.22 梯度公式与同时更新(L02a-2)。
A5 · Bias, variance and N-fold cross validation
(a) The same model class is trained on four different training sets. At a test point whose true value is 3.5, the four trained models predict 2.0, 3.0, 2.5, 4.5. Compute the mean prediction m, the gap between m and the true value, and the variance s = (1/N)Σ(xⁿ − m)² of the predictions.
(b) Two candidate models are compared with 4-fold cross validation on the training set. Validation errors per fold: Model 1 (degree 1): 4.2, 3.8, 4.5, 3.9; Model 2 (degree 5): 1.2, 6.0, 0.9, 5.1. Model 1’s training error is 3.9; Model 2’s training error is 0.4. Which model does cross validation select, and what role does the test set play?
A5 · 查看答案与解析
答案:(a) m = 3.0,比真值低 0.5,s = 0.875。(b) 平均验证误差 Model 1 为 4.1、Model 2 为 3.3,选 Model 2;测试集只在选定后报一次最终成绩。
- (a) m = 12/4 = 3.0,m − 3.5 = −0.5。
- 离差 −1、0、−0.5、1.5,平方和 3.5,s = 3.5/4 = 0.875。
- (b) Model 1 平均 16.4/4 = 4.1,Model 2 平均 13.2/4 = 3.3,3.3 < 4.1 → Model 2。
- 诊断:Model 1 训练误差 3.9 就很大,拟合不了训练集,属大偏差;Model 2 各折误差从 0.9 到 6.0 散得开,有方差偏大的迹象。
English: (a) m = 3.0, which is 0.5 below the true value 3.5; s = (1 + 0 + 0.25 + 2.25)/4 = 0.875. (b) N-fold CV averages the N validation errors: Model 1 gives 4.1 and Model 2 gives 3.3, so Model 2 is selected. The test set is used only once, after selection, to report the final error. Model 1 cannot fit the training data (training error 3.9), which indicates large bias.
易错点:按平均验证误差选,不按训练误差选(那样永远选更复杂的模型);测试集不能用来挑模型;方差除以 N。
回查:L02 p.32 方差公式、p.34 诊断规则、p.37 交叉验证(L02b-2)。
A6 · Sigmoid, cross entropy and one gradient step
A logistic regression model has w = (0.5, 1), b = 0.5. One training sample: x = (2, −1), label ȳ = 1. (a) Compute t = w’x + b, the output y = σ(t), and the cross-entropy loss (natural log). (b) Compute dL/dw and dL/db. (c) Do one gradient-descent step with η = 0.5 and recompute y.
A6 · 查看答案与解析
答案:(a) t = 0.5,y ≈ 0.6225,L ≈ 0.4741。(b) dL/dw ≈ (−0.7551, 0.3775),dL/db ≈ −0.3775。(c) w ≈ (0.8775, 0.8112),b ≈ 0.6888,新 t ≈ 1.6326,新 y ≈ 0.8365。
- t = 1 − 1 + 0.5 = 0.5,y = 1/(1 + e^(−0.5)) ≈ 0.6225。
- ȳ = 1 时 L = −ln y ≈ 0.4741。
- y − ȳ ≈ −0.3775,乘 x 得 dL/dw ≈ (−0.7551, 0.3775),dL/db ≈ −0.3775。
- w ← w − 0.5 × dL/dw ≈ (0.8775, 0.8112),b ≈ 0.6888。
- 新 t ≈ 1.6326,新 y ≈ 0.8365,更接近标签 1(新损失约 0.1785)。
English: t = w’x + b = 0.5, so y = σ(0.5) = 0.6225 and the cross-entropy loss is −ln(0.6225) = 0.4741. The gradients are dL/dw = (y − ȳ)x = (−0.7551, 0.3775) and dL/db = y − ȳ = −0.3775. After one step with η = 0.5, w = (0.8775, 0.8112), b = 0.6888, and the new output is σ(1.6326) = 0.8365, closer to the label.
易错点:σ’ = y(1 − y) 已在交叉熵梯度里约掉,不要再乘;第二个特征为负,w₂ 的梯度为正、更新后变小;题面要求 ln。
A7 · Decision boundary of logistic regression
A trained logistic regression on two features has w = (2, −1), b = −4. (a) Write the decision boundary as an equation and give its intercepts on the x₁ and x₂ axes. (b) Classify (3, 1) and (1, 0) with threshold 0.5 and give σ(t) for each.
A7 · 查看答案与解析
答案:(a) 2x₁ − x₂ − 4 = 0,即 x₂ = 2x₁ − 4,过 (2, 0) 与 (0, −4)。(b) (3, 1):t = 1,σ ≈ 0.7311,类 1;(1, 0):t = −2,σ ≈ 0.1192,类 0。
- y = 0.5 ⇔ t = 0 ⇔ w’x + b = 0。
- 令 x₂ = 0 得 x₁ = 2;令 x₁ = 0 得 x₂ = −4。
- (3, 1):t = 6 − 1 − 4 = 1 > 0 → 类 1。
- (1, 0):t = 2 − 4 = −2 < 0 → 类 0。
English: The boundary is where y = 0.5, i.e. w’x + b = 0: 2x₁ − x₂ − 4 = 0, a straight line through (2, 0) and (0, −4). For (3, 1), t = 1 and σ(1) = 0.7311 > 0.5, so it is class 1. For (1, 0), t = −2 and σ(−2) = 0.1192 < 0.5, so it is class 0.
易错点:判类只看 t 的正负;边界在原始特征空间里是直线;b 为负,代入别丢符号。
回查:L03 p.22 决策边界(L03a-4)。
A8 · Softmax probabilities and multi-class cross entropy
A 3-class softmax classifier has w₁ = (0.5, 0.5), b₁ = 0; w₂ = (1, −0.5), b₂ = 0.5; w₃ = (−0.5, 0), b₃ = 0. Input x = (1, 2), true class 2 (one-hot label (0, 1, 0)). (a) Compute the three scores w_c’x + b_c and the softmax probabilities. (b) Compute the cross-entropy loss (natural log). (c) Which class is predicted?
A8 · 查看答案与解析
答案:(a) 分数 1.5、0.5、−0.5;概率约 0.6652、0.2447、0.0900。(b) L ≈ 1.4076。(c) 预测类 1,与真实类 2 不符。
- z₁ = 1.5,z₂ = 1 − 1 + 0.5 = 0.5,z₃ = −0.5。
- 指数约 4.4817、1.6487、0.6065,和约 6.7369。
- 各除以和:0.6652、0.2447、0.0900,三者和为 1。
- one-hot 标签下多类交叉熵只剩正确类:−ln 0.2447 ≈ 1.4076。
- 最大概率在类 1。
English: The scores are 1.5, 0.5 and −0.5. Softmax gives exp(z_c)/Σexp(z) = 4.4817/6.7369 = 0.6652, 1.6487/6.7369 = 0.2447 and 0.6065/6.7369 = 0.0900. With a one-hot label for class 2, the cross entropy is −ln(0.2447) = 1.4076. The model predicts class 1, so this sample is misclassified.
易错点:交叉熵取正确类的概率,不取最大概率(取了会得 0.4076);分母要加全部三类的指数。
回查:L03 p.8 one-hot、p.23 softmax 与多类交叉熵(L03a-4)。
A9 · Naive Bayes with one categorical and one real-valued feature
A training set has 10 samples: 6 labelled Yes, 4 labelled No. Feature 1 (Wind) is categorical: Wind = Strong appears 2 times among the Yes samples and 3 times among the No samples. Feature 2 (Temperature) is real-valued and modelled by a Gaussian within each class: Yes has μ = 22, σ² = 4; No has μ = 28, σ² = 9. Classify a new day with Wind = Strong, Temperature = 25 using the Naive Bayes classifier, and give the normalized posterior of the predicted class. Gaussian density: p(x) = 1/√(2πσ²) · exp(−(x − μ)²/(2σ²)).
A9 · 查看答案与解析
答案:判为 No,P(No | x) ≈ 0.6514(P(Yes | x) ≈ 0.3486)。
- 先验:P(Yes) = 0.6,P(No) = 0.4。
- 类别特征:P(Strong | Yes) = 2/6 ≈ 0.3333,P(Strong | No) = 3/4 = 0.75。
- 高斯特征:Yes 1/√(8π) ≈ 0.1995,exp(−9/8) ≈ 0.3247,p(25 | Yes) ≈ 0.0648;No 1/√(18π) ≈ 0.1330,exp(−9/18) ≈ 0.6065,p(25 | No) ≈ 0.0807。
- 先验 × 连乘:Yes ≈ 0.0130,No ≈ 0.0242 → 判 No。
- 归一化:0.0242/(0.0130 + 0.0242) ≈ 0.6514。
English: Priors: P(Yes) = 0.6, P(No) = 0.4. Likelihoods: P(Strong | Yes) = 2/6, P(Strong | No) = 3/4; p(25 | Yes) = 0.0648 and p(25 | No) = 0.0807 from the Gaussians. Under the conditional-independence assumption, the unnormalized scores are 0.6 × 0.3333 × 0.0648 = 0.0130 for Yes and 0.4 × 0.75 × 0.0807 = 0.0242 for No, so the prediction is No. Normalizing gives P(No | x) = 0.6514.
易错点:类别特征的分母是该类样本数(6 或 4),不是 10;题面给的是方差 σ²;只判类可省归一化,要后验概率时分母 p(x) 不能漏。
A10 · Bayes’ rule with the law of total probability
A disease affects 2% of a population. A test is positive for 90% of people who have the disease and for 5% of people who do not. A randomly chosen person tests positive. What is the probability that the person has the disease?
A10 · 查看答案与解析
答案:P(D | +) ≈ 0.2687。
- P(D) = 0.02,P(+ | D) = 0.9,P(+ | not D) = 0.05。
- 全概率:P(+) = 0.018 + 0.049 = 0.067。
- 贝叶斯:0.018/0.067 ≈ 0.2687。
English: By the law of total probability, P(+) = P(+|D)P(D) + P(+|¬D)P(¬D) = 0.9 × 0.02 + 0.05 × 0.98 = 0.067. By Bayes’ rule, P(D|+) = 0.018/0.067 = 0.2687. Although the test is fairly accurate, most positives come from the much larger healthy group.
易错点:分母要用全概率把两种来源都加上;P(+ | D) 和 P(D | +) 是两回事。
回查:Practical 1 贝叶斯法则与全概率展开(P01b);L03 p.9 生成式模型测试时用贝叶斯法则(L03b)。
个人整理的学习笔记,不是官方材料;数字与结论以课件和讲师为准。