typestar

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

  1. BaseEstimator gives you get_params for free, which grid search needs.
  2. fit learns and returns self; transform never learns.
  3. Constructor arguments are stored unchanged, by convention.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Beyond the basics, step 22 of 25 in Machine learning with scikit-learn.

← Previous Next →