Imputing in a Pipeline
Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 47 of 58.
Real logs have gaps, and most models refuse them. A SimpleImputer fills each gap with its column's mean, median or most common value.
Fill before the split, and the test rows help choose that value: a leak. Seven dive depths and one gap:
In a pipeline, fit learns the fills from training rows only:
from sklearn.impute import SimpleImputer
pipe = make_pipeline(SimpleImputer(strategy="median"), StandardScaler(), model)
pipe.fit(X_train, y_train)
print(pipe[0].statistics_) # the first step's fillsFor a dive log (depth, minutes, water temperature), this prints:
[19. 44. 21.]With words too, the numbers' steps become one lane of the ColumnTransformer:
lane = make_pipeline(SimpleImputer(strategy="median"), StandardScaler())
("numbers", lane, ["depth_m", "minutes"])Challenge
MediumA forest service predicts which lightning strikes start fires, but some readings are missing. The starter splits wildfire.csv and reads a strategy, mean or median.
- Fit a pipeline of an imputer with that strategy, a scaler and logistic regression. Print its test accuracy.
- For each column, print the pipeline's fill and what all rows of
Xwould give, 1 decimal.
Another forest, with median:
Test accuracy: 0.840
humidity_pct: fill 46.0, all rows would give 46.0
wind_kmh: fill 29.0, all rows would give 32.0
temp_c: fill 25.0, all rows would give 26.0
days_without_rain: fill 23.0, all rows would give 23.0Wherever the two differ, filling before the split would have let the test rows shape the training data.
Try it yourself
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
# Lightning strikes in a dry forest: did the strike start a fire (1) or not (0)?
# Some weather readings are missing.
strikes = pd.read_csv("wildfire.csv")
columns = ["humidity_pct", "wind_kmh", "temp_c", "days_without_rain"]
X = strikes[columns]
y = strikes["fire"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
strategy = input()
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Supervised Learning
1Your First Model
Learning from ExamplesTrain and Test SetsFit and PredictScoring a ModelRecap: Seed Sorter2Nearest Neighbors
How k-NN DecidesChoosing kDecision BoundariesScaling for NeighborsRecap: Wine Neighbors5Measuring Classifiers
The Confusion MatrixPrecision and RecallThe F1 ScoreImbalanced ClassesROC CurvesRecap: Screening Test11Project: Penguin Species
Meet the PenguinsPrepare the DataChoose a ModelTune the WinnerShip the ModelPractice on your own: Online Python compiler