Level 2 — RecallData Preprocessing & Feature Engineering

Data Preprocessing & Feature Engineering

30 minutes40 marksprintable — key stays hidden on paper

Level: 2 — Recall (definitions, standard textbook problems, short derivations) Time limit: 30 minutes Total marks: 40


Q1. Classify each of the following variables as numerical (continuous/discrete), categorical (nominal), or ordinal: (a) blood type, (b) exam grade "A/B/C/D", (c) number of children, (d) temperature in °C. (4 marks)

Q2. State two common strategies for handling missing values, and give one advantage of imputation over row deletion. (3 marks)

Q3. A feature has values: [12,15,14,13,100][12, 15, 14, 13, 100]. Using the IQR rule, compute Q1Q_1, Q3Q_3, IQR, and the upper outlier boundary Q3+1.5×IQRQ_3 + 1.5 \times \text{IQR}. State whether 100100 is an outlier. (5 marks)

Q4. Given the dataset x=[10,20,30,40,50]x = [10, 20, 30, 40, 50], apply min-max scaling to the range [0,1][0,1]. Show the transformed value for each point. (4 marks)

Q5. For the same dataset x=[10,20,30,40,50]x = [10, 20, 30, 40, 50], compute the z-score standardization of the value x=40x = 40. Use the population standard deviation. (4 marks)

Q6. Explain the difference between one-hot encoding and label encoding. State one situation where label encoding is inappropriate. (4 marks)

Q7. Define target encoding and state one risk associated with it. (3 marks)

Q8. A value x=100x = 100 is log-transformed using the natural log. (a) Compute ln(100)\ln(100) (leave as ln\ln expression, then approximate to 2 dp). (b) State one reason log transformations are applied to features. (3 marks)

Q9. Define data leakage and give one concrete example of how it can occur during preprocessing. (4 marks)

Q10. (a) Name the technique SMOTE and state what it does for imbalanced datasets. (b) In a train/validation/test split, state the correct order of applying a scaler (fit vs transform) to avoid leakage. (6 marks)

Answer keyMark scheme & solutions

Q1. (4 marks) — 1 mark each

  • (a) Blood type → categorical (nominal) — unordered categories.
  • (b) Grade A/B/C/D → ordinal — ordered categories.
  • (c) Number of children → numerical (discrete) — countable integers.
  • (d) Temperature °C → numerical (continuous).

Q2. (3 marks)

  • Strategy 1: Deletion (listwise/row or column deletion) — 1 mark.
  • Strategy 2: Imputation (mean/median/mode, KNN, model-based) — 1 mark.
  • Advantage: imputation retains sample size / avoids losing information that deletion discards — 1 mark.

Q3. (5 marks) Sorted data: [12,13,14,15,100][12, 13, 14, 15, 100].

  • Median =14=14. Lower half =[12,13]=[12,13], upper half =[15,100]=[15,100].
  • Q1=12+132=12.5Q_1 = \frac{12+13}{2} = 12.5 — 1 mark.
  • Q3=15+1002=57.5Q_3 = \frac{15+100}{2} = 57.5 — 1 mark.
  • IQR=57.512.5=45\text{IQR} = 57.5 - 12.5 = 45 — 1 mark.
  • Upper boundary =57.5+1.5×45=57.5+67.5=125= 57.5 + 1.5\times45 = 57.5 + 67.5 = 125 — 1 mark.
  • Since 100<125100 < 125, by this rule 100 is NOT flagged as an outlier — 1 mark. (Why: the extreme value inflates Q3Q_3, widening the boundary — a known quirk of small samples.)

Q4. (4 marks) — Formula x=xminmaxminx' = \frac{x - \min}{\max - \min}, min=10,max=50\min=10,\max=50, range =40=40.

  • 10010 \to 0
  • 200.2520 \to 0.25
  • 300.530 \to 0.5
  • 400.7540 \to 0.75
  • 50150 \to 1

Correct formula: 1 mark; all 5 values correct: 3 marks (partial: 0.5 per pair).


Q5. (4 marks)

  • Mean μ=30\mu = 30 — 1 mark.
  • Variance (population) =(202+102+0+102+202)5=10005=200=\frac{(20^2+10^2+0+10^2+20^2)}{5}=\frac{1000}{5}=200; σ=20014.142\sigma=\sqrt{200}\approx14.142 — 2 marks.
  • z=403014.142=1014.1420.707z = \frac{40-30}{14.142} = \frac{10}{14.142} \approx 0.707 — 1 mark.

Q6. (4 marks)

  • One-hot encoding: creates a binary indicator column per category; no ordinal relationship implied — 1.5 marks.
  • Label encoding: assigns each category an integer (0,1,2,…) — 1.5 marks.
  • Inappropriate for label encoding: nominal features fed to models sensitive to magnitude (e.g., linear/distance-based models), since it falsely implies order — 1 mark.

Q7. (3 marks)

  • Target encoding: replaces a category with a statistic (usually the mean) of the target variable for that category — 2 marks.
  • Risk: target leakage / overfitting (encoding uses target info; must be computed on training folds only, often with smoothing) — 1 mark.

Q8. (3 marks)

  • (a) ln(100)=ln(102)=2ln102(2.302585)=4.60524.61\ln(100) = \ln(10^2) = 2\ln 10 \approx 2(2.302585) = 4.6052 \approx 4.61 — 2 marks.
  • (b) To reduce right-skew / compress large values / stabilize variance / handle multiplicative relationships — 1 mark.

Q9. (4 marks)

  • Definition: data leakage occurs when information from outside the training set (esp. test/future/target data) influences model training, giving over-optimistic performance — 2 marks.
  • Example: fitting a scaler/imputer on the whole dataset before splitting, so test statistics leak into training (or using target-derived features improperly) — 2 marks.

Q10. (6 marks)

  • (a) SMOTE = Synthetic Minority Over-sampling Technique — 1 mark; it generates synthetic minority-class samples by interpolating between a minority point and its nearest minority neighbours — 2 marks.
  • (b) Correct order: fit the scaler on the training set only, then transform train, validation, and test using those learned parameters — 3 marks. (Never fit on val/test — prevents leakage.)

[
  {"claim":"Q3 IQR and upper boundary; 100 not an outlier",
   "code":"data=[12,13,14,15,100]; q1=(12+13)/2; q3=(15+100)/2; iqr=q3-q1; ub=q3+Rational(3,2)*iqr; result=(q1==Rational(25,2)) and (q3==Rational(115,2)) and (iqr==45) and (ub==125) and (100<ub)"},
  {"claim":"Q4 min-max scaling values",
   "code":"xs=[10,20,30,40,50]; mn=min(xs); mx=max(xs); scaled=[Rational(x-mn,mx-mn) for x in xs]; result=scaled==[Integer(0),Rational(1,4),Rational(1,2),Rational(3,4),Integer(1)]"},
  {"claim":"Q5 z-score of 40 with population sigma approx 0.707",
   "code":"xs=[10,20,30,40,50]; mu=sum(xs)/len(xs); var=sum((x-mu)**2 for x in xs)/len(xs); sigma=sqrt(var); z=(40-mu)/sigma; result=abs(float(z)-0.7071)<0.001"},
  {"claim":"Q8 ln(100) approx 4.61",
   "code":"val=log(100); result=abs(float(val)-4.6052)<0.001"}
]