Skip to content

Handling Missing Values and Outliers ​

This page covers, within the preprocessing that cleans data before analysis, how to deal with blanks (missing values) and values that stick out (outliers).

Missing Values and Group-wise Imputation ​

A missing value is an empty value. The ways to handle it are deleting it (deletion), filling it with another value (imputation), or leaving it as is. Filling with the overall mean is simple, but the same value goes into rows of very different kinds. Group-wise imputation groups similar rows together and fills blanks with that group's mean, median, or mode, putting in a more plausible value.

But what the blank means comes first. Identifying information like a customer number is not filled, because filling it creates customers that don't exist.

In A1-1, 4,019 of the 4,382 blank product names were filled with the mode for the same product code, and blank customer numbers (22.8% of all rows) were excluded only from the RFM calculation.

IQR Outlier Detection ​

The IQR (interquartile range) is Q3 - Q1, that is, the width containing the middle 50% of the data. A fence is set 1.5 times that width outside it, and values outside the fence are treated as outliers.

Lower fence = Q1 - 1.5 × IQR
Upper fence = Q3 + 1.5 × IQR

Because it uses quartiles instead of the mean and standard deviation, extreme values can't shake the criterion itself.

In A1-1, the upper fence for quantity was 28.5, and 5.3% of all rows were outliers.

Clipping (Limiting the Range) ​

Instead of deleting outliers, this method presses them down to the fence value. The number of rows stays the same, and only the tails of the distribution are cut off. It is used when an outlier may be a real large order rather than an input error.

In A1-1, separate *_clipped columns were created with clip(). The mean quantity dropped from 11.12 to 7.07, while the median of 4 stayed the same. The original columns were kept and used for the RFM monetary calculation.

Interview Questions ​

Q1. How did you handle missing values, and why didn't you fill them with the mean? ​

This checks whether you first considered what a blank means instead of filling it mechanically.

Each column was handled differently. Blank product names were filled with the name used most often for the same product code. Filling with the overall mode would put in unrelated product names, so they were grouped by product code before filling. Blank customer numbers (22.8%) were not filled, because they are identifying information that tells you whose order it is. Filling them with arbitrary values would create customers that don't exist, so they were excluded only from the RFM calculation.

Q2. Why did you only limit the range of outliers instead of deleting them? ​

This asks whether you judged based on the data's context instead of applying the IQR rule as is.

By the IQR 1.5x rule, 5.3% of quantities were outliers. That is too many to call input errors, and they could be bulk orders from wholesale customers, so they were not deleted. Instead, values pressed down to the boundary were stored separately in *_clipped columns and used for charts and statistics, while the original values were kept. The RFM purchase amount needed to distinguish big customers, so it was calculated with the original amounts.

An answer like "Values outside the fence are outliers, so I removed them" is risky. In this data, those values may belong to your most important customers.

Source: dubu-alt/A1-1 (Codyssey AI Advanced A1-1 mission)