Interleaved — Phase 2

AI-ML interleaved practice

printable — key stays hidden on paper

Instructions: Solve each problem showing all work. These problems deliberately mix subtopics — read carefully and choose the correct method. Do not assume consecutive problems share a technique. Total: 62 marks. Use ...... notation for math.


1. (6 marks) A feature column has values [12,14,15,13,88,14,11][12, 14, 15, 13, 88, 14, 11]. Using the IQR rule, determine whether 8888 is an outlier. Show Q1Q_1, Q3Q_3, IQR, and the upper fence.

2. (5 marks) A dataset predicts house price. It contains columns: zip_code, bedrooms, satisfaction_rating (values: poor/average/good/excellent), and review_text. Classify each column as numerical, categorical, ordinal, or text. Justify the ambiguous one.

3. (7 marks) Given the two-point dataset (x,y)=(1,2),(3,4)(x,y) = (1,2), (3,4), derive the OLS slope β1\beta_1 and intercept β0\beta_0 using the closed-form formulas. State the fitted line.

4. (6 marks) A color column has values {red, green, blue}. Explain why label encoding is inappropriate here for a linear model, and give the one-hot representation. State how many columns one-hot produces if you drop one to avoid the dummy trap.

5. (7 marks) Standardize (z-score) the value x=20x = 20 from a feature with mean μ=15\mu = 15 and standard deviation σ=4\sigma = 4. Then min-max scale the same value given feature range [10,30][10, 30]. Show both results and state which transformation preserves the shape of the distribution.

6. (6 marks) You are asked to prevent data leakage. A colleague fits a StandardScaler on the entire dataset, then splits into train/test. Explain precisely what leaks, and give the correct ordering of operations.

7. (8 marks) For the multiple regression normal equation β^=(XTX)1XTy\hat{\beta} = (X^TX)^{-1}X^Ty, with X=[111213],y=[122]X = \begin{bmatrix} 1 & 1 \\ 1 & 2 \\ 1 & 3 \end{bmatrix}, \quad y = \begin{bmatrix} 1 \\ 2 \\ 2 \end{bmatrix} compute β^\hat\beta. Show XTXX^TX, its inverse, and XTyX^Ty.

8. (5 marks) A binary classification dataset has 950 negatives and 50 positives. Name two resampling strategies to address imbalance, and explain why plain accuracy is a misleading metric here.

9. (6 marks) A model has R2=0.85R^2 = 0.85 with n=100n = 100 samples and p=5p = 5 predictors. Compute the adjusted R2R^2. State why adjusted R2R^2 can decrease when adding a useless feature.


Answer keyMark scheme & solutions

1.Tests 2.1.3 (Outlier detection). Why: presence of one extreme value + "IQR rule" signals boxplot-fence method, not z-score.

Sorted: [11,12,13,14,14,15,88][11,12,13,14,14,15,88] (n=7n=7). Median = 1414. Lower half [11,12,13][11,12,13]Q1=12Q_1 = 12. Upper half [14,15,88][14,15,88]Q3=15Q_3 = 15. IQR=1512=3\text{IQR} = 15 - 12 = 3. Upper fence =Q3+1.5×IQR=15+4.5=19.5= Q_3 + 1.5\times\text{IQR} = 15 + 4.5 = 19.5. Since 88>19.588 > 19.5, 88 is an outlier.

2.Tests 2.1.1 (Types of data).

  • zip_code: categorical (numeric-looking but no meaningful arithmetic order).
  • bedrooms: numerical (discrete count).
  • satisfaction_rating: ordinal (ordered categories poor < average < good < excellent).
  • review_text: text. The ambiguous one is zip_code: though stored as digits, differences/means are meaningless, so treat as categorical.

3.Tests 2.2.3 (OLS derivation). Why: "derive with closed-form" → OLS slope/intercept formulas, not gradient descent.

xˉ=2\bar x = 2, yˉ=3\bar y = 3. β1=(xxˉ)(yyˉ)(xxˉ)2=(1)(1)+(1)(1)(1)2+12=22=1\beta_1 = \dfrac{\sum (x-\bar x)(y-\bar y)}{\sum (x-\bar x)^2} = \dfrac{(-1)(-1)+(1)(1)}{(-1)^2+1^2} = \dfrac{2}{2} = 1. β0=yˉβ1xˉ=312=1\beta_0 = \bar y - \beta_1\bar x = 3 - 1\cdot 2 = 1. Fitted line: y^=1+x\hat y = 1 + x.

4.Tests 2.1.6 (One-hot vs label encoding). Why: nominal unordered categories → one-hot; label encoding imposes a false order.

Label encoding red=0, green=1, blue=2 implies blue > green > red numerically, misleading a linear model. One-hot:

red green blue
1 0 0
0 1 0
0 0 1
Dropping one column to avoid the dummy-variable trap → 2 columns.

5.Tests 2.1.5 (Min-max & z-score). Why: two named scalings must be distinguished.

Z-score: z=20154=1.25z = \dfrac{20-15}{4} = 1.25. Min-max: x=20103010=1020=0.5x' = \dfrac{20-10}{30-10} = \dfrac{10}{20} = 0.5. Standardization (z-score) preserves the distribution shape (linear shift/scale); min-max also linear but bounds to [0,1][0,1]. Both are linear, but z-score is preferred when shape/outlier sensitivity matters.

6.Tests 2.1.13 (Data leakage). Why: fitting a scaler before splitting leaks test statistics.

What leaks: the scaler's mean and std are computed using test-set values, so information about the test distribution enters training. Correct order: (1) split into train/test, (2) fit scaler on train only, (3) transform train, (4) transform test with the train-fitted scaler.

7.Tests 2.2.5 (Normal equation). Why: explicit (XTX)1XTy(X^TX)^{-1}X^Ty → closed-form matrix solve.

XTX=[36614]X^TX = \begin{bmatrix} 3 & 6 \\ 6 & 14 \end{bmatrix}, det=4236=6\det = 42-36 = 6. (XTX)1=16[14663](X^TX)^{-1} = \frac{1}{6}\begin{bmatrix} 14 & -6 \\ -6 & 3 \end{bmatrix}. XTy=[1+2+21+4+6]=[511]X^Ty = \begin{bmatrix} 1+2+2 \\ 1+4+6 \end{bmatrix} = \begin{bmatrix} 5 \\ 11 \end{bmatrix}. β^=16[14663][511]=16[706630+33]=16[43]=[0.6670.5]\hat\beta = \frac{1}{6}\begin{bmatrix} 14 & -6 \\ -6 & 3 \end{bmatrix}\begin{bmatrix}5\\11\end{bmatrix} = \frac{1}{6}\begin{bmatrix} 70-66 \\ -30+33 \end{bmatrix} = \frac{1}{6}\begin{bmatrix} 4 \\ 3 \end{bmatrix} = \begin{bmatrix} 0.667 \\ 0.5 \end{bmatrix}. So β0=2/3\beta_0 = 2/3, β1=1/2\beta_1 = 1/2.

8.Tests 2.1.11 (Imbalanced data). Strategies: SMOTE (synthetic minority oversampling) and random undersampling of the majority. Accuracy misleads because predicting all-negative yields 95%95\% accuracy while catching zero positives; use precision/recall/F1/ROC-AUC instead.

9.Tests 2.2.8 (Adjusted R2R^2). Radj2=1(1R2)n1np1=10.159994=10.151.05319=10.15798=0.8420.R^2_{adj} = 1 - (1-R^2)\frac{n-1}{n-p-1} = 1 - 0.15\cdot\frac{99}{94} = 1 - 0.15\cdot 1.05319 = 1 - 0.15798 = 0.8420. Adjusted R2R^2 penalizes extra predictors: adding a useless feature raises pp, increasing n1np1\frac{n-1}{n-p-1}, which lowers adjusted R2R^2 unless the feature improves R2R^2 enough.

[
  {"claim":"IQR upper fence for problem 1 is 19.5 and 88 is an outlier","code":"data=sorted([12,14,15,13,88,14,11]); Q1=12; Q3=15; iqr=Q3-Q1; fence=Q3+1.5*iqr; result=(fence==19.5) and (88>fence)"},
  {"claim":"Normal equation beta = [2/3, 1/2]","code":"import numpy as np; X=np.array([[1,1],[1,2],[1,3]]); y=np.array([1,2,2]); b=np.linalg.inv(X.T@X)@X.T@y; result=bool(np.allclose(b,[2/3,1/2]))"},
  {"claim":"Adjusted R2 approx 0.8420","code":"R2=0.85; n=100; p=5; adj=1-(1-R2)*(n-1)/(n-p-1); result=abs(adj-0.8420)<0.001"}
]