Ravindra BagaleCourses & study guides मराठी Track your progress

40. Data Preprocessing for Machine Learning (Missing Values, Scaling, Encoding)

40.2 Missing Values — Impute Simply

Why. Many sklearn estimators refuse NaN. You must decide: drop the row, or fill with a simple value.

What. Beginner pattern: numeric missing → fill with train-set median (robust to outliers). Category missing → fill with train-set mode (most common value) or a label like "unknown".

How.

Impute from train only

  1. Split first (Chapter 31 habit).
  2. Compute median/mode on X_train.
  3. Fill train and test with those same values.
import pandas as pd
from sklearn.model_selection import train_test_split

df = pd.DataFrame({
 "litres": [120.0, None, 90.0, 150.0, 110.0, None, 130.0, 95.0],
 "reorder": [1, 0, 0, 1, 1, 0, 1, 0],
})
X = df[["litres"]]
y = df["reorder"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)

median_litres = X_train["litres"].median()
X_train = X_train.fillna({"litres": median_litres})
X_test = X_test.fillna({"litres": median_litres})
print("median from train:", median_litres)
print(X_train, "\n", X_test)

What you should see. No NaNs. Test rows use the train median — that avoids leakage.

sklearn helper

Later you can wrap this in SimpleImputer. The idea is the same: fit on train, transform train and test.