/en-tutor-prep-ib-cs-data-preprocessing-cleaning-features/
/en-tutor-prep-ib-cs-data-preprocessing-cleaning-features/
General Inquiry: info@TutorZone.com.hk
Office Hours: Monday to Saturday - 9:00 AM to 6:00 PM
WhatsApp Monday to Sunday - 24 hours
Hotline: +852 6828 1809
Instant Support: +852 9061 3106

IB Computer Science Data Preprocessing: Cleaning, Feature Selection and Dimensionality Reduction (7.8 to 4.2 error, 78 fits, curse of dimensionality)

IB Computer Science Data Preprocessing: Cleaning, Feature Selection and Dimensionality Reduction (7.8 to 4.2 error, 78 fits, curse of dimensionality)

  • 2026-11-27

Students think a weak model needs a stronger algorithm, but A4.2 asks something else: clean and scale the same 4,800 rows properly and a plain K-NN beats an uncleaned complex model. This lesson turns preprocessing into four bills you can add up.

Objectives — Students can: (1) diagnose four defects with one treatment each: 96 duplicate rows (2.0 per cent), 288 missing test scores (6.0 per cent) and 144 missing attendance values (3.0 per cent), 1,776 rows with a wrong date format (37.0 per cent), and outliers (two impossible values plus 16 above three standard deviations); (2) compare three imputations: deleting loses 6.0 per cent of rows, mean imputation errs by 7.8 marks and regression on attendance errs by 4.2 marks, which is 46.2 per cent lower; (3) separate filter, wrapper and embedded selection and cost forward selection at 78 fits against all 4,095 subsets; (4) standardise: a 10 mark gap is 0.81 standard deviations and a 0.1 attendance gap is 0.83, and unscaled the mark gap counts 100.0 times as much; (5) explain the curse of dimensionality, with the distance coefficient of variation falling from 0.476 in two dimensions to 0.059 in one hundred; (6) size the saving: 460,800 bytes fall to 188,160 bytes, 40.8 per cent.

01 Prerequisites (3-minute check)

If any of these fails, fix it before the lesson starts.

Skill Quick check Pass mark
Mean and standard deviation With a mean of 68.5 and a standard deviation of 12.4, what is the z value of 75 About 0.52
Percentages What percentage is 96 out of 4800 rows Part over whole, times one hundred
Distance Two points differ by 3 and by 4. What is the distance Five, by Pythagoras

02 Lesson flow (60 minutes)

Time Stage Tutor does Student does
0-8 min Opening check Ask what a model learns when one student appears twice Write an answer, compare at the end
8-24 min Cleaning and imputation 96 duplicates, 288 missing, 1,776 bad formats Choose a treatment per defect
24-38 min Three selection strategies 12 columns kept down to 5, 78 fits against 4,095 State the difference
38-52 min Normalisation and standardisation A 10 mark gap against a 0.1 attendance gap Compute z values and shares
52-57 min Curse and reduction The coefficient falls from 0.476 to 0.059 State the cost
57-60 min Exit check Collect and mark three items Work independently

03 Teaching script (read it out)

Scene 1: Cleaning: four defects, four treatments, one principle

Open with 4,800 rows and 12 columns of school records. The first defect is duplication: 96 rows are identical, 2.0 per cent, leaving 4,704 after de-duplication. Left in, those 96 students count twice in training and their weight doubles. The second is missing values: 288 test scores (6.0 per cent) and 144 attendance values (3.0 per cent). The third is format: 1,776 dates written three different ways (37.0 per cent), which sort October before September as text. The fourth is outliers: two impossible values (999 and minus five) plus 16 above three standard deviations, meaning above 105.7 marks, each to be checked as a typo or a genuine extreme.
Cost the three imputations. Deleting 288 rows throws away 6.0 per cent of the sample, and if the students with missing scores are mostly low scorers, the model never learns about them. Filling the mean of 68.5 is cheapest but flattens the variance and errs by 7.8 marks on average. A simple regression on attendance and homework completion predicts the missing score with an error of 4.2 marks, 46.2 per cent lower. State the principle: ask why the value is missing. If missingness relates to the answer, deletion biases the model, and imputation should add a column flagging that the value was missing.
Finish with normalisation against standardisation. Normalisation maps to zero and one: value minus minimum, over maximum minus minimum, so a 0 to 100 score becomes 0 to 1. Standardisation is value minus mean, over standard deviation, so 75 marks gives (75 minus 68.5) over 12.4, about 0.52, and the output may be negative. With extreme values, standardisation is steadier, and the median and interquartile version is steadier still.

Student task: five defect cases (duplicates, six per cent missing, three date formats, a score of 999, a score of minus five), each with one treatment and one sentence on what goes wrong if nothing is done.

Scene 2: Feature selection: keep the most informative columns, not the fewest

All three strategies answer the same question at different moments. Filter methods decide before training, scoring each column against the answer: attendance correlates at 0.62, homework completion at 0.58, test score at 0.71, while classroom sits at 0.02 and student number at 0.00, so the last two are dropped. Wrapper methods put the model inside the loop and score subsets by performance. All 4,095 subsets of 12 columns is too many, so forward selection tries 12 single columns, then 11 pairs, and so on up to 12 columns, which is 78 fits in total, 52.5 times fewer. Embedded methods decide during training: L1 regularisation, or Lasso, drives useless coefficients to zero, and here 7 of the 12 columns are zeroed, leaving 5.
Cost both sides. 4,800 rows times 12 columns times 8 bytes is 460,800 bytes; after de-duplication, 4,704 rows times 5 columns is 188,160 bytes, or 40.8 per cent. Distance computation falls from 12 dimensions to 5 across 11,517,600 pairs of rows.
A dropped column cannot be recovered, so remove the two that can never be used, identifiers and post-hoc fields such as final grade, then let the three strategies argue over the rest. The post-hoc field is the leak that makes a model look perfect in training.

Student task: for 12 named columns, say keep, drop or investigate, and identify which one is target leakage.

Scene 3: Scaling and the curse of dimensionality: when the unit is the whole game

Start with a contrast. Two features: test score (0 to 100, standard deviation 12.4) and attendance (0 to 1, standard deviation 0.12). Two students differ by 10 marks and 0.1 attendance. Unscaled, the distance is the square root of 10 squared plus 0.1 squared, which is just over 10, and the mark gap counts 100.0 times as much as the attendance gap, so attendance may as well be absent. After standardisation the mark gap is 0.81 standard deviations and the attendance gap is 0.83, and their squared contributions are 48.4 and 51.6 per cent: now both features count.
Then dimensions. Scatter 250 random points in a unit cube and measure every pairwise distance. In two dimensions the mean distance is 0.53 with a standard deviation of 0.25, a coefficient of variation of 0.476. In 12 dimensions it is 0.175, in 100 it is 0.059, and in 1,000 it is 0.019. The lower the coefficient, the closer the nearest neighbour comes to the farthest, and the less distance means anything. That is the curse of dimensionality, and it is why K-NN degrades in high dimensions.
Reduction is the countermeasure: compress 100 columns into 10 components that keep most of the variance, and distances regain contrast. The price is that components are no longer the original columns, interpretation weakens, and the projection must be recomputed for new data. The guide states explicitly that principal component analysis and linear discriminant analysis are beyond scope, so the class only needs why we reduce and what reduction costs.

Student task: three sentences on one dataset, each with a number: what happens without scaling, what scaling changes, what reduction trades away.

04 Question types and worked steps

Type Example Steps Answer
Diagnose defects and choose treatments A table with duplicates, missing values, bad formats and outliers Four steps: count affected rows, decide whether missingness relates to the answer, choose delete or impute, record before and after counts 96 duplicates (2.0 per cent), 6.0 per cent missing, 37.0 per cent bad formats
Choose a selection strategy A column count, a row count and whether retraining is affordable Drop identifiers and leaks first, then pick by cost: filter is fastest, wrapper is most accurate but expensive, embedded finishes inside one training run 78 forward fits against 4,095 subsets at 12 columns
Compute standardisation and shares Two features with a mean and a standard deviation, and the gaps for one pair of students Take each z value, square both, then compute the shares. Squaring before adding is the step students skip 0.81 and 0.83; shares of 48.4 and 51.6 per cent
Explain the curse and the cost of reduction A 12 dimension dataset and a 100 dimension dataset Distance concentration, then K-NN failure, then reduce or collect more rows, then name the cost to interpretability The coefficient falls from 0.175 to 0.059
Sanity check (teach this): Five checks. First, de-duplication must lower the row count: 4,800 minus 96 is 4,704, so a larger number means duplicates were counted as additions. Second, an improvement must stay under one hundred per cent: 7.8 falling to 4.2 is 46.2 per cent, and 185 per cent has the fraction upside down. Third, the two squared contributions must sum to one hundred: 48.4 plus 51.6 does, and anything else forgot to square. Fourth, the coefficient of variation is below one and falls with dimension: 0.476 above 0.175 above 0.059. Fifth, the reduced table must be smaller: 188,160 against 460,800 is 40.8 per cent. The commonest misjudgement is that more features always mean a better model: seven of the twelve columns correlate near zero, and they only spread the distances further, which is the classroom version of the curse.

05 Independent practice (3 questions)

Question Answer What to watch
4,800 rows include 96 exact duplicates. How many rows remain, and what percentage were duplicates? 4,704 rows, with 2.0 per cent duplicates Whether duplicates were counted as additions
Mean imputation errs by 7.8 marks and regression imputation by 4.2. What percentage better is regression? 46.2 per cent (a gap of 3.5999999999999996 over the original 7.8) Whether the original error was the denominator
A 10 mark gap (standard deviation 12.4) and a 0.1 attendance gap (standard deviation 0.12): after standardisation, what share of the squared distance does each take? 48.4 and 51.6 per cent (z values of 0.81 and 0.83) Whether both values were squared before adding

06 Common mistakes and what to say

Mistake Student writes Say this
Treating mean imputation as universal Saying fill the mean, it is fastest Filling the mean of 68.5 pulls everyone towards the middle and flattens the variance, with an error of 7.8 marks. Regressing on attendance brings it to 4.2. If missingness relates to the answer, add a flag column as well.
Running K-NN without scaling Saying both features are already numbers, so distance is fine A 10 mark gap is 10 units and a 0.1 attendance gap is 0.1 units, so the mark gap counts 100.0 times as much and attendance is effectively ignored. Standardise first, then both count about half each.
Assuming more features are always better Saying put all 12 columns in, more data is more accurate Seven of the twelve columns barely correlate with the answer and only spread the distances: the coefficient falls from 0.476 to 0.059. Five columns keep most of the signal.
Deleting missing rows without costing it Saying drop every row with a gap Dropping 288 rows loses 6.0 per cent of the sample. If missing scores cluster among low scorers, the model never sees them, so impute and flag instead.

07 Homework (paper, section and question numbers)

08 Exit check (last 5 minutes)

Question Answer Decision
Four defects Duplicates, missing values, formats, outliers Saying the data is dirty means replay act one
Three imputations Delete, mean at 7.8, regression at 4.2 No error figures means not yet
Standardisation z values of 0.81 and 0.83, about half each Dividing by the maximum means normalisation was confused with standardisation

09 Tutor FAQ

Must PCA be taught?

A4.2.3 states that principal component analysis and linear discriminant analysis are beyond scope. Teach the purpose of reduction, which is fighting the curse, saving memory and allowing visualisation, and its cost, which is that components lose the meaning of the original columns. Eigen-decomposition does not belong in this course.

When do we normalise and when do we standardise?

Both are scaling. Normalisation, or min-max, maps values into a fixed range, which suits bounded features without extremes. Standardisation, or the z score, centres on the mean and scales by the standard deviation, which suits features with outliers or algorithms that assume a normal shape. K-NN, neural networks and gradient descent prefer standardisation.

How do we choose among the three imputations?

Ask about the mechanism. Values missing completely at random may be deleted; values related to other columns call for regression or K-NN imputation; values related to the answer itself require a flag column, or the model confuses missing with low. Here 288 of 4,800 scores are missing (6.0 per cent), so deletion throws away too much and imputation wins.

Find a tutor — freeTutor Registration →

A TutorZone lesson-prep guide. Written by the TutorZone team; questions and lesson flow are original and are not taken from any school or publisher material.

Join as a tutor or coach — academic, music, sports, arts, STEM. Free to register. See how it works →