Most scikit-learn models can't handle NaN — they'll error on missing values. Imputation fills the gaps with a sensible estimate so training can proceed.
SimpleImputer
from sklearn.impute import SimpleImputerimp = SimpleImputer(strategy="median") # or "mean", "most_frequent", "constant"X_train_i = imp.fit_transform(X_train) # learns the median on trainX_test_i = imp.transform(X_test) # applies the same median
Choosing a strategy
Key takeaways
- 01Most models can't handle NaN — impute missing values first.
- 02SimpleImputer fills with mean/median/most_frequent/constant.
- 03Use median for numeric (outlier-robust), most_frequent for categorical.
- 04The imputer learns fill values on train and applies them to test (put it in a pipeline).
