Skip to content

[MRG+1] - Voting classifier flatten transform (Continuation) #9188

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

Conversation

herilalaina
Copy link
Contributor

Reference Issue

Fixes #7230, continuation of #7794

What does this implement/fix? Explain your changes.

Improve tests, fixes docstring and pep8 error

@herilalaina
Copy link
Contributor Author

Ask for a review for this PR. @jnothman @amueller

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.

You've not tested the warning code, not silenced any warnings currently produced in testing or examples

@@ -163,6 +175,10 @@ def fit(self, X, y, sample_weight=None):
if n_isnone == len(self.estimators):
raise ValueError('All estimators are None. At least one is '
'required to be a classifier!')

if not self.flatten_transform and self.voting is 'soft':
Copy link
Member

Choose a reason for hiding this comment

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

This condition means the user who wants flatten_transform=False will always get the warning. We can avoid this by defaulting to some special marker, eg 'default'. We can tell the user in the warning message that setting it explicitly will Silence the warning

@jnothman
Copy link
Member

Otherwise it's looking good

@herilalaina herilalaina force-pushed the voting_classifier_flatten_transform branch 2 times, most recently from bf29f5f to 0926b4c Compare July 11, 2017 15:36
@herilalaina
Copy link
Contributor Author

Thanks @jnothman Changes have been made.


- Added ``flatten_transform`` parameter to :class:`ensemble.VotingClassifier`
to change output shape of `transform` method to 2 dimensional.
:issue:`7794` by `Ibraim Ganiev <olologin>` and
Copy link
Member

Choose a reason for hiding this comment

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

Missing :user: here and in the next line

" Setting it explicitly will silence this warning",
DeprecationWarning)
warnings.warn("'flatten_transform' default value will be "
"changed to True in 0.21.", DeprecationWarning)
Copy link
Member

Choose a reason for hiding this comment

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

I just meant to add here: To silence this warning you may explicitly set flatten_transform=False.

DeprecationWarning)
warnings.warn("'flatten_transform' default value will be "
"changed to True in 0.21.", DeprecationWarning)
self.flatten_transform = False
Copy link
Member

Choose a reason for hiding this comment

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

We shouldn't be modifying parameters. Just handle the default case in transform. In fact it makes sense to only raise this warning in transform as it won't be relevant to the majority of users who only care about prediction

@herilalaina
Copy link
Contributor Author

I moved the warning into transform as you said. default is handle into False now. Since we want to change default into True in 0.21, should I add any deprecation warning in transform (like previous commit) ?

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.

Yes, of course the full deprecation warning belongs in transform

@jnothman
Copy link
Member

The whole idea here is to maintain backwards compatibility, but to warn the users that they need to change their code to either maintain the old behaviour or adopt the new behaviour. Imagine using VotingClassifier(...='soft').fit_transform(X, y) now, then running the same code on a release with this PR merged.

if isinstance(self.flatten_transform,
str) and self.flatten_transform == 'default':
warnings.warn("To silence this warning you may"
" explicitly set flatten_transform=False",
Copy link
Member

Choose a reason for hiding this comment

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

Please merge these warnings into one

probas = self._collect_probas(X)
if isinstance(self.flatten_transform,
str) and self.flatten_transform == 'default':
warnings.warn("To silence this warning you may"
Copy link
Member

Choose a reason for hiding this comment

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

Surely the first sentence only makes sense after the second.

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.

You've got it. LGTM!

@jnothman jnothman changed the title [MRG] - Voting classifier flatten transform (Continuation) [MRG+1] - Voting classifier flatten transform (Continuation) Jul 12, 2017
@herilalaina
Copy link
Contributor Author

Thanks for all your review

Copy link
Member

@amueller amueller 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 apart from nitpicks.

@@ -284,6 +289,17 @@ Model evaluation and meta-estimators
- Added ``sample_weight`` parameter to :meth:`pipeline.Pipeline.score`.
:issue:`7723` by :user:`Mikhail Korobov <kmike>`.

- ``check_estimator`` now attempts to ensure that methods transform, predict, etc.
Copy link
Member

Choose a reason for hiding this comment

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

This seems unrelated.

@@ -61,6 +62,12 @@ class VotingClassifier(_BaseComposition, ClassifierMixin, TransformerMixin):
The number of jobs to run in parallel for ``fit``.
If -1, then the number of jobs is set to the number of cores.

flatten_transform : bool, optional (default='default')
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't it be None by default by convention?

flatten_transform : bool, optional (default='default')
Affects shape of transform output only when voting='soft'
If voting='soft' and flatten_transform=True, transform method returns
matrix with shape (n_samples, n_classifiers * n_classes) instead of
Copy link
Member

Choose a reason for hiding this comment

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

maybe instead of "instead" say "if flatten_transform=False it returns"...

Copy link
Member

Choose a reason for hiding this comment

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

Maybe say what the current default behavior is and that it will change in the future. You can also use a versionadded sphinx directive here.

@@ -256,16 +269,30 @@ def transform(self, X):

Returns
-------
If `voting='soft'`:
array-like = [n_classifiers, n_samples, n_classes]
If `voting='soft'` and `flatten_transform=False`:
Copy link
Member

Choose a reason for hiding this comment

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

And what if flatten_transform is True?

voting='soft',
flatten_transform=False).fit(X, y)

assert_array_equal(eclf1.transform(X).shape, (3, 4, 2))
Copy link
Member

Choose a reason for hiding this comment

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

can you wrap this in an assert_warns_message? Right now the warning is raised, and assert_warns_message actually returns the value, so you can then compare the shapes.

@herilalaina herilalaina force-pushed the voting_classifier_flatten_transform branch from 2e388bc to f1de47f Compare July 18, 2017 19:08
@amueller amueller merged commit 6f70202 into scikit-learn:master Jul 21, 2017
@amueller
Copy link
Member

Do we want to backport this? Otherwise we need to move the whatsnew entry and change the deprecation version.

@jnothman
Copy link
Member

jnothman commented Jul 22, 2017 via email

@amueller
Copy link
Member

@jnothman ok. I'll do all backporting in one go just before we release, I think.

jnothman pushed a commit to jnothman/scikit-learn that referenced this pull request Aug 6, 2017
…learn#9188)

* flatten_transform parameter added to VotingClassifier

* Regression test added

* What's new section added

* flake8 fix

* Improve test and docstring

* Add what's new entry

* default value flatten_transofrm

* Add test for warning msg

* Fix bug in assert_warns_message

* Move warn msg into transform

* Add deprecation warning

* Merge warning

* Change warn msg

* Move what's content into Trees and ensembles

* Fixes minor bug

* update what's new

* update test
dmohns pushed a commit to dmohns/scikit-learn that referenced this pull request Aug 7, 2017
…learn#9188)

* flatten_transform parameter added to VotingClassifier

* Regression test added

* What's new section added

* flake8 fix

* Improve test and docstring

* Add what's new entry

* default value flatten_transofrm

* Add test for warning msg

* Fix bug in assert_warns_message

* Move warn msg into transform

* Add deprecation warning

* Merge warning

* Change warn msg

* Move what's content into Trees and ensembles

* Fixes minor bug

* update what's new

* update test
dmohns pushed a commit to dmohns/scikit-learn that referenced this pull request Aug 7, 2017
…learn#9188)

* flatten_transform parameter added to VotingClassifier

* Regression test added

* What's new section added

* flake8 fix

* Improve test and docstring

* Add what's new entry

* default value flatten_transofrm

* Add test for warning msg

* Fix bug in assert_warns_message

* Move warn msg into transform

* Add deprecation warning

* Merge warning

* Change warn msg

* Move what's content into Trees and ensembles

* Fixes minor bug

* update what's new

* update test
paulha pushed a commit to paulha/scikit-learn that referenced this pull request Aug 19, 2017
…learn#9188)

* flatten_transform parameter added to VotingClassifier

* Regression test added

* What's new section added

* flake8 fix

* Improve test and docstring

* Add what's new entry

* default value flatten_transofrm

* Add test for warning msg

* Fix bug in assert_warns_message

* Move warn msg into transform

* Add deprecation warning

* Merge warning

* Change warn msg

* Move what's content into Trees and ensembles

* Fixes minor bug

* update what's new

* update test
AishwaryaRK pushed a commit to AishwaryaRK/scikit-learn that referenced this pull request Aug 29, 2017
…learn#9188)

* flatten_transform parameter added to VotingClassifier

* Regression test added

* What's new section added

* flake8 fix

* Improve test and docstring

* Add what's new entry

* default value flatten_transofrm

* Add test for warning msg

* Fix bug in assert_warns_message

* Move warn msg into transform

* Add deprecation warning

* Merge warning

* Change warn msg

* Move what's content into Trees and ensembles

* Fixes minor bug

* update what's new

* update test
maskani-moh pushed a commit to maskani-moh/scikit-learn that referenced this pull request Nov 15, 2017
…learn#9188)

* flatten_transform parameter added to VotingClassifier

* Regression test added

* What's new section added

* flake8 fix

* Improve test and docstring

* Add what's new entry

* default value flatten_transofrm

* Add test for warning msg

* Fix bug in assert_warns_message

* Move warn msg into transform

* Add deprecation warning

* Merge warning

* Change warn msg

* Move what's content into Trees and ensembles

* Fixes minor bug

* update what's new

* update test
jwjohnson314 pushed a commit to jwjohnson314/scikit-learn that referenced this pull request Dec 18, 2017
…learn#9188)

* flatten_transform parameter added to VotingClassifier

* Regression test added

* What's new section added

* flake8 fix

* Improve test and docstring

* Add what's new entry

* default value flatten_transofrm

* Add test for warning msg

* Fix bug in assert_warns_message

* Move warn msg into transform

* Add deprecation warning

* Merge warning

* Change warn msg

* Move what's content into Trees and ensembles

* Fixes minor bug

* update what's new

* update test
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.

Output shape of VotingClassifier.transform is non-standard
4 participants