Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 40 ยท Pipelines and Feature Engineering

Writing Custom Transformers

Create your own scikit-learn preprocessing steps with FunctionTransformer or by subclassing BaseEstimator and TransformerMixin, so custom logic works in pipelines and searches.

Built-in transformers cover a lot, but sooner or later you need a step that is specific to your data: take the log of income, add a "rooms per household" ratio, clip outliers using training percentiles. If you write that logic as loose code before the model, you lose the benefits of pipelines: the same step at prediction time, leakage safety, and tunable settings.

scikit-learn gives you two ways to make a step that fits in: FunctionTransformer for stateless functions, and a small class for steps that learn something from the training data.

FunctionTransformer

Wrap any function that takes an array and returns an array. It has nothing to learn, so fit does nothing.

Python FunctionTransformer with log1p

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

When the relationship is multiplicative or the feature is skewed, a log transform often straightens it. Giving inverse_func lets you call inverse_transform later. Use feature_names_out="one-to-one" if you want names carried through.

You can also pass kw_args={"k": 2} to give the function extra arguments, which are then visible to searches via functiontransformer__kw_args.

A class that learns: BaseEstimator + TransformerMixin

When a step must remember something from the training set, write a class with:

  • fit(self, X, y=None): learn and store attributes ending in _ (such as lower_). Return self.
  • transform(self, X): apply what was learned.
  • Constructor arguments are stored unchanged under the same names (no logic in __init__), so cloning and grid search work.

Inheriting BaseEstimator gives you get_params/set_params for free; inheriting TransformerMixin gives you fit_transform.

Python A winsorizing transformer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Because Clipper follows the conventions, grid search can set clipper__upper and clipper__lower, and cross_val_score will refit the percentiles on each training fold.

A feature-adding transformer

Many custom steps add columns, such as ratios of existing ones. Here is one that works with a DataFrame and reports its output names, so downstream tools can read them.

Python Ratio features with get_feature_names_out

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Domain-aware ratios such as rooms per household let simple models express relationships that raw counts hide.

Rules of thumb

A well-behaved transformer

Plain init Store arguments, nothing else.
Learn in fit Store results as name_ attributes.
Return self So fit_transform can chain.
No peeking transform must not refit.

Check it with sklearn.utils.estimator_checks.check_estimator.

1 / 2
  • Never compute statistics in transform from the data passed in; that would leak test data and change results with batch size.
  • Return the same number of rows you received.
  • Accept both NumPy arrays and DataFrames if you can; use validate_data (from sklearn.utils.validation) in production-grade transformers to check input and remember n_features_in_.
  • Put reusable transformers in a module of your project, not a notebook, so you can unpickle models later (see saving models).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. When is FunctionTransformer enough?

    FunctionTransformer wraps stateless functions. Steps that learn (percentiles, means) need a class with fit.
  2. 2. Where should a custom transformer store what it learned in fit?

    By convention fitted attributes end in an underscore; init only stores constructor arguments.
  3. 3. What does inheriting TransformerMixin give you?

    TransformerMixin provides fit_transform from your fit and transform. BaseEstimator provides get_params and set_params.
  4. 4. Why must transform not compute percentiles from its own input?

    Statistics must come from the training data in fit, so new data is treated the same way every time.

Summary

  • FunctionTransformer wraps stateless functions; subclass BaseEstimator and TransformerMixin for steps that learn.
  • Store constructor arguments unchanged, learned values in name_ attributes, and return self from fit.
  • Custom steps work with pipelines, cross-validation and grid search like built-in ones.
  • Add get_feature_names_out so column names survive.

Next: Feature Selection and Engineering