pythonintermediate

Custom sklearn Pipeline with Transformer

Build a custom scikit-learn Pipeline with a custom BaseEstimator Transformer for data preprocessing.

python
import numpy as np
import pandas as pd
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression

class OutlierClipper(BaseEstimator, TransformerMixin):
    def __init__(self, lower_q: float = 0.05, upper_q: float = 0.95):
        self.lower_q = lower_q
        self.upper_q = upper_q

    def fit(self, X, y=None):
        self.lower_ = np.quantile(X, self.lower_q, axis=0)
        self.upper_ = np.quantile(X, self.upper_q, axis=0)
        return self

    def transform(self, X):
        return np.clip(X, self.lower_, self.upper_)

X = np.random.randn(200, 4)
y = (X[:, 0] + X[:, 1] > 0).astype(int)

pipe = Pipeline([('clip', OutlierClipper()), ('scale', StandardScaler()), ('clf', LogisticRegression())])
pipe.fit(X, y)
print('Pipeline accuracy:', pipe.score(X, y))

Use Cases

  • custom preprocessing
  • ML pipelines
  • feature engineering

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.