Data Preprocessing & Feature Engineering
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: . Using the IQR rule, compute , , IQR, and the upper outlier boundary . State whether is an outlier. (5 marks)
Q4. Given the dataset , apply min-max scaling to the range . Show the transformed value for each point. (4 marks)
Q5. For the same dataset , compute the z-score standardization of the value . 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 is log-transformed using the natural log. (a) Compute (leave as 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: .
- Median . Lower half , upper half .
- — 1 mark.
- — 1 mark.
- — 1 mark.
- Upper boundary — 1 mark.
- Since , by this rule 100 is NOT flagged as an outlier — 1 mark. (Why: the extreme value inflates , widening the boundary — a known quirk of small samples.)
Q4. (4 marks) — Formula , , range .
Correct formula: 1 mark; all 5 values correct: 3 marks (partial: 0.5 per pair).
Q5. (4 marks)
- Mean — 1 mark.
- Variance (population) ; — 2 marks.
- — 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) — 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"}
]