pythonintermediate
Custom sklearn Pipeline with Transformer
Build a custom scikit-learn Pipeline with a custom BaseEstimator Transformer for data preprocessing.
pythonPress ⌘/Ctrl + Shift + C to copy
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.
typescriptadvanced
AI Prompt Chaining Pattern
Chain multiple LLM calls sequentially where each step's output feeds into the next for complex tasks.
Best for: Complex multi-step AI workflows
#prompt-engineering#chaining
typescriptbeginner
Transformer Architecture
AI/ML technique: transformer-architecture
Best for: machine learning
#ai#machine-learning
pythonbeginner
Hugging Face Transformers Pipeline
Run text classification, NER, summarisation, and translation tasks with the HF Pipelines API.
Best for: NLP tasks
#huggingface#transformers
pythonintermediate
LangChain Sequential Multi-Step Chain
Build a multi-step reasoning pipeline where each step's output feeds into the next chain.
Best for: multi-step AI pipelines
#langchain#sequential