How to Fill Missing Data with sklearn’s SimpleImputer and KNNImputer
This guide explains how to use scikit-learn’s SimpleImputer and KNNImputer to fill missing values, covering available strategies such as mean, median, most frequent, and constant, and provides complete Python code examples with expected output.
scikit-learn provides several estimators for missing‑value imputation, such as SimpleImputer, IterativeImputer, and KNNImputer.
Univariate Imputation with SimpleImputer
It performs univariate imputation using simple strategies, replacing missing values with column‑wise descriptive statistics (e.g., mean, median, most frequent) or a constant.
Example
<code>import numpy as np
from sklearn.impute import SimpleImputer
imp_mean = SimpleImputer(missing_values=np.nan, strategy='mean')
imp_mean.fit([[7, 2, 3], [4, np.nan, 6], [10, 5, 9]])
X = [[np.nan, 2, 3], [4, np.nan, 6], [10, np.nan, 9]]
print(imp_mean.transform(X))
</code>The result is
<code>[[ 7. 2. 3.]
[ 4. 3.5 6.]
[10. 3.5 9.]]
</code>The available imputation strategies include
mean
median
most_frequent
constant
K‑Nearest Neighbors Imputation with KNNImputer
It uses a K‑nearest neighbors model for imputation.
<code>import numpy as np
from sklearn.impute import KNNImputer
X = [[1, 2, np.nan], [3, 4, 3], [np.nan, 6, 5], [8, 8, 7]]
imputer = KNNImputer(n_neighbors=2)
imputer.fit_transform(X)
</code>The result is:
<code>array([[1. , 2. , 4. ],
[3. , 4. , 3. ],
[5.5, 6. , 5. ],
[8. , 8. , 7. ]])
</code>References
https://scikit-learn.org/stable/modules/generated/sklearn.impute.SimpleImputer.html#sklearn.impute.SimpleImputer
Model Perspective
Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.