A Pipeline is a list of named steps that fit and transform in order. The last step is the model. Scaling then lives inside fit on train and predict on test — no manual transform to forget.
Goal
Wrap StandardScaler + LogisticRegression in a Pipeline and score it.
Scale then classify
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
X, y = make_classification(n_samples=200, n_features=4, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=0
)
pipe = Pipeline(
[
("scale", StandardScaler()),
("clf", LogisticRegression(max_iter=200)),
]
)
pipe.fit(X_train, y_train)
print("accuracy:", round(accuracy_score(y_test, pipe.predict(X_test)), 3))
print("steps", list(pipe.named_steps))pipe.predict scales with the train-fitted scaler, then classifies.
Named steps
from sklearn.datasets import make_classification
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
X, y = make_classification(n_samples=80, n_features=4, random_state=0)
pipe = Pipeline(
[
("scale", StandardScaler()),
("clf", LogisticRegression(max_iter=200)),
]
)
pipe.fit(X, y)
print(pipe.named_steps["clf"].coef_.round(3))Grid search (later) refers to nested parameters as clf__C.
You should see
Put ColumnTransformer in as the first step when you have city names plus numbers. Practice does that on kiosk.csv.