Data Collection & Pre-processing MCQs for the NSCT
Data collection and pre-processing is the work of gathering raw data and turning it into a clean, numeric, consistently scaled table that a model can learn from: handling missing values, scaling features, encoding categories and dealing with outliers. It is the fourth of the 17 topics in the AI / Machine Learning and Data Analytics area, which carries 10% of the NSCT according to HEC's Areas of Competencies.
Expect quick calculations (a min-max value, a z-score, an imputed mean) and scenario questions about data leakage. Pandas syntax from Python for AI & Data Analytics shows up here too.
Data collection and splitting
Primary data is collected by you for your purpose, such as your FYP survey; secondary data was collected by someone else, such as a public dataset. Han and Kamber group the later work into cleaning, integration (merging sources), reduction and transformation.
Before any fitting, split the data. A stratified split keeps class proportions equal in both sets: with 2,000 rows, 5% positive and a 20% test set, the test set gets 400 rows including 20 positives. Time-ordered data needs a chronological split instead of a random one.
Exam angle: every statistic used for pre-processing (mean, min, max, category list) must be learnt from the training set only. Fitting on all the data is data leakage.
Missing values
Rubin's three mechanisms decide what is safe:
| Mechanism | Missingness depends on | Example |
|---|---|---|
| MCAR (completely at random) | Nothing | Sensor packets lost at random |
| MAR (at random) | Other observed columns | Older respondents skip an online question |
| MNAR (not at random) | The missing value itself | High earners hide their income |
Options are deletion (rows or columns), simple imputation (mean, median, mode), model-based imputation (KNN or regression) and adding a "was missing" indicator column. For [5, NaN, 9, 10], mean imputation fills 8 and median imputation fills 9.
Exam angle: mean imputation keeps the mean but shrinks the variance, because every filled value has zero deviation.
Scaling
| Method | Formula | Result | Outlier-sensitive? |
|---|---|---|---|
| Min-max normalisation | (x - min) / (max - min) | 0 to 1 on training data | Yes |
| Standardisation (z-score) | (x - mean) / sd | Mean 0, sd 1 | Somewhat |
| Robust scaling | (x - median) / IQR | Centred on median | No |
| Decimal scaling | x / 10^j | All |x| below 1 | Yes |
Example: with training min 0 and max 200, the value 50 becomes 0.25. A test value of 250 becomes 1.25, because the scaler does not clip.
Exam angle: distance-based and gradient-based models (KNN, SVM, k-means, neural networks) need scaling; decision trees and random forests do not.
Encoding
- One-hot encoding makes one 0/1 column per category. Use it for nominal data such as city.
- Ordinal (label) encoding maps categories to 0, 1, 2. Use it only when the order is real, such as Matric < Intermediate < Bachelor's.
- Dummy variable trap: with an intercept, keep k - 1 dummies, because all k sum to 1 and are perfectly collinear.
- High cardinality (thousands of postal codes) makes one-hot wide and sparse. Frequency or target encoding are alternatives, but target encoding must be fitted inside cross-validation to avoid leakage.
Exam angle: label-encoding a nominal column for a linear model invents an order, so the model treats blue = 2 as "twice" green = 1.
Outliers
Two standard detection rules:
- IQR rule: flag values below Q1 - 1.5 x IQR or above Q3 + 1.5 x IQR. With Q1 = 10 and Q3 = 30, the fences are -20 and 60.
- z-score rule: flag |z| > 3 (some texts use 2.5).
After detection you can remove, cap (winsorise), transform (log) or keep the value. An outlier is not automatically an error: in fraud detection the extreme rows are often the very cases you want.
Common traps
- Scaling or imputing before the train/test split. Split first, then fit on training data.
- Dividing by the full row count when computing a mean that has NaNs. Only observed values count.
- Oversampling (or SMOTE) before splitting, which copies minority rows into the test set.
- Assuming min-max output always lies in 0 to 1. Unseen test values can fall outside it.
- Deleting rows whose missingness is tied to the label. It biases the training set.
- Using ordinal codes for nominal categories in linear or distance-based models.
Worked examples
1. Understanding. A hospital dataset has blank 'pain score' entries for patients who were unconscious on arrival, and a separate, complete column records consciousness. Which mechanism is this? Answer: MAR. Whether the value is missing depends on another observed column (consciousness), not on the pain score itself. Imputation that uses that column, such as model-based imputation, handles it better than plain deletion.
2. Application. A feature has mean 120 and standard deviation 15. What is the z-score of 90? Answer: (90 - 120) / 15 = -2, two standard deviations below the mean.
3. Analysis. A team one-hot encodes 'city' on the full dataset, finds a city that appears only in the test rows, then splits. Is anything wrong? Answer: yes. The encoder learnt its category list from test data, a mild form of leakage. Fit the encoder on training data and set it to ignore unknown categories at prediction time, so a new city maps to all zeros.
Practise
The quiz below has 100 questions on this topic, filterable by tier and difficulty.
Practise: 100 questions
FAQs
Should I normalise or standardise my data?
Use min-max normalisation when a model expects a bounded range, such as pixel values for a neural network, and there are few outliers. Use standardisation for most linear models, SVMs and PCA, especially when features look roughly bell-shaped. With strong outliers, a robust scaler based on the median and IQR is safer than either.
Is it better to drop or impute missing values?
Drop rows only when very few are missing and the data is plausibly MCAR. Drop a column when most of it is missing and it adds little. Otherwise impute: median for skewed numbers, mode for categories, or KNN and model-based imputation when columns are related. Adding a missing-indicator column keeps any signal in the missingness itself.
What is data leakage in pre-processing?
Leakage happens when information that would not be available at prediction time reaches the model during training. Common causes are fitting scalers, imputers or encoders on the whole dataset, resampling before the split, and features recorded after the outcome, such as a debt collection date when predicting default. Leakage makes test scores look better than real performance.
Next step
Continue with Exploratory Data Analysis (EDA), the next topic in HEC's order. You can also return to the AI & ML MCQs hub or test yourself in an NSCT mock test.