Skip to content

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

Closed

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.

Activity

  1. added
    Easy
    on Sep 24, 2016
  2. fukatani commented on Sep 24, 2016

    @fukatani
    Contributor

    I'm interested in this issue and working on it!

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