Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Support sklearn.compose.TransformedTargetRegressor #240

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion tests/test_trainable.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
from tune_sklearn._detect_booster import (
has_xgboost, has_required_lightgbm_version, has_catboost)

from lightgbm import LGBMRegressor
from ray.tune.schedulers import AsyncHyperBandScheduler
from sklearn.compose import TransformedTargetRegressor
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression, SGDClassifier
from sklearn.model_selection import check_cv
from sklearn.svm import SVC

from tune_sklearn.utils import _check_multimetric_scoring, get_early_stop_type
from tune_sklearn.utils import _check_multimetric_scoring, get_early_stop_type, EarlyStopping


def create_xgboost():
Expand Down Expand Up @@ -203,3 +206,14 @@ def testWarmStart(self):
trainable.train()
trainable.train()
trainable.stop()


class TestGetEarlyStopType(unittest.TestCase):
def testLGBMRegressor(self):
lgbm_regressor = LGBMRegressor()
transformed_target_regressor = TransformedTargetRegressor(lgbm_regressor)
early_stopping_type = get_early_stop_type(
estimator=transformed_target_regressor,
early_stopping=AsyncHyperBandScheduler(),
)
self.assertEqual(early_stopping_type, EarlyStopping.LGBM)
15 changes: 15 additions & 0 deletions tune_sklearn/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from collections import defaultdict
from typing import Dict

from sklearn.base import RegressorMixin
from sklearn.compose import TransformedTargetRegressor
from sklearn.metrics import check_scoring
from sklearn.pipeline import Pipeline

from tune_sklearn._detect_booster import (
is_xgboost_model, is_lightgbm_model_of_required_version, is_catboost_model)
import numpy as np
Expand Down Expand Up @@ -94,8 +97,20 @@ def check_error_warm_start(early_stop_type, estimator_config, estimator):


def get_early_stop_type(estimator, early_stopping):
# If estimator is TransformedTargetRegressor we should get the wrapped regressor.
if isinstance(estimator, TransformedTargetRegressor):
estimator = estimator.regressor

if not early_stopping:
return EarlyStopping.NO_EARLY_STOP

if check_is_pipeline(estimator):
for step_name, step in estimator.steps:
Copy link
Member

@Yard1 Yard1 Apr 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three things:

  • we shouldn't be only checking for regressors
  • this is not foolproof & goes against duck typing principle of sklearn
  • we can just assume that the last step of the pipeline is an estimator. You can't really make a pipeline with multiple estimators.

is_regressor = isinstance(step, RegressorMixin)
if is_regressor:
estimator = step
break

can_partial_fit = check_partial_fit(estimator)
can_warm_start_iter = check_warm_start_iter(estimator)
can_warm_start_ensemble = check_warm_start_ensemble(estimator)
Expand Down