eli5-org / eli5

A library for debugging/inspecting machine learning classifiers and explaining their predictions
MIT License
260 stars 42 forks source link

Error when importing eli5 #39

Open leetabix opened 1 year ago

leetabix commented 1 year ago

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:

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

Thanks, James

GegznaV commented 1 year ago

@leetabix I think you should propose a pull request (PR) with the changes you've already made: it at least will be possible to install the working version of the package from your PR.

GegznaV commented 1 year ago

Related:

frosty24 commented 1 year ago

This commit addresses this as well: https://github.com/eli5-org/eli5/pull/31/commits/840695d869e47b8e6cc05baca428d24881113fb6

GegznaV commented 1 year ago

@frosty24, is https://github.com/eli5-org/eli5/commit/840695d869e47b8e6cc05baca428d24881113fb6 merged to the main branch of this repo? I'm a bit confused as GitHub informs: image

frosty24 commented 1 year ago

I picked this commit from the following open PR in this repo: https://github.com/eli5-org/eli5/pull/31

isabelizimm commented 1 year ago

Hi all! I ran into this error as well. I ended up installing scikit-learn==1.2.2 and then was able to import eli5 successfully.

mprytoluk commented 12 months ago

Is it possible for me to do something about this? I've stopped using eli5 in my project because I had to update scikit-learn.

GegznaV commented 11 months ago

If nobody answers, you may use SHAP values as an alternative: https://github.com/shap/shap That project is currently alive.

RobertCarruthers-Ki commented 10 months ago

Slight correction to the above.

Instead of replacing @if_delegate_has_method(delegate='wrapped_estimator_') with: @available_if(_estimator_has('wrapped_estimator_')) it should be replaced with the name of the function being checked for: @available_if(_estimator_has('predict_proba')) (or whatever the relevant function is) based on: https://github.com/scikit-learn/scikit-learn/issues/20506

This is implemented in my fork here

DrJStrudwick commented 7 months ago

Any update on this?

mprytoluk commented 7 months ago

I believe it's safe to say this project is dead. Some fork on this project would be great.