MODELCATALOG

What to do about the blanks.

sklearn.impute

impute

What to do about the blanks.

1 Get data Something to practise on
2 Split & tune An honest test set
3 Prepare Into numbers a model accepts
4 Compose One object, no leakage
5 Model The algorithm itself
6 Unsupervised No labels available
7 Measure Is it any good, and why
8 Utilities Glue and extension

What it is for

Filling missing values. Worth pausing on the choice, because in financial data the fact that a field is blank is very often signal in its own right — an unanswered question on an application form means something different from a zero.

Contents

SimpleImputer common Mean, median, most frequent or constant. Start here
KNNImputer Fill from similar rows
IterativeImputer Model each column from the others. Powerful, slow, needs the experimental import
MissingIndicator bank Adds a boolean 'was missing' column. Often the missingness is the signal

Worked examples 2

Runnable, with the arguments that change the outcome called out. Output shapes are shown as comments where they help.

Impute, and keep the fact that it was missing

In application data, a blank field usually means something. Throwing that signal away to fill the value is a real loss.

from sklearn.impute import SimpleImputer
import numpy as np

X = np.array([[45_000, 3.0],
              [np.nan,  1.0],
              [62_000,  np.nan],
              [51_000,  2.0]])

# add_indicator appends a boolean column per feature that had NaNs
imp = SimpleImputer(strategy="median", add_indicator=True)
out = imp.fit_transform(X)

print(out)
# [[4.5e4 3.0 0.0 0.0]
#  [5.1e4 1.0 1.0 0.0]     <- income was missing
#  [6.2e4 2.5 0.0 1.0]     <- dependants was missing
#  [5.1e4 2.0 0.0 0.0]]

# The model can now learn "applications with no stated income behave
# differently", which is often more predictive than the imputed value.

When you can skip imputation entirely

HistGradientBoosting handles NaN natively — it learns which side of each split missing values should go. Often better than any imputation you would choose.

from sklearn.ensemble import HistGradientBoostingClassifier
import numpy as np

X = np.array([[45_000, 3.0], [np.nan, 1.0], [62_000, np.nan], [51_000, 2.0]])
y = np.array([0, 1, 0, 1])

clf = HistGradientBoostingClassifier(max_iter=50)
clf.fit(X, y)          # no imputer, no error
print(clf.predict(X))

# Also true of XGBoost and LightGBM. NOT true of LogisticRegression,
# SVC, KNeighbors or RandomForest, all of which raise on NaN.

Worth knowing

  1. HistGradientBoosting handles missing values natively — no imputation step needed at all.