Skip to content

[MRG+2] BUG: do not call fit twice in TransformedTargetetRegressor #11641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 3, 2019

Conversation

glemaitre
Copy link
Member

closes #11618

Call fit only if check_inverse=True.

@glemaitre glemaitre changed the title BUG: do not call fit if not checking inverse in TransformedTargetetRegressor [MRG] BUG: do not call fit if not checking inverse in TransformedTargetetRegressor Jul 20, 2018
@glemaitre
Copy link
Member Author

@jnothman @jorisvandenbossche Could you have a look at that.

Copy link
Member

@jnothman jnothman left a comment

Choose a reason for hiding this comment

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

This looks like the right fix. Is it silly to add a test??

Is there a reason we should not be doing the fit_transform in _fit_transformer?

@glemaitre
Copy link
Member Author

Because we want to avoid the transform if we don't need to make the checking, isn't it?

@jnothman
Copy link
Member

I think we're transforming y in all cases in order to train the regressor...

@glemaitre
Copy link
Member Author

I think we're transforming y in all cases in order to train the regressor...

The transform called in _fit_transformer is only there for checking that transform followed by inverse_transform is giving the same results than the original data. This operation can be skipped if we are not interested in this check.

@glemaitre
Copy link
Member Author

@thomasjpfan @jorisvandenbossche @rth if you can have a look at this PR.

@jnothman jnothman added this to the 0.20.3 milestone Jan 27, 2019
Copy link
Member

@thomasjpfan thomasjpfan left a comment

Choose a reason for hiding this comment

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

To make this easier for other reviewers, the "...if not checking inverse..." should be removed from the title.

A test that uses a dummy transformer asserting that fit is called once, would be nice to have.

@@ -184,7 +190,7 @@ def fit(self, X, y, sample_weight=None):
self.regressor_ = clone(self.regressor)

# transform y and convert back to 1d array if needed
y_trans = self.transformer_.fit_transform(y_2d)
y_trans = self.transformer_.transform(y_2d)
Copy link
Member

Choose a reason for hiding this comment

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

Moving the following block to up a few lines so it follows self._fit_transformer(y_2d) would make it easier to follow how y transforms.

y_trans = self.transformer_.transform(y_2d)
...
if y_trans.ndim == 2 and y_trans.shape[1] == 1:
    y_trans = y_trans.squeeze(axis=1)

Copy link
Member

@jeremiedbb jeremiedbb left a comment

Choose a reason for hiding this comment

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

I agree with @thomasjpfan comment to move the fit close to _fit_transformer, which should happen before dealing with the regressor. Otherwise lgtm.

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Looks good to me. @thomasjpfan also asked for a test. Do we think that is needed? (it's not really a bug, rather implementation efficiency)

@glemaitre glemaitre changed the title [MRG] BUG: do not call fit if not checking inverse in TransformedTargetetRegressor [MRG] BUG: do not call fit in TransformedTargetetRegressor Jan 30, 2019
@glemaitre glemaitre changed the title [MRG] BUG: do not call fit in TransformedTargetetRegressor [MRG] BUG: do not call fit twice in TransformedTargetetRegressor Jan 30, 2019
@jnothman
Copy link
Member

jnothman commented Jan 30, 2019 via email

@jorisvandenbossche
Copy link
Member

This is ready to merge?

Copy link
Member

@qinhanmin2014 qinhanmin2014 left a comment

Choose a reason for hiding this comment

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

please add a what's new entry

return X


@pytest.mark.parametrize("check_inverse", [False, True])
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we only need to test check_inverse = False?

@qinhanmin2014 qinhanmin2014 changed the title [MRG] BUG: do not call fit twice in TransformedTargetetRegressor [MRG+2] BUG: do not call fit twice in TransformedTargetetRegressor Feb 1, 2019
@glemaitre
Copy link
Member Author

please add a what's new entry

It is not touching any behaviour that a user should be aware of, isn't it?

@qinhanmin2014 qinhanmin2014 merged commit c5075f5 into scikit-learn:master Feb 3, 2019
thomasjpfan pushed a commit to thomasjpfan/scikit-learn that referenced this pull request Feb 6, 2019
thomasjpfan pushed a commit to thomasjpfan/scikit-learn that referenced this pull request Feb 7, 2019
jnothman pushed a commit to jnothman/scikit-learn that referenced this pull request Feb 19, 2019
@jnothman jnothman mentioned this pull request Feb 19, 2019
17 tasks
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
koenvandevelde pushed a commit to koenvandevelde/scikit-learn that referenced this pull request Jul 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TransforemdTargetRegressor fit calls to transformer.fit twice
6 participants