A transformer of your own in Python
Implement fit and transform and your code slots into any pipeline.
import numpy as np
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
class ClipOutliers(BaseEstimator, TransformerMixin):
def __init__(self, quantile=0.95):
self.quantile = quantile
def fit(self, X, y=None):
self.limits_ = np.quantile(np.asarray(X), self.quantile, axis=0)
return self
def transform(self, X):
return np.minimum(np.asarray(X), self.limits_)
data = [[1.0, 2.0], [2.0, 4.0], [3.0, 90.0]]
pipeline = make_pipeline(ClipOutliers(quantile=0.9), StandardScaler())
print(pipeline.fit_transform(data).round(2))
print(pipeline.named_steps["clipoutliers"].limits_)
How it works
BaseEstimatorgives youget_paramsfor free, which grid search needs.fitlearns and returns self;transformnever learns.- Constructor arguments are stored unchanged, by convention.
Keywords and builtins used here
ClipOutliersasclassdeffitprintreturnselftransform
The run, in numbers
- Lines
- 22
- Characters to type
- 673
- Tokens
- 171
- Three-star pace
- 115 tpm
At the three-star pace of 115 tokens a minute, this run takes about 89 seconds.
Step 1 of 3 in Beyond the basics, step 22 of 25 in Machine learning with scikit-learn.