Where you would use it
Predict missing BMI from age, sex, activity and blood pressure; then predict missing blood pressure using the updated BMI and other variables.
Iterative imputation treats each incomplete variable as a prediction problem. It cycles through features, predicts one feature from the others, updates the filled values and repeats. This captures multivariate relationships that simple univariate imputation ignores.
Iterative imputation treats each incomplete variable as a prediction problem. It cycles through features, predicts one feature from the others, updates the filled values and repeats. This captures multivariate relationships that simple univariate imputation ignores.
The practical value of Iterative imputation / chained equations comes from understanding both the transformation and the boundary around it: what information is allowed to enter, what assumption is being made, and how you know the result is still valid after the transformation.
A beginner-friendly way to reason about it is to start with a tiny case where the correct result can be checked independently. Once the mechanism is clear, scale the exact same reasoning to larger tables, pipelines or models.
Initialise missing values, fit a model for one incomplete feature using the other features, replace its missing entries, move to the next feature, and iterate until the updates stabilise or a limit is reached.
The interactive view uses a concept-specific plot when the topic maps naturally to one; otherwise it uses a workflow view instead of leaving a broken placeholder.
Predict missing BMI from age, sex, activity and blood pressure; then predict missing blood pressure using the updated BMI and other variables.
A useful diagnostic question is: Could the same code still run successfully if the analytical assumption were wrong? If yes, add an explicit validation check rather than relying on execution success.
Keep the example small enough that you can inspect each stage manually.
# Purpose: demonstrate Iterative imputation / chained equations with a small, inspectable example.
# Follow the comments and printed stages to connect each operation with its result.
# Import the library or helper used in this example.
# Step 1 — Import the module so its functions/classes are available to the rest of this example.
import numpy as np
# Import the library or helper used in this example.
# Step 2 — Import the module so its functions/classes are available to the rest of this example.
import pandas as pd
# Import the library or helper used in this example.
# Step 3 — Import only the named objects needed by the following steps, keeping dependencies explicit.
from sklearn.experimental import enable_iterative_imputer # noqa: F401
# Import the library or helper used in this example.
# Step 4 — Import only the named objects needed by the following steps, keeping dependencies explicit.
from sklearn.impute import IterativeImputer
# Create a small labelled dataset that is easy to inspect by eye.
# Step 5 — Construct `X` as a tabular object with named columns for inspectable analysis.
X = pd.DataFrame({
"age": [22,25,28,31,34,37,40,43,46,49,52,55],
"income": [42,45,np.nan,52,55,59,62,np.nan,70,74,78,82],
"spend": [18,19,21,np.nan,24,25,27,29,31,33,np.nan,36]
})
# Print this intermediate result so you can verify the workflow step by step.
# Step 6 — Display the current value explicitly so the result/state can be inspected during execution.
print("STEP 1 · Missing counts:", X.isna().sum().to_dict())
# Store this intermediate value with a descriptive name for the next step.
# Step 7 — Compute the right-hand expression and store its result in `imp` for the next step.
imp = IterativeImputer(max_iter=10, random_state=7)
# Create a small labelled dataset that is easy to inspect by eye.
# Step 8 — Fit the transformation on the training input and immediately transform that same input.
Xi = pd.DataFrame(imp.fit_transform(X), columns=X.columns)
# Print this intermediate result so you can verify the workflow step by step.
# Step 9 — Display the current value explicitly so the result/state can be inspected during execution.
print("STEP 2 · Iterations used:", imp.n_iter_)
# Print this intermediate result so you can verify the workflow step by step.
# Step 10 — Display the current value explicitly so the result/state can be inspected during execution.
print("STEP 3 · Completed data (selected rows):")
# Print this intermediate result so you can verify the workflow step by step.
# Step 11 — Display the current value explicitly so the result/state can be inspected during execution.
print(Xi.loc[[2,3,7,10]].round(2).to_string(index=False))STEP 1 · Missing counts: {'age': 0, 'income': 2, 'spend': 2}
STEP 2 · Iterations used: 3
STEP 3 · Completed data (selected rows):
age income spend
28.0 48.62 21.00
31.0 52.00 22.43
43.0 66.49 29.00
52.0 78.00 34.37