Skip to content
Machine Learning

Pipeline

Chain preprocessing and modeling with Pipeline.

By EZ4Code Team
pipelinecolumn-transformer

Code

import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

num_features = ["age", "income"]
cat_features = ["city"]

preprocess = ColumnTransformer([
    ("num", Pipeline([
        ("imputer", SimpleImputer(strategy="median")),
        ("scaler", StandardScaler()),
    ]), num_features),
    ("cat", Pipeline([
        ("imputer", SimpleImputer(strategy="most_frequent")),
        ("onehot", OneHotEncoder(handle_unknown="ignore")),
    ]), cat_features),
])

pipe = Pipeline([
    ("preprocess", preprocess),
    ("clf", RandomForestClassifier(n_estimators=100, random_state=42)),
])

# Treats the whole pipeline as a single estimator
pipe.fit(X_train, y_train)
score = pipe.score(X_test, y_test)
print(score)

Explanation

Pipeline chains transformers and a final estimator so that fit and predict apply the full sequence, preventing leakage of validation data into preprocessing. ColumnTransformer applies different preprocessing per column type, common with mixed numeric and categorical data. Treating the pipeline as one estimator makes grid search and deployment simpler.

More Machine Learning Snippets