简体   繁体   中英

Check if model from sklearn.ensemble has been fitted to data

Check if RandomForestClassifier object have been fitted to data. I can catch NotFittedError from sklearn.exceptions but is there better way? And in general we can't then is relaying on error catching good practice in Python ?

Probably the best way to check sklearn estimators is the check_is_fitted utility function, although that too will raise the NotFittedError on failure. Docs pages. That function mostly looks for "fitted attributes" that end in an underscore, so if you want to check specifically for RandomForestClassifier , maybe you should just check for hasattr(model, "estimators_") ?

As to whether try-except'ing is pythonic, see eg Using 'try' vs. 'if' in Python and Better to 'try' something and catch the exception or test if it's possible first to avoid an exception? .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM