Skip to content

[MRG+1] Throw an error with explicit message if n_estimators is not an integer. #7457

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 8 commits into from
Oct 14, 2016

Conversation

fabianegli
Copy link
Contributor

Reference Issue

Example: Fixes #7146

What does this implement/fix? Explain your changes.

Added type checking of n_estimators and made the corresponding error message explicit about the type issue.

I used the Type checking currently proposed in issue #7394

Any other comments?

I got the intended behavior when testing with rf.fit(n_estimators=100), which works as expeced, and rf.fit(n_estimators='100') which throws the type error as expected.

Example error message with traceback:

Traceback (most recent call last):
File "", line 1, in
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/sklearn/ensemble/forest.py", line 247, in fit
self._validate_estimator()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/sklearn/ensemble/base.py", line 61, in _validate_estimator
"got {0}.".format(type(self.n_estimators)))
ValueError: n_estimators must be an integer, got <class 'str'>.

@raghavrv
Copy link
Member

Could you close this one and merge this commit onto your other PR (#7454). Both can be reviewed in one go...

@fabianegli
Copy link
Contributor Author

Couldn't figure out how to merge pull requests. I closed the old pull request. Since only whitespace characters were changed/deleted to comply with pep8 there is nothing lost by closing the first pull request, which does not have to be reviewed at all any more.

@raghavrv
Copy link
Member

While you are in this branch (issue_7146_pep8), do git cherry-pick abd95d3. That will bring the changes brought about by your recently closed PR (#7454) into this active PR.

@fabianegli
Copy link
Contributor Author

fabianegli commented Sep 19, 2016

Ah, cool, thank you for teaching me how to 'cherry-pick'! There is nothing in the closed pull request that I want to migrate though. Everything that should be here from the other branch is already in this pull request.
This might be bad practice, but I just checked out the master branch again, made a new branch, added the improved pep8-complying changes again. All because of my lack of 'cherry-pick'ing skills... I am sorry for the mess/confusion.
I wonder: Is it a big nogo to close a pull request and completely forget about it?

@raghavrv
Copy link
Member

No problem at all. Thanks for the work! I didn't know you did that already...

Now you will have to add the tests and ping us again.

@fabianegli
Copy link
Contributor Author

Now I understand how this works with the pull requests and latter pushing to the same branch which contains the pull request. It works even smoother than I imagined!

@TomDLT
Copy link
Member

TomDLT commented Sep 19, 2016

Thanks for adding a test, but it fails on your branch because of a typo.

@fabianegli
Copy link
Contributor Author

Will it work if I amend the commit in the branch locally and push it 'again'?

@TomDLT
Copy link
Member

TomDLT commented Sep 19, 2016

  • If you add a new commit, you can push normally.
  • If you amend an commit that has already been pushed, you will need to "force push", with the option git push -f, but you should use it wisely as it is a strong source of problems.

@fabianegli
Copy link
Contributor Author

Just made a new commit. No forcing done.

def test_base_not_int_n_estimators():
# Check that instantiating a BaseEnsemble with a string as n_estimators raises
# a ValueError requesting n_estimators to be supplied as an integer.
ensemble_string = BaggingClassifier(base_estimator=Perceptron(), n_estimators='3')
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

  • check if this works for np.int32(3)
  • check if this raises an error for 3.0

@fabianegli
Copy link
Contributor Author

I wonder, is there an easy way to build scikit-learn on Mac OS X?
python setup.py install --prefix /path/to/prefix did run, but testing with nosetests failed utterly on me with a message that scikit learn seems to have been built incorrectly and the following error message: ImportError: No module named 'sklearn.__check_build._check_build'

@TomDLT
Copy link
Member

TomDLT commented Sep 20, 2016

  • Be sure to remove all scikit-learn installation in your environment (check with pip list and/or conda list)
  • Install with python setup.py develop

More info in the contributing guide.

@amueller
Copy link
Member

amueller commented Oct 7, 2016

needs a rebase.

@fabianegli
Copy link
Contributor Author

Is there anything else that needs to be done before this PR can be merged?

@TomDLT
Copy link
Member

TomDLT commented Oct 14, 2016

LGTM

@TomDLT TomDLT changed the title Throw an error with explicit message if n_estimators is not an integer. [MRG+1] Throw an error with explicit message if n_estimators is not an integer. Oct 14, 2016
@TomDLT
Copy link
Member

TomDLT commented Oct 14, 2016

I wonder how the squash_and_merge GitHub function will deal with the rebase commit (d0666e4).
Yet the diff looks good, so I guess it might works.


def test_base_zero_n_estimators():
# Check that instantiating a BaseEnsemble with n_estimators<=0 raises
# a ValueError.
ensemble = BaggingClassifier(base_estimator=Perceptron(), n_estimators=0)
ensemble = BaggingClassifier(base_estimator=Perceptron(),
Copy link
Member

Choose a reason for hiding this comment

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

why the newline?

@amueller
Copy link
Member

@TomDLT rebase commit? I don't think there's such a thing...

@amueller amueller merged commit 53e6381 into scikit-learn:master Oct 14, 2016
@amueller
Copy link
Member

thanks!

@fabianegli fabianegli deleted the issue_7146_pep8 branch October 14, 2016 20:13
amueller pushed a commit to amueller/scikit-learn that referenced this pull request Oct 14, 2016
…n integer. (scikit-learn#7457)

* Throw an error with explicit message if n_estimators is not an integer.

* Testing for explicit message if n_estimators is not an integer.

* Fixed typo in test for explicit message if n_estimators is an integer.

* Added tests for np.int32 and float input.

* pep8 compliance

* fix function name

* Import numpy to test n_estimators suplied as numpy int32.
Sundrique pushed a commit to Sundrique/scikit-learn that referenced this pull request Jun 14, 2017
…n integer. (scikit-learn#7457)

* Throw an error with explicit message if n_estimators is not an integer.

* Testing for explicit message if n_estimators is not an integer.

* Fixed typo in test for explicit message if n_estimators is an integer.

* Added tests for np.int32 and float input.

* pep8 compliance

* fix function name

* Import numpy to test n_estimators suplied as numpy int32.
paulha pushed a commit to paulha/scikit-learn that referenced this pull request Aug 19, 2017
…n integer. (scikit-learn#7457)

* Throw an error with explicit message if n_estimators is not an integer.

* Testing for explicit message if n_estimators is not an integer.

* Fixed typo in test for explicit message if n_estimators is an integer.

* Added tests for np.int32 and float input.

* pep8 compliance

* fix function name

* Import numpy to test n_estimators suplied as numpy int32.
maskani-moh pushed a commit to maskani-moh/scikit-learn that referenced this pull request Nov 15, 2017
…n integer. (scikit-learn#7457)

* Throw an error with explicit message if n_estimators is not an integer.

* Testing for explicit message if n_estimators is not an integer.

* Fixed typo in test for explicit message if n_estimators is an integer.

* Added tests for np.int32 and float input.

* pep8 compliance

* fix function name

* Import numpy to test n_estimators suplied as numpy int32.
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.

Dynamic typing for number of trees in RandomForestClassifier
4 participants