Skip to content

DOC Improve docstring around set_output #24672

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 6 commits into from
Oct 18, 2022

Conversation

thomasjpfan
Copy link
Member

@thomasjpfan thomasjpfan commented Oct 15, 2022

Reference Issues/PRs

Related to scikit-learn/enhancement_proposals#78

What does this implement/fix? Explain your changes.

This PR improves the user docs and developer docs around set_output.

@thomasjpfan thomasjpfan added the Quick Review For PRs that are quick to review label Oct 15, 2022
@@ -158,6 +158,10 @@ def set_output(self, transform=None):
transform : {"default", "pandas"}, default=None
Configure output of `transform` and `fit_transform`.

- `"default"`: Output of an un-configured transformer
- `"pandas"`: DataFrames output
- `"None"`: No-op
Copy link
Member

Choose a reason for hiding this comment

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

Uhm. It should be the keyword None and not the string "None", isn't it?

@@ -265,6 +265,10 @@ def set_output(self, transform=None):
transform : {"default", "pandas"}, default=None
Configure output of `transform` and `fit_transform`.

- `"default"`: Output of an un-configured transformer
- `"pandas"`: DataFrames output
- `"None"`: No-op
Copy link
Member

Choose a reason for hiding this comment

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

After reading the "no op" explanation I was left wondering how "no-op" was different from "default". Looking at the code I think the answer is "they are the same". If that is correct, can we say "None: Default output (format) of a transformer"?

I think I like "Default output format of a transformer" better than "Output of an un-configured transformer", but I am not sure if they are equivalent? As in, I could imagine that there is a subtle difference between "unconfigured" and "default"?,

Copy link
Member

@adrinjalali adrinjalali Oct 17, 2022

Choose a reason for hiding this comment

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

I think no-op is supposed to mean transform=None would do nothing, but transform="default" sets the output to be pandas. As in: est.set_output(transform="pandas").set_outout(transform=None) vs est.set_output(transform="pandas").set_outout(transform="default"): the first one returns a pandas dataframe.

Copy link
Member

Choose a reason for hiding this comment

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

Ouf, that is subtle indeed. Why does None exist as an option if it does nothing (not even reset the state to "unconfigured")?

but transform="default" sets the output to be pandas

is this right? I was expecting "default" to be "not pandas"/numpy because there is an option to set pandas

Copy link
Member

Choose a reason for hiding this comment

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

is this right?

Sorry, typo, but you got the intention :D

Why does None exist as an option if it does nothing

This method might start having other args as well, and then we'd want to be able to set only one thing, while leaving the rest unchanged:

def f(a=None, b=None):
    ...

# This would not change the value set by the method for `b`
f(a="value")

Copy link
Member Author

@thomasjpfan thomasjpfan Oct 17, 2022

Choose a reason for hiding this comment

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

Why does None exist as an option if it does nothing (not even reset the state to "unconfigured")?

In the future, we may want to add predict to set_output:

pipe.set_output(transform="pandas")
pipe.set_output(predict="pandas")

The second call with predict="pandas" should not adjust the configuration for transform.

is this right? I was expecting "default" to be "not pandas"/numpy because there is an option to set pandas

I took into account existing third party estimators that only return DataFrames as the default. If we had "default" == "numpy" output, then they would not be following the set_output API. I did not want to add more work for third party libraries to convert their DataFrames into NumPy arrays just to conform to the API.

Even in scikit-learn, there are three possible return containers for transform="default":

  • With the recent Array API support: Any Array API container such as cupy.array_api.Array
  • NumPy's ndarray
  • SciPy's sparse matrix

Copy link
Member Author

Choose a reason for hiding this comment

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

From this discussion, I updated the docstring to:

- `"default"`: Default output format of a transformer
- `"pandas"`: DataFrame output
- `None`: Transform configuration is unchanged

Copy link
Member

Choose a reason for hiding this comment

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

Cool! Thanks for all the explanations. I like your change Thomas.

Copy link
Contributor

@Micky774 Micky774 left a comment

Choose a reason for hiding this comment

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

LGTM

@thomasjpfan thomasjpfan added this to the 1.2 milestone Oct 18, 2022
@Micky774 Micky774 merged commit d4306ba into scikit-learn:main Oct 18, 2022
glemaitre pushed a commit to glemaitre/scikit-learn that referenced this pull request Oct 31, 2022
* DOC Improve docstring around set_output

* DOC Improve docs around set_output

* DOC Address comments

* DOC Better grammar

* DOC Improve wording

* DOC Improves docstring in set_config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Quick Review For PRs that are quick to review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants