scikit-learn / scikit-learn/scikit-learn

Allow NaNs for the target values in TransformedTargetRegressor

Open
#11,339 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

help wanted module:compose
Dominant language
Python
Stars
67.3k
Forks
27.4k
Avg merge
1d 15h
Merged PRs (30d)
58

Description

Description

One potential use case for TransformedTargetRegressor is to get rid of missing values in the target. but currently initial check of the fit method doesn't allow such array.

Steps/Code to Reproduce

Example:

from sklearn.compose import TransformedTargetRegressor
from sklearn.impute import SimpleImputer
from sklearn.tree import DecisionTreeRegressor
from sklearn import datasets

X, y = datasets.load_linnerud(return_X_y=True)

## put some NaN in y
y[5, 1] = np.NaN

estimator = TransformedTargetRegressor(
    regressor=DecisionTreeRegressor(),
    func = lambda _y: SimpleImputer().fit_transform(_y), # becuse SimpleImputer doesnt have inverse
    inverse_func = lambda _y: _y,
    check_inverse = False
)

estimator.fit(X, y)

This raises:
ValueError: Input contains NaN, infinity or a value too large for dtype('float64').

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in sklearn/compose/_target.py around the initial check in TransformedTargetRegressor.fit, then inspect the provided reproduction using SimpleImputer and DecisionTreeRegressor. Determine how target NaNs should be handled while preserving the transformer behavior, and verify the reproduction no longer raises the reported ValueError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.