Missingness and imputation
Quick answer
Imputation replaces missing input values with a defined estimate or placeholder so a downstream model can process them. It does not recover the true values. For example, a missing package weight can be replaced by the median weight observed in training; a separate indicator can record that the measurement was absent. All examples use synthetic inputs and local scikit-learn 1.8.0 where imported; ByteMind package execution is unverified.
Fit a mean and median on training values
Training weights are 2, 4 and 12 kg; one training value and one incoming value are missing. The incoming observed 100 kg must not affect the fitted statistics.
import numpy as npfrom sklearn.impute import SimpleImputertrain = [[2.], [4.], [12.], [np.nan]]new = [[np.nan], [100.]]for strategy in ['mean', 'median']: imp = SimpleImputer(strategy=strategy).fit(train) print(strategy, imp.transform(new).ravel().tolist())Expected output:
mean [6.0, 100.0]median [4.0, 100.0]Only the missing input changes; the observed 100 is preserved. The mean is 18/3 and the median is 4.
How it works
Missing completely at random (MCAR) means missingness is independent of observed and unobserved data. Missing at random (MAR) means that, conditional on observed data, missingness does not depend on the missing values. Missing not at random (MNAR) means this conditional independence fails. A random sensor outage might approximate MCAR; missing weight explained by observed warehouse may be MAR; heavy items omitted because of their unrecorded weight may be MNAR. These are modeling assumptions, not diagnoses obtained from a missingness count. Audit impossible measurements, sentinels and units before converting to NaN. A genuine zero is not missing. Mean imputation is sensitive to extremes, median is more robust, mode is a frequent category (define tie handling), and constant imputation needs a documented value. Dropping rows may distort the population. Compare approaches on validation data; evaluate final test only after selection.
Keep a missingness indicator
The second column never has missing data during fitting. Inspect the feature layout after transforming a row with both values missing.
import numpy as npfrom sklearn.impute import SimpleImputerimp = SimpleImputer(strategy='median', add_indicator=True)imp.fit([[1., 10.], [np.nan, 20.], [3., 30.]])print(imp.transform([[np.nan, np.nan]]).tolist())print(imp.indicator_.features_.tolist())Expected output:
[[2.0, 20.0, 1.0]][0]Only the first feature had missing values during fit, so only it has an appended indicator. A fixed indicator for every column can be built with MissingIndicator(features="all").
Common mistakes
Never fit on test data or replace all missing weights with zero by default. A fully missing training column needs an explicit keep/drop or constant policy; do not assume median is defined. Imputation can reduce variability and hide uncertainty. Indicators may expose process changes or proxies; monitor them and validate their utility. In scikit-learn 1.8, SimpleImputer defaults to dropping fully missing columns except as specified by keep_empty_features and strategy; pin and test the output schema.
Practice and recap
Try median fill with missingness flags. Input [train, incoming]; train is a list of finite integers or None with at least one observed value. Incoming is a list of finite integers or None. Return [filled, flags], replacing None with the training median (average of middle two when even) and flagging only None as 1. Preserve order; empty incoming returns [[], []]. Then explain the preprocessing assumptions before choosing a transformation. Next: Categorical encoding.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Missingness and imputation — knowledge check
Apply the preprocessing concept to small synthetic data and diagnose mistakes.
An observed zero kg is valid under a fixture contract. What should an imputer do?
Which assumption describes MAR?
Training values are 2, 4, 12. What median fills a missing validation value?
What can an indicator record?
Why compare strategies using validation rather than test data?