A4.2.1 · Data preprocessing (HL only)
The Significance of Data Cleaning
A machine learning model can only ever be as good as the data it learns from. Before any training happens, that data almost always needs real work done to it first, and skipping that work is one of the most common ways a model quietly ends up wrong.
Explainer
There's an old computing proverb for this: garbage in, garbage out. A model trained on flawed data doesn't just produce slightly worse results, it can actively learn the wrong patterns, and no amount of clever algorithm design afterward can fully undo that.
Data cleaning is the process of finding and correcting problems in a data set, outliers, duplicates, errors, irrelevant entries, inconsistent formatting and missing values, before that data is used to train a model. Clean data produces a model that's more accurate on data it has already seen, and more importantly, generalises better to genuinely new data it hasn't.
Worked example
Six cleaning techniques
Each technique tackles a genuinely different kind of problem. Click each one to see how it's detected and fixed, with a fresh example.
Try it yourself
Detecting an outlier with numpy
Real data cleaning is usually done in code, not by eye. This runs the same Z-score method described above on a small screen-time data set, one entry, a typo, sticks out clearly once the numbers are actually calculated.
Running in offline fallback mode. This example needs numpy, which only the full Python engine (Pyodide) provides, so it isn't available here. If Pyodide can't load, usually a blocked school network or no internet connection, pressing Run will show a clear message explaining why, rather than a confusing crash.
The Z-score for the mistyped "400" comes out well above the threshold of 3, exactly the kind of number that gets a value flagged for a human to check, without anyone having to spot it by eye first.
Explainer
Normalization vs standardization
Once the data is clean, one more preprocessing step usually matters: putting every feature on a comparable scale, since a feature measured in the thousands can otherwise dominate a model over one measured in single digits, regardless of which one is actually more important.
Normalization rescales a feature into a fixed range, typically 0 to 1, so every feature contributes on the same footing regardless of its original units.
Standardization rescales a feature around its own average, so the result centres on zero with a spread of exactly one standard deviation, which suits data with outliers or no fixed natural minimum and maximum better than squeezing it into a fixed range would.
Try it yourself
Normalizing and standardizing with numpy
The same five numbers, transformed two different ways. Notice normalization always lands between 0 and 1, while standardization spreads out around zero, some values negative, some positive.
Running in offline fallback mode. This example needs numpy, which only the full Python engine (Pyodide) provides, so it isn't available here. If Pyodide can't load, usually a blocked school network or no internet connection, pressing Run will show a clear message explaining why, rather than a confusing crash.
Try changing data to include a genuine outlier (like the previous example's 400) and re-running: normalization squashes every other value into a tiny sliver near 0, while standardization keeps the ordinary values much more spread out and readable, exactly why standardization suits data with outliers better.
Check your understanding
A data set has one feature measured in millimetres (values from 0 to 5,000) and another measured as a percentage (values from 0 to 100). Before training, which preprocessing step directly addresses this mismatch in scale?
Common mistake
RecurringA genuinely documented pattern: candidates either skip normalization and standardization entirely when describing preprocessing, or use the two terms interchangeably as if they were the same operation. They solve related but genuinely different problems, and picking the wrong one, or applying it inconsistently between training and later data, can quietly hurt a model's performance.
Treats them as the same step: "The data was normalized to standardize its scale." Uses both words as if they described one identical operation, missing that a fixed 0-to-1 range and a mean-0/standard-deviation-1 distribution are two different transformations suited to different situations.
Names the actual difference: "The data was normalized to rescale it into a fixed 0-to-1 range, since the algorithm being used is sensitive to feature scale; a data set with major outliers would have used standardization instead, since squeezing an outlier into a fixed range distorts everything else." Correctly treats them as two distinct tools chosen for different reasons.
Revision questions
Past-paper style. Write an answer, then reveal the marking points and mark yourself.
[2]Define the term outlier, and state one statistical method used to detect one.
Marking points
评分要点
- An outlier is a data point that lies far outside the normal range of the other values.
- It can be detected statistically, for example a value more than three standard deviations from the mean, or outside 1.5 times the interquartile range.
- 离群值是一个远在其他数值正常范围之外的数据点。
- 它可以用统计方法检测,例如距均值超过三个标准差的值,或在四分位距 1.5 倍之外的值。
[3]Distinguish between normalization and standardization, referring to when each is more appropriate.
Marking points
评分要点
- Normalization rescales a feature to a fixed range, usually 0 to 1.
- Standardization rescales a feature to mean 0 and standard deviation 1.
- Normalization is more appropriate when the data is bounded or not normally distributed, or an algorithm expects inputs in a set range.
- Standardization is more appropriate when the data is roughly normally distributed or contains outliers, which it tolerates better.
- 归一化把特征重新缩放到固定范围,通常是 0 到 1。
- 标准化把特征重新缩放为均值 0、标准差 1。
- 当数据有界或不服从正态分布,或某算法要求输入在设定范围内时,归一化更合适。
- 当数据大致服从正态分布或含有离群值时,标准化更合适,它对离群值容忍度更好。
[6]A weather station's data set has several missing temperature readings, some clearly impossible values (like 300°C), and dates recorded in three different formats. Describe how each of these three issues should be addressed before the data is used to train a model.
Marking points
评分要点
- Missing readings: either remove those rows, or impute a replacement such as the mean, median or an interpolated value; state which and why.
- Impossible values such as 300 degrees C: treat as errors, remove them or correct them if the true value can be recovered, then handle the resulting gap as a missing value.
- Dates in three formats: parse all of them and convert to one consistent date format (or a proper date type) so they can be ordered and compared.
- All three steps are data cleaning and must happen before the data is used to train a model, so the model does not learn from errors or fail on inconsistent input.
- 缺失读数:要么删除那些行,要么填补一个替代值,如均值、中位数或插值;说明选哪种及原因。
- 不可能的值如 300 摄氏度:视为错误,删除它们,或在能恢复真实值时纠正,然后把由此产生的空缺按缺失值处理。
- 三种格式的日期:全部解析并转换为一种一致的日期格式(或正规的日期类型),使它们可以排序和比较。
- 这三步都是数据清洗,必须在数据用于训练模型之前完成,以免模型从错误中学习或在不一致的输入上失败。
Flashcards
Active recall for this statement’s key terms.
Keep this statement’s practice with you
Free account: unlock every revision question and flashcard for A4.2.1, and every other IB CS 2027 statement.