[Python] Getting Error that says matplotlib is not installed when using Shap.waterfall function

Discussão em 'Python' iniciado por Stack, Maio 19, 2025.

  1. Stack

    Stack Membro Participativo

    So this is my code, i want to Waterfallplot, but i get the error following my code, i tried using pip install in my terminal in powershell or installing via the liabarie tool in pyplot, nothing seems to work, why might this be and how do i fix it? I am using shap version 0.47.2 and matplotlib version 3.10.2

    !pip install shap
    !pip install matplotlib

    import pandas as pd
    import numpy as np
    import xgboost as xgb
    import shap
    from sklearn.model_selection import train_test_split
    from sklearn.datasets import fetch_california_housing
    from xgboost.core import Objective
    import matplotlib.pyplot as plt

    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, random_state=42)

    model = xgb.XGBRegressor(Objective = 'reg:squarederror',random_state=42)
    model.fit(X_train, Y_train)

    explainer = shap.Explainer(model)
    shap_values = explainer(X_test)
    #%%
    shap.plots.waterfall(shap_values[0])
    plt.show()




    ImportError Traceback (most recent call last)
    Cell In[67], line 1
    ----> 1 shap.plots.waterfall(shap_values[0])
    2 plt.show()

    File ~\PycharmProjects\XAI notebook\.venv\Lib\site-packages\shap\__init__.py:37, in UnsupportedModule.__getattribute__(self, item)
    36 def __getattribute__(self, item):
    ---> 37 raise ImportError(_no_matplotlib_warning)

    ImportError: matplotlib is not installed so plotting is not available! Run `pip install matplotlib` to fix this.

    Continue reading...

Compartilhe esta Página