Pipelines in Python
A pipeline chains preprocessing and a model into one estimator, so nothing leaks.
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline, make_pipeline
from sklearn.preprocessing import StandardScaler
named = Pipeline([
("scale", StandardScaler()),
("model", LogisticRegression(max_iter=200)),
])
quick = make_pipeline(StandardScaler(), LogisticRegression())
X = [[0.0, 1.0], [1.0, 0.5], [2.0, 0.0], [3.0, 0.5]]
y = [0, 0, 1, 1]
named.fit(X, y)
print(named.predict([[1.5, 0.25]]))
print(named.named_steps["scale"].mean_)
print(quick.fit(X, y).score(X, y))
How it works
- Each step is a (name, estimator) pair; the last one predicts.
fitfits every step in turn on the training data only.make_pipelinenames the steps after their classes.
Keywords and builtins used here
print
The run, in numbers
- Lines
- 18
- Characters to type
- 512
- Tokens
- 154
- Three-star pace
- 105 tpm
At the three-star pace of 105 tokens a minute, this run takes about 88 seconds.
Step 4 of 4 in Preprocessing, step 7 of 25 in Machine learning with scikit-learn.