Advanced
Advanced Features
Build custom estimators and transformers, persist models for production, implement multiclass strategies, and leverage powerful ensemble methods.
Custom Estimators
Create your own scikit-learn compatible estimators by inheriting from base classes:
Python
from sklearn.base import BaseEstimator, TransformerMixin class OutlierClipper(BaseEstimator, TransformerMixin): """Clip outliers beyond n standard deviations.""" def __init__(self, n_std=3): self.n_std = n_std def fit(self, X, y=None): self.mean_ = X.mean(axis=0) self.std_ = X.std(axis=0) return self def transform(self, X): lower = self.mean_ - self.n_std * self.std_ upper = self.mean_ + self.n_std * self.std_ return np.clip(X, lower, upper) # Use in a pipeline just like any sklearn transformer pipe = make_pipeline(OutlierClipper(n_std=3), StandardScaler(), Ridge())
Model Persistence
Python
import joblib # Save the entire pipeline (including preprocessing) joblib.dump(pipe, "model_pipeline.joblib") # Load and use immediately loaded_pipe = joblib.load("model_pipeline.joblib") predictions = loaded_pipe.predict(new_data) # For sklearn-specific format (newer, safer) from sklearn.utils import estimator_html_repr # Also available: skops for secure serialization
Ensemble Methods
Python
from sklearn.ensemble import VotingClassifier, StackingClassifier # Voting: combine multiple models voting = VotingClassifier(estimators=[ ("rf", RandomForestClassifier(n_estimators=100)), ("svc", SVC(probability=True)), ("lr", LogisticRegression()) ], voting="soft") # Stacking: use a meta-learner stacking = StackingClassifier(estimators=[ ("rf", RandomForestClassifier()), ("svc", SVC(probability=True)) ], final_estimator=LogisticRegression(), cv=5) stacking.fit(X_train, y_train)
Feature Selection
Python
from sklearn.feature_selection import ( SelectKBest, f_classif, RFECV ) # Statistical feature selection selector = SelectKBest(f_classif, k=10) X_selected = selector.fit_transform(X, y) # Recursive Feature Elimination with CV rfecv = RFECV(RandomForestClassifier(), step=1, cv=5, scoring="accuracy") rfecv.fit(X, y) print(f"Optimal features: {rfecv.n_features_}") print(f"Selected: {rfecv.support_}")
Multiclass Strategies
| Strategy | Description | When to Use |
|---|---|---|
| One-vs-Rest (OvR) | Train N binary classifiers | Default for most algorithms |
| One-vs-One (OvO) | Train N*(N-1)/2 classifiers | SVM with small datasets |
| Native multiclass | Algorithm handles it directly | Decision trees, Random Forest, Naive Bayes |
Next: Best Practices
Learn production deployment patterns, performance optimization, and common pitfalls to avoid.
Next: Best Practices →Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX