Filling in missing values in Python
SimpleImputer replaces gaps with a statistic learned from the training data.
import numpy as np
from sklearn.impute import SimpleImputer
train = np.array([[1.0, 10.0], [3.0, np.nan], [5.0, 30.0]])
test = np.array([[np.nan, 20.0]])
imputer = SimpleImputer(strategy="median")
filled = imputer.fit_transform(train)
print(imputer.statistics_)
print(filled)
print(imputer.transform(test))
How it works
strategypicks the mean, median or most frequent value.- It learns the fill value in
fit, so the test set gets the same one. statistics_shows what it will substitute per column.
Keywords and builtins used here
asprint
The run, in numbers
- Lines
- 12
- Characters to type
- 309
- Tokens
- 91
- Three-star pace
- 105 tpm
At the three-star pace of 105 tokens a minute, this run takes about 52 seconds.
Step 2 of 4 in Preprocessing, step 5 of 25 in Machine learning with scikit-learn.