Home Knowledge Base Handling Missing Values

Handling Missing Values is a critical data preprocessing step in machine learning because most algorithms cannot process NaN/Null values — requiring practitioners to choose between deletion (removing incomplete rows or columns), imputation (filling missing values with statistical estimates like mean, median, or model-based predictions), or using algorithms that handle missingness natively (XGBoost, LightGBM), with the choice depending on whether data is missing randomly or systematically, the percentage of missingness, and the dataset size.

What Are Missing Values?

Types of Missing Data

TypeMeaningExampleImplication
MCAR (Missing Completely At Random)Missingness is unrelated to any variableA sensor randomly malfunctionsSafe to delete rows
MAR (Missing At Random)Missingness depends on observed variablesHigh-income people skip income questionsImpute using related variables
MNAR (Missing Not At Random)Missingness depends on the missing value itselfPeople with low credit scores hide their scoreHardest — "missingness" itself is a signal

Handling Strategies

StrategyMethodProsConsWhen to Use
Drop rowsDelete rows with NaNSimple, preserves feature spaceLoses data, biased if not MCAR<5% missing, large dataset
Drop columnsDelete features with many NaNReduces complexityLoses potentially useful features>50% missing in a column
Mean/MedianFill with column averageSimple, fastIgnores relationships between featuresNumeric features, MCAR
ModeFill with most frequent valueWorks for categoricalMay amplify majority classCategorical features
KNN ImputerFill using K nearest complete neighborsCaptures local patternsSlow for large datasetsMAR, moderate missingness
Iterative ImputerModel each feature as a function of othersMost accurateComputationally expensiveMAR, complex relationships
Indicator VariableAdd is_missing_feature column (0/1)Preserves missingness signalDoubles feature countMNAR (missingness is informative)

Python Implementation

from sklearn.impute import SimpleImputer, KNNImputer

# Mean imputation
mean_imp = SimpleImputer(strategy='mean')
X_filled = mean_imp.fit_transform(X)

# KNN imputation (uses neighbors)
knn_imp = KNNImputer(n_neighbors=5)
X_filled = knn_imp.fit_transform(X)

Common Mistakes

MistakeProblemFix
Imputing before train/test splitTest data leaks into imputer statisticsFit imputer on train, transform both
Using mean for skewed dataMean is pulled by outliers (salary: $50K mean but $35K median)Use median for skewed distributions
Ignoring MNAR patternsMissing values carry information you discardAdd indicator columns
One strategy for all columnsDifferent features need different approachesColumn-specific imputation strategies

Handling Missing Values is the essential first step of data preprocessing — requiring practitioners to diagnose why data is missing, choose appropriate strategies based on missingness type and severity, and implement imputation correctly within cross-validation to prevent data leakage, because the model can only be as good as the data it receives.

missing valuesimputehandle

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.