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 TransformedTargetRegressor

From Leeroopedia
Revision as of 16:38, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Scikit_learn_Scikit_learn_TransformedTargetRegressor.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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

Overview

Concrete tool for regressing on a transformed target variable provided by scikit-learn.

Description

TransformedTargetRegressor is a meta-estimator that applies a non-linear transformation to the target variable y in regression problems. During fit, it transforms the target (using a transformer object or a func/inverse_func pair) and fits the regressor on the transformed target. During predict, it applies the inverse transformation to the regressor's predictions. This is useful when the target distribution benefits from transformation, such as applying logarithmic transformation to skewed targets.

Usage

Use TransformedTargetRegressor when the target variable in a regression problem would benefit from transformation before fitting, such as log-transforming skewed targets or applying quantile normalization. It ensures the transformation is consistently applied during training and the inverse is correctly applied to predictions, avoiding manual transformation errors.

Code Reference

Source Location

Signature

class TransformedTargetRegressor(RegressorMixin, BaseEstimator):
    def __init__(
        self,
        regressor=None,
        *,
        transformer=None,
        func=None,
        inverse_func=None,
        check_inverse=True,
    ):

Import

from sklearn.compose import TransformedTargetRegressor

I/O Contract

Inputs

Name Type Required Description
regressor estimator object No Regressor object (e.g., derived from RegressorMixin). If None, LinearRegression is used. Default is None.
transformer estimator object No Transformer object (e.g., derived from TransformerMixin). Cannot be set at the same time as func/inverse_func. Default is None.
func callable No Function to apply to y before fitting. Cannot be set with transformer. Must return a 2D array. Default is None.
inverse_func callable No Function to apply to predictions to return to original target space. Required if func is set. Default is None.
check_inverse bool No Whether to verify that func followed by inverse_func returns the original values. Default is True.

Outputs

Name Type Description
y_pred ndarray of shape (n_samples,) Predicted target values, inverse-transformed to the original target space.
regressor_ estimator instance The fitted regressor trained on the transformed target.
transformer_ estimator instance The fitted transformer (either the provided transformer or a FunctionTransformer wrapping func/inverse_func).

Usage Examples

Basic Usage

from sklearn.compose import TransformedTargetRegressor
from sklearn.linear_model import LinearRegression
from sklearn.datasets import make_regression
import numpy as np

X, y = make_regression(n_samples=100, n_features=5, noise=10, random_state=42)

# Using func/inverse_func pair
regr = TransformedTargetRegressor(
    regressor=LinearRegression(),
    func=np.log1p,
    inverse_func=np.expm1,
)
# Shift target to be positive for log transform
y_positive = y - y.min() + 1
regr.fit(X, y_positive)
y_pred = regr.predict(X)
print(f"Predictions shape: {y_pred.shape}")

# Using a transformer object
from sklearn.preprocessing import QuantileTransformer
regr2 = TransformedTargetRegressor(
    regressor=LinearRegression(),
    transformer=QuantileTransformer(output_distribution="normal"),
)
regr2.fit(X, y)
y_pred2 = regr2.predict(X)
print(f"Predictions shape: {y_pred2.shape}")

Related Pages

Page Connections

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