Stateless preprocessing
A pipeline needs a deterministic feature operation that has no fitted parameters.
Scikit-Learn · Class
FunctionTransformer turns an arbitrary callable into a scikit-learn transformer. It is designed for stateless operations such as logarithmic feature transforms or custom scaling.
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.
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)| Name | Type | Description |
|---|---|---|
func | callable, default=None | Callable used for the forward transformation; None means identity. |
inverse_func | callable, default=None | Callable used for inverse_transform; None means identity. |
validate | bool, default=False | Whether to check and convert X before calling func. |
accept_sparse | bool, default=False | Whether sparse input is accepted during validation. |
check_inverse | bool, default=True | Whether to check that func and inverse_func are approximately inverse. |
feature_names_out | callable, 'one-to-one', or None, default=None | Controls output feature-name generation. |
kw_args | dict, default=None | Additional keyword arguments forwarded to func. |
inv_kw_args | dict, default=None | Additional keyword arguments forwarded to inverse_func. |
A fitted transformer whose transform and inverse_transform methods call the configured functions.
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.
A pipeline needs a deterministic feature operation that has no fitted parameters.
Features contain frequencies or other nonnegative values needing compression.
A project has a domain-specific scaling rule represented by a Python function.
A callable needs to be inserted between existing scikit-learn pipeline stages.
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.
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.
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.
TransformerMixinPipelineColumnTransformersklearn.preprocessing.FunctionTransformer