Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Scikit learn Scikit learn FunctionTransformer

From Leeroopedia


Knowledge Sources
Domains Data Preprocessing, Feature Engineering
Last Updated 2026-02-08 15:00 GMT

Overview

Concrete tool for constructing a transformer from an arbitrary callable provided by scikit-learn.

Description

FunctionTransformer forwards its X (and optionally y) arguments to a user-defined function or function object and returns the result. This is useful for stateless transformations such as taking the log of frequencies, doing custom scaling, or any other custom feature transformation. It integrates seamlessly into scikit-learn pipelines.

Usage

Use FunctionTransformer when you need to apply a custom stateless transformation as part of a scikit-learn pipeline. It wraps any callable into a transformer-compatible interface, enabling custom preprocessing steps alongside standard scikit-learn transformers.

Code Reference

Source Location

Signature

class FunctionTransformer(TransformerMixin, BaseEstimator):
    def __init__(
        self,
        func=None,
        inverse_func=None,
        *,
        validate=False,
        accept_sparse=False,
        check_inverse=True,
        feature_names_out=None,
        kw_args=None,
        inv_kw_args=None,
    ):

Import

from sklearn.preprocessing import FunctionTransformer

I/O Contract

Inputs

Name Type Required Description
func callable No The callable to use for the transformation. If None, uses the identity function.
inverse_func callable No The callable to use for the inverse transformation. If None, uses the identity function.
validate bool No Whether to validate the input X array before calling func. Default is False.
accept_sparse bool No Whether func accepts a sparse matrix as input. Default is False.
check_inverse bool No Whether to check that func followed by inverse_func leads to the original inputs. Default is True.
feature_names_out callable or str No Determines the feature names out of the transformer. If 'one-to-one', output names equal input names.
kw_args dict No Dictionary of additional keyword arguments to pass to func.
inv_kw_args dict No Dictionary of additional keyword arguments to pass to inverse_func.

Outputs

Name Type Description
X_transformed array-like The result of applying func to X.

Usage Examples

Basic Usage

from sklearn.preprocessing import FunctionTransformer
import numpy as np

transformer = FunctionTransformer(np.log1p, np.expm1)
X = np.array([[0, 1], [2, 3]])
X_transformed = transformer.fit_transform(X)
print(X_transformed)
# Inverse transform
X_original = transformer.inverse_transform(X_transformed)
print(X_original)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment