Day 18: scikit-learn workflow: pipelines, preprocessing, cross-validation
scikit-learn: the fit/predict contract
Every scikit-learn model follows the same two-method contract: .fit(X, y) learns from training data, .predict(X) produces predictions. X is a 2D array (rows = examples, columns = features), y is the labels. Once you internalize fit/predict/transform, the entire library — and most of PyTorch's higher-level shape — becomes predictable.
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LogisticRegression()
model.fit(X_train, y_train) # learn
predictions = model.predict(X_test) # infer
model.score(X_test, y_test) # accuracy on held-out dataPipelines: preprocessing without leakage
Yesterday's data-leakage warning has a concrete fix: a Pipeline chains preprocessing steps (scaling, encoding) and the model into one object. When you call .fit, each preprocessing step learns its parameters (e.g. the scaler's mean) from *training data only*, and applies them consistently at predict time. This makes leakage structurally hard instead of a discipline you have to remember.
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
pipe = Pipeline([
("scale", StandardScaler()), # learns mean/std on train only
("model", RandomForestClassifier()),
])
pipe.fit(X_train, y_train) # scaler fit on train, then model fit on scaled train
pipe.predict(X_test) # test data scaled with TRAIN statistics — no leakageCross-validation
A single train/val split is noisy — you might get lucky or unlucky. k-fold cross-validation splits the data into k parts, trains k times (each time holding out a different part as validation), and averages the scores. It gives a more stable estimate of performance, at k× the compute. Use it whenever a dataset is small enough that one split's luck matters.
Key terms
- fit / predict
- scikit-learn's universal contract: fit learns parameters from data, predict applies them to new data.
- Pipeline
- An object chaining preprocessing and a model so preprocessing is learned on training data only — preventing leakage.
- k-fold cross-validation
- Splitting data into k folds and averaging performance across k train/validate rounds for a more stable estimate.
Why does wrapping a StandardScaler and a model in a single Pipeline help prevent data leakage?