Skip to main content
Albero Academy

Handling Missing Values

Fill gaps with SimpleImputer so models can train.

Preprocessing7 min

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

fill the gaps
from sklearn.impute import SimpleImputer
imp = SimpleImputer(strategy="median") # or "mean", "most_frequent", "constant"
X_train_i = imp.fit_transform(X_train) # learns the median on train
X_test_i = imp.transform(X_test) # applies the same median

Choosing a strategy

Key takeaways

  1. 01Most models can't handle NaN — impute missing values first.
  2. 02SimpleImputer fills with mean/median/most_frequent/constant.
  3. 03Use median for numeric (outlier-robust), most_frequent for categorical.
  4. 04The imputer learns fill values on train and applies them to test (put it in a pipeline).

Finished this chapter?

Mark it complete to track your progress through Scikit-learn.