Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

FunctionTransformer

FunctionTransformer turns an arbitrary callable into a scikit-learn transformer. It is designed for stateless operations such as logarithmic feature transforms or custom scaling.

How it works

FunctionTransformer wraps a callable as a scikit-learn transformer. During transformation it forwards X, and optionally y plus configured keyword arguments, to that callable; it can also wrap an inverse callable and optionally validate the input.

Syntax

class sklearn.preprocessing.FunctionTransformer(func=None, inverse_func=None, *, validate=False, accept_sparse=False, check_inverse=True, feature_names_out=None, kw_args=None, inv_kw_args=None)

Parameters

NameTypeDescription
funccallable, default=NoneCallable used for the forward transformation; None means identity.
inverse_funccallable, default=NoneCallable used for inverse_transform; None means identity.
validatebool, default=FalseWhether to check and convert X before calling func.
accept_sparsebool, default=FalseWhether sparse input is accepted during validation.
check_inversebool, default=TrueWhether to check that func and inverse_func are approximately inverse.
feature_names_outcallable, 'one-to-one', or None, default=NoneControls output feature-name generation.
kw_argsdict, default=NoneAdditional keyword arguments forwarded to func.
inv_kw_argsdict, default=NoneAdditional keyword arguments forwarded to inverse_func.

Returns

A fitted transformer whose transform and inverse_transform methods call the configured functions.

Key points

  • It adapts an ordinary Python callable to scikit-learn's transformer interface.
  • Typical uses include logarithmic feature transforms and custom scaling that do not learn state from training data.
  • The forward callable receives the arguments supplied to transform, with configured positional and keyword arguments forwarded.
  • Omitting func leaves the data unchanged during the forward transformation.
  • Setting feature_names_out to one-to-one preserves the input feature names for the output.

Common mistakes

  • Enabling validation converts the input to a two-dimensional NumPy array or sparse matrix before calling the function.
  • Avoid lambda functions when the fitted transformer must be serialized.

Example: Apply a log transformation

import numpy as np
from sklearn.preprocessing import FunctionTransformer

transformer = FunctionTransformer(np.log1p, validate=True)
X = np.array([[0, 1], [2, 3]])
print(transformer.fit_transform(X))

Applies a logarithm-plus-one transformation to every element of a NumPy array.

When to use it

Stateless preprocessing

A pipeline needs a deterministic feature operation that has no fitted parameters.

Logarithmic transformation

Features contain frequencies or other nonnegative values needing compression.

Custom scaling

A project has a domain-specific scaling rule represented by a Python function.

Pipeline adaptation

A callable needs to be inserted between existing scikit-learn pipeline stages.

Interview questions

QWhat does FunctionTransformer do?

It creates a transformer around an arbitrary callable. The callable receives X, and optionally y, and its return value becomes the transformed output.

Follow-up: How would you pass fixed configuration to the callable? Use kw_args, which forwards a dictionary of keyword arguments during transformation.

QWhat are func and inverse_func used for?

func defines the forward transformation and inverse_func defines the inverse transformation. If either is omitted, the corresponding operation uses the identity function.

Follow-up: How can the relationship between the two functions be checked? Keep check_inverse enabled and fit the transformer so scikit-learn can check their consistency.

QWhat does validate control?

With validate=False, the input is not checked before func is called. With validate=True, X is converted to a two-dimensional NumPy array or sparse matrix before the callable runs.

Follow-up: When is this wrapper especially useful? It is suited to stateless operations, such as logarithmic transformations or custom scaling, inside a pipeline.

  • TransformerMixin
  • Pipeline
  • ColumnTransformer
  • sklearn.preprocessing.FunctionTransformer

Source: FunctionTransformer — scikit-learn 1.9.1 documentation. Checked 2026-10-15.