Thanks to visit codestin.com
Credit goes to github.com

Skip to content

TransformedTargetRegressor with Pipeline is not fitting model upon calling .fit #29983

Closed
@jmaddalena

Description

@jmaddalena

Describe the bug

A common use case is to use Pipeline to transform the feature set and to wrap it with TransformedTargetRegressor to transform the response variable. But when used in combination and calling fit on the TransformedTargetRegressor object, the model internal to the Pipeline is not actually fit.

Steps/Code to Reproduce

from sklearn.preprocessing import StandardScaler
from sklearn_pandas import DataFrameMapper
from xgboost import XGBRegressor
from sklearn.compose import TransformedTargetRegressor
from sklearn import datasets
from sklearn.pipeline import Pipeline
import pandas as pd
import numpy as np

iris = datasets.load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
y_col = "sepal length (cm)"
x_cols = [x for x in iris.feature_names if x != y_col]

model = XGBRegressor()

mod_pipeline = Pipeline([("scaler", StandardScaler()), ("model", model)])

mod_pipeline = TransformedTargetRegressor(regressor=mod_pipeline, func=np.log1p, inverse_func=np.expm1)

mod_pipeline.fit(df[x_cols], df[y_col])

mod_pipeline.regressor['model'].__sklearn_is_fitted__()

Expected Results

True

Actual Results

False

Versions

System:
    python: 3.11.9 (main, May 17 2024, 12:31:23) [Clang 14.0.3 (clang-1403.0.22.14.1)]
executable: /Users/jmaddalena/projects/vesta/.venv/bin/python
   machine: macOS-14.5-x86_64-i386-64bit

Python dependencies:
      sklearn: 1.4.1.post1
          pip: 24.0
   setuptools: 66.1.1
        numpy: 1.26.4
        scipy: 1.12.0
       Cython: 3.0.9
       pandas: 2.2.2
   matplotlib: 3.8.3
       joblib: 1.3.2
threadpoolctl: 3.3.0

Built with OpenMP: True

threadpoolctl info:
       user_api: openmp
   internal_api: openmp
    num_threads: 11
         prefix: libomp
       filepath: /Users/jmaddalena/projects/vesta/.venv/lib/python3.11/site-packages/sklearn/.dylibs/libomp.dylib
        version: None

       user_api: blas
   internal_api: openblas
    num_threads: 11
         prefix: libopenblas
       filepath: /Users/jmaddalena/projects/vesta/.venv/lib/python3.11/site-packages/numpy/.dylibs/libopenblas64_.0.dylib
        version: 0.3.23.dev
threading_layer: pthreads
   architecture: Nehalem

       user_api: blas
   internal_api: openblas
    num_threads: 11
         prefix: libopenblas
       filepath: /Users/jmaddalena/projects/vesta/.venv/lib/python3.11/site-packages/scipy/.dylibs/libopenblas.0.dylib
        version: 0.3.21.dev
threading_layer: pthreads
   architecture: Nehalem

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions