I guaranteed
I guaranteed

Reputation: 299

'Exact' object has no attribute 'shap_values'

import shap
import pandas as pd
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor

dataset = fetch_california_housing(as_frame=True)
X = dataset["data"]
y = dataset["target"]
X_train, X_test, y_train, y_test = train_test_split(X, y,  test_size=0.2)

model = RandomForestRegressor()

model.fit(X_train, y_train)

explainer = shap.Explainer(model.predict, X_test)
shap_values = explainer(X_test)
shap_values = explainer.shap_values(X)
AttributeError: 'Exact' object has no attribute 'shap_values'

In order to know the SHAP values, even though I entered the above code, the above error occurs at the last line.

When I look at the example codes, it seems to work without any problem, but the SHAP library version is updated and the problem seems to have occurred.

What code can I use instead of .shap_values?

Upvotes: 2

Views: 2322

Answers (1)

Sergey Bushmanov
Sergey Bushmanov

Reputation: 25189

If you check out explainer object:

dir(explainer)

['__call__',
 '__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 '_cached_gray_codes',
 '_compute_main_effects',
 '_gray_code_cache',
 '_instantiated_load',
 'explain_row',
 'feature_names',
 'linearize_link',
 'link',
 'load',
 'masker',
 'model',
 'output_names',
 'save',
 'supports_model_with_masker']

you'll find out the object doesn't have shap_values method or attribute. Hence your error.

However, you already have shap_values object with everything you need:

shap_values

.values =
array([[-3.35735356e-01, -7.13048075e-02,  1.72043493e-02, ...,
         4.34819130e-01, -7.32259144e-01,  1.33657838e-01],
       [ 6.52537977e-01,  1.13007470e-01,  9.42159765e-03, ...,
         2.27540128e-01,  2.63606641e-01,  3.19787969e-02],
       [-5.21078654e-01,  1.27421958e-02, -6.97000846e-02, ...,
         6.47177548e-05,  7.54791715e-02,  1.12118219e-01],
       ...,
       [ 7.92578890e-02, -5.45558945e-03,  1.97615869e-02, ...,
        -2.96984576e-01,  1.09652257e-01, -6.13352645e-01],
       [-9.01863457e-02, -4.94473246e-02,  1.27294579e-01, ...,
         2.32800011e-02, -7.88081199e-01,  1.23530401e-01],
       [ 2.62943083e-01, -2.16743690e-02,  4.10964201e-02, ...,
        -3.23427676e-01,  3.72933299e-01, -6.50382792e-02]])

.base_values =
array([2.0767511, 2.0767511, 2.0767511, ..., 2.0767511, 2.0767511,
       2.0767511])

.data =
array([[   3.089     ,   18.        ,    3.99712644, ...,    1.61091954,
          38.58      , -121.41      ],
       [   5.1718    ,   31.        ,    5.67641682, ...,    2.48446069,
          34.16      , -118.23      ],
       [   2.6893    ,   26.        ,    5.35526316, ...,    2.59962406,
          34.45      , -119.29      ],
       ...,
       [   4.3676    ,   28.        ,    6.39285714, ...,    3.1047619 ,
          34.18      , -117.3       ],
       [   3.9722    ,   17.        ,   20.90078329, ...,    2.60835509,
          38.93      , -120.        ],
       [   5.0091    ,   33.        ,    5.88377193, ...,    3.33333333,
          33.77      , -118.02      ]])

Upvotes: 2

Related Questions