Level 5 — MasteryData Preprocessing & Feature Engineering

Data Preprocessing & Feature Engineering

90 minutes60 marksprintable — key stays hidden on paper

Level 5 — Mastery (cross-domain: math + statistics + coding + proof) Time limit: 90 minutes Total marks: 60

Instructions: Answer all three questions. Show derivations, justify every modeling choice, and state assumptions. Code may be written in Python (NumPy/pandas/scikit-learn style pseudocode acceptable if correct).


Question 1 — Scaling, Leakage, and the Pipeline Contract (20 marks)

A dataset has a single feature xx. You split into train and test sets and apply z-score standardization.

(a) Prove that after standardization using the training statistics μtr,σtr\mu_{tr}, \sigma_{tr}, the transformed training feature has sample mean 00 and (population) variance 11. Show the algebra. (4 marks)

(b) A colleague computes μ,σ\mu, \sigma over the entire dataset (train + test combined) before splitting. Explain precisely why this constitutes data leakage, and quantify it: given training values {2,4,6,8}\{2,4,6,8\} and test values {100,102}\{100, 102\}, compute the train feature's standardized mean under the correct (train-only) procedure versus the leaky (full-data) procedure. (6 marks)

(c) Min-max scaling maps xxxminxmaxxminx \mapsto \frac{x - x_{min}}{x_{max} - x_{min}}. Prove that min-max scaling and z-score standardization are related by an affine transformation on any fixed dataset, i.e. find constants a,ba, b such that zi=ami+bz_i = a\,m_i + b where mim_i is the min-max value and ziz_i the z-score of point ii. Express a,ba, b in terms of range R=xmaxxminR = x_{max}-x_{min}, σ\sigma, and μ,xmin\mu, x_{min}. (6 marks)

(d) State one concrete scenario where min-max scaling is preferable to standardization, and one where it is dangerous. (4 marks)


Question 2 — Outliers, Transformations, and Distributional Reasoning (20 marks)

Consider positive-valued feature data x>0x > 0 that is right-skewed.

(a) For the sample {1,2,2,3,3,3,4,5,8,27}\{1, 2, 2, 3, 3, 3, 4, 5, 8, 27\}, compute Q1Q_1, Q3Q_3, the IQR, and the Tukey upper fence Q3+1.5IQRQ_3 + 1.5\cdot\text{IQR}. Identify which points are flagged as outliers. Use linear interpolation quartiles (positions 0.25(n+1)0.25(n+1) and 0.75(n+1)0.75(n+1)). (6 marks)

(b) Apply a log\log transform y=ln(x)y = \ln(x) to the value 2727 and to the median. Argue mathematically (using the derivative of ln\ln) why log transforms compress large values more than small values, and hence reduce right-skew and outlier influence. (6 marks)

(c) The Box–Cox transform is x(λ)=xλ1λx^{(\lambda)} = \frac{x^\lambda - 1}{\lambda} for λ0\lambda \neq 0. Prove that limλ0xλ1λ=lnx\lim_{\lambda \to 0} \frac{x^\lambda - 1}{\lambda} = \ln x, justifying why ln\ln is the continuous λ=0\lambda=0 case. (4 marks)

(d) Write correct pseudocode that: fits the outlier fences and log-transform parameters on the training set only, then applies them to a held-out set — and explain the leakage risk if done otherwise. (4 marks)


Question 3 — Encoding, Imbalance, and Correlation (20 marks)

A classification dataset has a categorical feature city (4 levels), a binary target y, and class imbalance (positive rate 5%).

(a) Contrast one-hot encoding vs target (mean) encoding for city. Derive the target-encoded value for a city with 3 samples having targets {1,0,1}\{1,0,1\} using additive (Laplace) smoothing with prior = global mean yˉ=0.05\bar y = 0.05 and smoothing weight m=10m=10: e^=nyˉc+myˉn+m\hat{e} = \frac{n\bar{y}_c + m\bar{y}}{n+m}. (6 marks)

(b) Explain the leakage mechanism in naive target encoding and how out-of-fold / smoothed encoding mitigates it. (4 marks)

(c) SMOTE generates a synthetic minority point as xnew=xi+δ(xnnxi)x_{new} = x_i + \delta(x_{nn} - x_i), δU(0,1)\delta \sim U(0,1). Given minority points xi=(1,2)x_i=(1,2) and nearest neighbor xnn=(3,6)x_{nn}=(3,6) with δ=0.25\delta=0.25, compute xnewx_{new}. Prove that every SMOTE point lies on the segment between the two points and state one risk of SMOTE with respect to outliers. (6 marks)

(d) Two features have Pearson correlation r=0.98r = 0.98. Compute the Variance Inflation Factor VIF=11R2\text{VIF} = \frac{1}{1-R^2} (with R2=r2R^2 = r^2 for the two-feature case) and interpret whether multicollinearity is a concern. (4 marks)


Answer keyMark scheme & solutions

Question 1

(a) [4 marks] Standardized value zi=xiμtrσtrz_i = \frac{x_i - \mu_{tr}}{\sigma_{tr}}. Mean: zˉ=1nxiμtrσtr=1σtr(1nxiμtr)=1σtr(μtrμtr)=0\bar z = \frac{1}{n}\sum \frac{x_i-\mu_{tr}}{\sigma_{tr}} = \frac{1}{\sigma_{tr}}\left(\frac{1}{n}\sum x_i - \mu_{tr}\right) = \frac{1}{\sigma_{tr}}(\mu_{tr}-\mu_{tr}) = 0. (2) Variance: 1n(zi0)2=1n(xiμtr)2σtr2=σtr2σtr2=1\frac{1}{n}\sum (z_i-0)^2 = \frac{1}{n}\sum \frac{(x_i-\mu_{tr})^2}{\sigma_{tr}^2} = \frac{\sigma_{tr}^2}{\sigma_{tr}^2}=1. (2)

(b) [6 marks] Leakage: test-set information (its mean/spread) influences the training transform; the model implicitly "sees" future data, inflating optimism and breaking the independence of the test set. (2)

Correct (train-only): μtr=2+4+6+84=5\mu_{tr}=\frac{2+4+6+8}{4}=5, σtr2=9+1+1+94=5\sigma_{tr}^2=\frac{9+1+1+9}{4}=5, σtr=52.236\sigma_{tr}=\sqrt5\approx2.236. Standardized train mean =0=0 by part (a). (2)

Leaky (full data, 6 points): μfull=2+4+6+8+100+1026=2226=37\mu_{full}=\frac{2+4+6+8+100+102}{6}=\frac{222}{6}=37. Train standardized mean =14xi37σfull=\frac{1}{4}\sum\frac{x_i-37}{\sigma_{full}}. Train raw mean =5=5, so numerator mean =537=32=5-37=-32; σfull=Varfull\sigma_{full}=\sqrt{\text{Var}_{full}}. Varfull=16(xi37)2=1225+1089+961+841+3969+42256=1231062051.67_{full}=\frac{1}{6}\sum(x_i-37)^2 = \frac{1225+1089+961+841+3969+4225}{6}=\frac{12310}{6}\approx2051.67, σfull45.30\sigma_{full}\approx45.30. Leaky train mean =3245.300.706=\frac{-32}{45.30}\approx-0.706. (2) → Correct gives 00; leaky gives 0.71\approx -0.71, showing distortion.

(c) [6 marks] mi=xixminRm_i=\frac{x_i-x_{min}}{R}, zi=xiμσz_i=\frac{x_i-\mu}{\sigma}. Solve xi=miR+xminx_i = m_i R + x_{min}; substitute: zi=miR+xminμσ=Rσmi+xminμσz_i=\frac{m_i R + x_{min}-\mu}{\sigma}=\frac{R}{\sigma}m_i + \frac{x_{min}-\mu}{\sigma}. So a=Rσa=\dfrac{R}{\sigma}, b=xminμσb=\dfrac{x_{min}-\mu}{\sigma} — an affine map. (6) (4 for form, 2 for correct constants)

(d) [4 marks] Preferable: bounded-input requirements, e.g. image pixel intensities to [0,1][0,1], or neural nets with sigmoid activations / distance metrics needing fixed range. (2) Dangerous: presence of outliers — a single extreme xmaxx_{max} compresses all other values into a tiny sub-interval, destroying resolution; standardization (or robust scaling) is safer. (2)


Question 2

(a) [6 marks] Sorted: 1,2,2,3,3,3,4,5,8,271,2,2,3,3,3,4,5,8,27, n=10n=10. Q1Q_1 position =0.25(11)=2.75=0.25(11)=2.75 → between 2nd(2) and 3rd(2): Q1=2+0.75(0)=2Q_1=2+0.75(0)=2. (2) Q3Q_3 position =0.75(11)=8.25=0.75(11)=8.25 → between 8th(5) and 9th(8): Q3=5+0.25(3)=5.75Q_3=5+0.25(3)=5.75. (2) IQR =5.752=3.75=5.75-2=3.75; upper fence =5.75+1.5(3.75)=5.75+5.625=11.375=5.75+1.5(3.75)=5.75+5.625=11.375. (1) Outlier: 27>11.37527>11.375 ⇒ flagged (only 27). (1)

(b) [6 marks] ln273.296\ln 27 \approx 3.296; median (avg of 5th,6th =3,3=3,3) =3=3, ln31.099\ln 3\approx1.099. (2) Raw gap 27 vs 3 = 24; log gap 2.197\approx 2.197 — hugely compressed. (1) Since ddxlnx=1x\frac{d}{dx}\ln x = \frac{1}{x}, the transform's slope decreases as xx grows; large xx get mapped with smaller local stretch, so wide right-tail spacing collapses, reducing skew and outlier leverage. (3)

(c) [4 marks] limλ0xλ1λ\lim_{\lambda\to0}\frac{x^\lambda-1}{\lambda}: indeterminate 0/00/0. Write xλ=eλlnxx^\lambda=e^{\lambda\ln x}. Either L'Hôpital w.r.t. λ\lambda: ddλ(xλ1)=xλlnx\frac{d}{d\lambda}(x^\lambda-1)=x^\lambda\ln x, denominator derivative =1=1, limit =x0lnx=lnx=x^0\ln x=\ln x. (4) (2 for method, 2 for result)

(d) [4 marks]

# TRAIN ONLY
q1, q3 = np.percentile(X_train, [25,75])
iqr = q3 - q1
upper = q3 + 1.5*iqr          # fences fit on train
# choose lambda / use log; store shift if needed
X_train_t = np.log(X_train + eps)
 
# APPLY to test using SAME fences/transform
X_test_clipped = np.clip(X_test, None, upper)
X_test_t = np.log(X_test_clipped + eps)

Leakage risk: fitting fences/transform params on test (or full) data lets test distribution influence preprocessing, biasing evaluation optimistically. (2 code, 2 explanation)


Question 3

(a) [6 marks] One-hot: 4 binary columns, no ordinal assumption, no target leakage, but high dimensionality/sparsity for many levels. Target encoding: single column = per-category target mean, compact and captures signal, but risks leakage/overfit on rare levels. (2) Smoothed encode: n=3n=3, yˉc=1+0+13=230.667\bar y_c=\frac{1+0+1}{3}=\frac23\approx0.667, yˉ=0.05\bar y=0.05, m=10m=10. e^=3(0.667)+10(0.05)3+10=2.0+0.513=2.5130.1923\hat e=\frac{3(0.667)+10(0.05)}{3+10}=\frac{2.0+0.5}{13}=\frac{2.5}{13}\approx0.1923. (4)

(b) [4 marks] Naive encoding uses the target of a row to build that row's feature → direct target leakage inflating training performance and overfitting rare categories. Out-of-fold encoding (compute a fold's encoding from other folds) and smoothing toward the global prior break the self-reference, shrinking unreliable small-nn estimates. (4)

(c) [6 marks] xnew=xi+δ(xnnxi)=(1,2)+0.25((3,6)(1,2))=(1,2)+0.25(2,4)=(1+0.5,2+1)=(1.5,3)x_{new}=x_i+\delta(x_{nn}-x_i)=(1,2)+0.25((3,6)-(1,2))=(1,2)+0.25(2,4)=(1+0.5,\,2+1)=(1.5,\,3). (3) Proof: xnew=(1δ)xi+δxnnx_{new}=(1-\delta)x_i+\delta x_{nn} is a convex combination with δ[0,1]\delta\in[0,1], hence lies on the line segment between the two points. (2) Risk: if xix_i or xnnx_{nn} is an outlier/noisy minority point, SMOTE interpolates into unrealistic regions, amplifying noise and blurring class boundaries. (1)

(d) [4 marks] R2=r2=0.982=0.9604R^2=r^2=0.98^2=0.9604; VIF=110.9604=10.039625.25\text{VIF}=\frac{1}{1-0.9604}=\frac{1}{0.0396}\approx25.25. (2) VIF 10\gg 10 ⇒ severe multicollinearity; the two features are nearly redundant → unstable coefficient estimates; drop one or combine (PCA/regularization). (2)

[
  {"claim":"Leaky train standardized mean approx -0.706","code":"xs=[2,4,6,8,100,102]; mu=sum(xs)/len(xs); var=sum((x-mu)**2 for x in xs)/len(xs); sig=sqrt(var); train=[2,4,6,8]; m=sum((x-mu)/sig for x in train)/len(train); result=abs(float(m)-(-0.706))<0.01"},
  {"claim":"Tukey upper fence = 11.375 and 27 is the only outlier","code":"Q1=2; Q3=Rational(575,100); iqr=Q3-Q1; fence=Q3+Rational(3,2)*iqr; data=[1,2,2,3,3,3,4,5,8,27]; outs=[x for x in data if x>fence]; result=(fence==Rational(11375,1000)) and outs==[27]"},
  {"claim":"Box-Cox limit equals ln x","code":"lam,x=symbols('lam x',positive=True); result=limit((x**lam-1)/lam,lam,0)==log(x)"},
  {"claim":"Smoothed target encoding approx 0.1923","code":"e=(3*Rational(2,3)+10*Rational(5,100))/(3+10); result=abs(float(e)-0.1923)<0.001"},
  {"claim":"VIF approx 25.25 for r=0.98","code":"r=Rational(98,100); R2=r**2; vif=1/(1-R2); result=abs(float(vif)-25.2525)<0.01"},
  {"claim":"SMOTE point is (1.5,3)","code":"xi=Matrix([1,2]); xnn=Matrix([3,6]); d=Rational(1,4); xnew=xi+d*(xnn-xi); result=xnew==Matrix([Rational(3,2),3])"}
]