Skip to content

Error when importing eli5 #39

Closed
Closed
@leetabix

Description

@leetabix

When creating a virtual environment (using venv) and pip installing eli5 and jupyter lab, I'm unable to import eli5 as I receive the following error in my notebook:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[1], line 1
----> 1 import eli5

File \Lib\site-packages\eli5\__init__.py:13
      6 from .formatters import (
      7     format_as_html,
      8     format_html_styles,
      9     format_as_text,
     10     format_as_dict,
     11 )
     12 from .explain import explain_weights, explain_prediction
---> 13 from .sklearn import explain_weights_sklearn, explain_prediction_sklearn
     14 from .transform import transform_feature_names
     17 try:

File \Lib\site-packages\eli5\sklearn\__init__.py:3
      1 # -*- coding: utf-8 -*-
      2 from __future__ import absolute_import
----> 3 from .explain_weights import (
      4     explain_weights_sklearn,
      5     explain_linear_classifier_weights,
      6     explain_linear_regressor_weights,
      7     explain_rf_feature_importance,
      8     explain_decision_tree,
      9 )
     10 from .explain_prediction import (
     11     explain_prediction_sklearn,
     12     explain_prediction_linear_classifier,
     13     explain_prediction_linear_regressor,
     14 )
     15 from .unhashing import (
     16     InvertableHashingVectorizer,
     17     FeatureUnhasher,
     18     invert_hashing_and_fit,
     19 )

File \Lib\site-packages\eli5\sklearn\explain_weights.py:78
     73 from eli5.transform import transform_feature_names
     74 from eli5._feature_importances import (
     75     get_feature_importances_filtered,
     76     get_feature_importance_explanation,
     77 )
---> 78 from .permutation_importance import PermutationImportance
     81 LINEAR_CAVEATS = """
     82 Caveats:
     83 1. Be careful with features which are not
   (...)
     90    classification result for most examples.
     91 """.lstrip()
     93 HASHING_CAVEATS = """
     94 Feature names are restored from their hashes; this is not 100% precise
     95 because collisions are possible. For known collisions possible feature names
   (...)
     99 the result is positive.
    100 """.lstrip()

File \Lib\site-packages\eli5\sklearn\permutation_importance.py:7
      5 import numpy as np
      6 from sklearn.model_selection import check_cv
----> 7 from sklearn.utils.metaestimators import if_delegate_has_method
      8 from sklearn.utils import check_array, check_random_state
      9 from sklearn.base import (
     10     BaseEstimator,
     11     MetaEstimatorMixin,
     12     clone,
     13     is_classifier
     14 )

ImportError: cannot import name 'if_delegate_has_method' from 'sklearn.utils.metaestimators' (\Lib\site-packages\sklearn\utils\metaestimators.py)

The issue appears to be due to scikit-learn depreciating the if_delegate_has_method decorator (in \Lib\site-packages\sklearn\utils\metaestimators.py) and replacing it with another decorator called available_if.

I've made the following updates to \Lib\site-packages\eli5\sklearn\permutation_importance.py and I'm now able to import the eli5 library:

  • changed import statement to mention available_if instead of if_delegate_has_method
  • replaced all mentions of:
    @if_delegate_has_method(delegate='wrapped_estimator_')

with

    @available_if(_estimator_has('wrapped_estimator_'))
  • defined a new _estimator_has function for use with the above

Please let me know if you'd like me to propose the above update for merge.

Thanks,
James

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions