Data Preparation for Machine Learning
Filling a missing value, encoding a category, and clipping an extreme observation change the inputs a model receives. This article follows those choices through small experiments: what information each transformation retains, what assumptions it introduces, and how to evaluate it without using held-out data to fit the transformation.
Missing values are data
A missing value can mean that a measurement failed, a question was skipped, or a field was not applicable. Investigate how it was produced before choosing a replacement. In the constructed example below, the measured values are independent of the target, but positive cases are more likely to have a missing measurement.
The examples use NumPy and scikit-learn. cross_val_score(..., cv=5) reports accuracy on five held-out folds for these classifiers. A pipeline fits its imputer only on each training fold; add_indicator=True appends a missingness column for features with missing values during that fit. The original input has one column, so the flagged version has two here.
import numpy as np
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.dummy import DummyClassifier
rng = np.random.default_rng(0)
n = 4000
y = rng.integers(0, 2, n)
feature = rng.normal(size=n)
missing = rng.random(n) < np.where(y == 1, 0.55, 0.10)
observed = feature.copy()
observed[missing] = np.nan
X = observed.reshape(-1, 1)
for flag in (False, True):
model = make_pipeline(SimpleImputer(strategy="mean", add_indicator=flag),
LogisticRegression())
name = "impute + missing flag" if flag else "impute only"
print(f"{name:24s} {cross_val_score(model, X, y, cv=5).mean():.4f}")
print(f"majority baseline {cross_val_score(DummyClassifier(strategy='most_frequent'), X, y, cv=5).mean():.4f}")
print(f"missing rate: positives {missing[y == 1].mean():.3f}"
f" negatives {missing[y == 0].mean():.3f}")
# impute only 0.5485
# impute + missing flag 0.7213
# majority baseline 0.5072
# missing rate: positives 0.540 negatives 0.092
Accuracy is 0.5485 with imputation alone and 0.7213 with the flag, compared with a majority baseline of 0.5072. The original feature values carry no target signal in this generating process. A missingness flag is 1 when a value is absent and 0 when it is present. Adding that column gives logistic regression a separate coefficient for missingness. Filling gaps with a constant can leave a recognizable pattern, but does not give this linear model an explicit indicator for it.
With one numeric input, logistic regression fits a linear score and a monotone probability curve. It cannot isolate an exact interior constant as a separate category. A tree might detect the concentrated imputed values through nearby thresholds. The impute-only score here should therefore not be explained as logistic regression detecting equality to the imputation constant; the printed majority baseline also gives a comparison for the sample’s class imbalance.
A missingness flag is a candidate feature to evaluate, especially when the collection process is informative. Check that the missingness is observable at prediction time and that the relationship persists in the intended setting. For example, a hospital changing when it orders a test can change the meaning of an absent result. An indicator need not improve every model, particularly one that already handles missing values.
What imputation costs
Replacing missing values by a mean puts all those rows at one point. Under missingness completely at random, replacing a fraction of one variable by its population mean reduces its variance and attenuates its Pearson correlation with a fully observed partner. Other missingness patterns need not have the same effect. The following descriptive experiment compares the original and filled arrays; it does not evaluate a fitted predictor.
import numpy as np
rng = np.random.default_rng(1)
z = rng.normal(50, 10, 5000)
partner = 0.8 * z + 0.6 * rng.normal(0, 10, 5000)
dropped = rng.random(5000) < 0.30
with_gaps = z.copy(); with_gaps[dropped] = np.nan
filled = np.where(np.isnan(with_gaps), np.nanmean(with_gaps), with_gaps)
print(f"sd original {z.std():.4f} observed only {np.nanstd(with_gaps):.4f}"
f" mean-imputed {filled.std():.4f}")
print(f"corr complete rows {np.corrcoef(z[~dropped], partner[~dropped])[0, 1]:.4f}"
f" mean-imputed {np.corrcoef(filled, partner)[0, 1]:.4f}")
# sd original 10.0064 observed only 10.1205 mean-imputed 8.4030
# corr complete rows 0.7972 mean-imputed 0.6687
Here each value is removed independently with probability 0.30: missing completely at random (MCAR). The standard deviation falls from 10.01 to 8.40 and the correlation from 0.80 to 0.67. With an observation probability \(r\) and population-mean replacement, variance becomes \(r\operatorname{Var}(Z)\), and correlation with a fully observed partner is multiplied by \(\sqrt r\). For \(r=0.70\), that factor is about 0.837. The code uses a finite sample and its observed mean, so its values need not match that population calculation exactly. Treating filled values as ordinary measurements can mislead covariance analysis and uncertainty estimates.
Model-based imputation predicts a missing value from other observed columns. It can use relationships that a column mean ignores, but a deterministic prediction still does not restore the full missing-value uncertainty. Multiple imputation uses several plausible completed datasets and appropriate pooling to represent that uncertainty under assumptions about missingness and the imputation model; it is not automatically correct for every problem. For prediction, compare practical alternatives inside the training procedure. Some tree implementations, including histogram gradient boosting, can learn how to route missing values directly. Native handling also needs validation against the deployment setting.
Categorical encoding
One-hot encoding gives each category its own indicator column. With categories a, b, and c, the value b becomes [0, 1, 0]. This avoids assigning numerical distances between category labels, but many levels can produce a large feature matrix and leave little training data for rare categories.
A held-out or future case may contain a category that was absent when the encoder was fitted. The next example makes that case explicit.
import numpy as np
from sklearn.preprocessing import OneHotEncoder
train = np.array([["a"], ["b"], ["c"], ["a"], ["b"]])
test = np.array([["a"], ["d"]]) # "d" was never seen
for handle in ("error", "ignore"):
enc = OneHotEncoder(handle_unknown=handle, sparse_output=False).fit(train)
try:
print(f"handle_unknown={handle:7s} -> {enc.transform(test).tolist()}")
except ValueError as e:
print(f"handle_unknown={handle:7s} -> ValueError: {str(e)[:52]}")
# handle_unknown=error -> ValueError: Found unknown categories ['d'] in column 0 during tr
# handle_unknown=ignore -> [[1.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
Raising an error is useful when an unseen category signals malformed input. With handle_unknown="ignore", an unknown category contributes zeros to this field’s encoded columns; a linear model then uses its intercept and other features. That fallback has no category-specific estimate learned from training. Missing values are a separate case: OneHotEncoder can learn a category for NaN when it appears during fitting. If it is unseen, the unknown-category policy applies. With a dropped reference category, an all-zero encoding can also coincide with that reference. Test the exact combination of missing-value handling, category dropping, and unknown-category handling you intend to use.
Fit the encoder on training rows and test its behavior on held-out levels, missing entries, and rare levels. Offline evaluation can expose these failures if its splits contain unseen categories. Fitting the encoder on the whole dataset first hides that particular test and uses information from the evaluation rows.
Scaling
Standardization uses \(z=(x-\mu)/s\), with the mean \(\mu\) and standard deviation \(s\) fitted on training rows. If those are 100 and 20, a value of 140 becomes 2. Min-max scaling maps the training extrema to a chosen range; new values can fall outside it unless clipped. Robust scaling centers by the median and divides by the interquartile range (75th minus 25th percentile). Its fitted scale is less sensitive to extremes, but it does not remove them. The choice depends on the model, the units, and which differences should count as large.
- Distance-based methods โ k-NN, k-means, SVM with an RBF kernel โ are sensitive to feature scales. A large numeric scale can dominate distances. Standardization is useful when the units should not determine feature weights, but domain-specific weights or already comparable units can justify another choice.
- Regularized linear models are sensitive to units because a common coefficient penalty changes its effect when a feature is rescaled. Standardization is one way to make that choice explicit; it is not mathematically required, and different feature penalties may be intentional.
- Gradient-based optimization often benefits from better conditioning, as explained in Optimization for Machine Learning. Scaling alone does not resolve every source of poor conditioning.
- Tree-based models usually do not need standardization. Positive affine rescaling preserves the candidate training partitions of an ideal axis-aligned threshold tree. Rounding, approximate split searches, and ties can affect implementations; a transformation that merges distinct values can also change the available splits.
Fit data-dependent transformations on training rows and reuse them on evaluation rows and future inputs. Within cross-validation, fit them separately inside each training fold. A Pipeline combines preprocessing with the estimator so that cross-validation fits both in the right place. This applies to imputation and encoding as well as scaling; Train, Validation, Test, and Data Leakage explains the evaluation design.
The same question on real data
The Adult census dataset lets us inspect missingness without designing its relationship to the target. The loader below may download data on its first run. Missing entries can arrive as NaN or as a literal question mark depending on the loading path, so the code checks both. This is a descriptive audit of the dataset, not a test of predictive improvement from adding a flag.
import numpy as np
from fairlearn.datasets import fetch_adult
d = fetch_adult(as_frame=True)
X = d.data.copy()
y = (d.target == ">50K").astype(int)
miss = X.isin(["?"]) | X.isna()
cols = miss.sum()[lambda s: s > 0].sort_values(ascending=False)
print(f"rows {len(X):,} columns {X.shape[1]}")
for c, n in cols.items():
print(f" {c:16s} missing {n:5,} ({n / len(X) * 100:5.2f}%)")
anymiss = miss.any(axis=1)
print(f"rows with any missing value: {anymiss.sum():,} ({anymiss.mean() * 100:.2f}%)")
print(f">50K rate, complete rows {y[~anymiss].mean():.4f}"
f" rows with missing {y[anymiss].mean():.4f}")
# rows 48,842 columns 14
# occupation missing 2,809 ( 5.75%)
# workclass missing 2,799 ( 5.73%)
# native-country missing 857 ( 1.75%)
# rows with any missing value: 3,620 (7.41%)
# >50K rate, complete rows 0.2478 rows with missing 0.1323
Missing entries occur in three columns and affect 7.41% of rows. The observed rate of income above the threshold is 24.78% among complete rows and 13.23% among rows with at least one missing entry. This shows an association between completeness and the target in this dataset. It does not compare the predictive value of missingness with that of the other features.
Deleting all affected rows changes the population represented in training: incomplete cases have a different observed outcome rate. If deployment includes them, a model trained only on complete cases needs evaluation on that intended population. An imputer with indicators or a model with native missing-value handling can retain those rows, but its performance must be measured. Also, dropna() removes actual missing entries; a literal ? must first be converted to a missing marker or handled by an explicit mask.
References
- scikit-learn documentation. Common pitfalls and recommended practices: data leakage
- scikit-learn documentation. Preprocessing data: encoding categorical features
Discover more from Insightful Data Lab
Subscribe to get the latest posts sent to your email.
