Skip to content

Dynamic typing for number of trees in RandomForestClassifier #7146

Closed

Description

@fabianegli

I think it would not hurt to allow n_estimators to be fed into RandomForestClassifier as a string. And if the input is not an int or throws a ValueException when transforming with int() there should be a more verbose error message in the style of "TypeError: n_estimators has to be an integer."

>>> from sklearn.ensemble import RandomForestClassifier
>>> forest = RandomForestClassifier(n_estimators=10, oob_score=True, n_jobs=1) 
>>> forest.fit([[1,2,3],[1,4,5],[0,3,2],[5,3,5]],[0,1,0,1]).predict([[2,3,5]])
array([1])
>>> forest = RandomForestClassifier(n_estimators='10', oob_score=True, n_jobs=1)
>>> forest.fit([[1,2,3],[1,4,5],[0,3,2],[5,3,5]],[0,1,0,1]).predict([2,3,5])    Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  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 58, in _validate_estimator
    if self.n_estimators <= 0:
TypeError: unorderable types: str() <= int()

Activity

  1. agramfort commented on Aug 5, 2016

    @agramfort
    Member

    docstring says it's a float. If we go down this path we'll have to type check any integer param input which is to me an overkill. I think you got it right quickly so it's no real pain.

  2. fabianegli commented on Aug 5, 2016

    @fabianegli
    ContributorAuthor

    It took quite some time, since I only saw the TypeError in my original setting but not the traceback. I can understand that it is low priority, but in my eyes it remains an issue.

  3. amueller commented on Sep 12, 2016

    @amueller
    Member

    @agramfort I think we in fact often do check input types and the validation that is done here is a bit weird...

  4. added
    Easy
    on Sep 12, 2016
  5. sudk1896 commented on Sep 13, 2016

    @sudk1896

    @fabianegli @amueller : I'd like to take a stab at this. I understand that you need to do type checking for n_estimators here, check if its float or string, if it is a string see if it is convertible to float. Is that what you intend to implement ? Thanks.

  6. amueller commented on Sep 13, 2016

    @amueller
    Member

    I think we should check if it's int (see #7394 or "git grep numbers"), and if it's not give an appropriate error message. I don't think we should try converting.

  7. raghavrv commented on Sep 15, 2016

    @raghavrv
    Member

    I don't think it's a good idea to start converting from strings. Already we validate enough... Just the error message alone should be improved...

  8. GaelVaroquaux commented on Sep 15, 2016

    @GaelVaroquaux
    Member
  9. raghavrv commented on Sep 15, 2016

    @raghavrv
    Member

    @sudk1896 Could you raise a PR which validates the n_estimators and raises an error if it is neither numbers.Integral nor np.integer?

  10. sudk1896 commented on Sep 15, 2016

    @sudk1896

    @raghavrv : There are different types of trees RandomForest and ExtraTrees. Do you want the error checking to be done in these or in the base classes such as BaseForest etc ?
    I am new to scikit-learn. Thanks.

  11. amueller commented on Sep 15, 2016

    @amueller
    Member

    The fewer places you can add the check, the better. Look at where the current check is and the error is raised.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Easy

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions