Skip to content

hasattr(est, 'feature_importances_') should never happen #7481

@jnothman

Description

@jnothman

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    EasyWell-defined and straightforward way to resolve

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions