-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
hasattr(est, 'feature_importances_') should never happen #7481
Description
In some cases, notably TreeClassifier/Regressor and ensembles, feature_importances_ is defined as a property and takes substantial calculation. This violates the assumption that getting an attribute or checking for its presence will be fast, but we can't fix that API design bug in the short term. With properties, a call to hasattr(est, some_attribute) takes as long as getting that attribute: it executes the descriptor's __get__ and hasattr will return True if it runs without error.
This means that if hasattr(est, 'feature_importances_'): do something with est.feature_importances_ is slow. We need to replace these (in SelectFromModel and RFE) with getattr(obj, feature_importances_, DUMMY) or something.
We should look for other cases where hasattr is used to check for model attributes that may be defined by expensive descriptors.
Related to #7478.