You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Dynamic typing for number of trees in RandomForestClassifier #7146
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()
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.
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.
@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.
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.
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.
@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.
Activity
agramfort commentedon Aug 5, 2016
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.
fabianegli commentedon Aug 5, 2016
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.
amueller commentedon Sep 12, 2016
@agramfort I think we in fact often do check input types and the validation that is done here is a bit weird...
sudk1896 commentedon Sep 13, 2016
@fabianegli @amueller : I'd like to take a stab at this. I understand that you need to do type checking for
n_estimatorshere, 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.amueller commentedon Sep 13, 2016
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.
raghavrv commentedon Sep 15, 2016
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...
GaelVaroquaux commentedon Sep 15, 2016
raghavrv commentedon Sep 15, 2016
@sudk1896 Could you raise a PR which validates the
n_estimatorsand raises an error if it is neithernumbers.Integralnornp.integer?sudk1896 commentedon Sep 15, 2016
@raghavrv : There are different types of trees
RandomForestandExtraTrees. Do you want the error checking to be done in these or in the base classes such asBaseForestetc ?I am new to scikit-learn. Thanks.
amueller commentedon Sep 15, 2016
The fewer places you can add the check, the better. Look at where the current check is and the error is raised.