shaprpy
Source:.github/shaprpy.md
shaprpy is a Python wrapper for the R package shapr, using the rpy2 Python library to access R from within Python.
Note: This wrapper is not as comprehensively tested as the R package.
rpy2has limited support on Windows, and the same therefore applies toshaprpy.shaprpyhas only been tested on Linux (and WSL - Windows Subsystem for Linux), and the below instructions assume a Linux environment.Requirement: Python 3.10 or later is required to use
shaprpy.
Installation
These instructions assume you already have pip and R installed and available to the Python environment in which you want to run shaprpy.
- Official instructions for installing
pipcan be found here. - Official instructions for installing R can be found here.
On Debian/Ubuntu-based systems, R can also be installed via:
1. Install the R package shapr
shaprpy requires the R package shapr (version 1.0.5 or newer). In your R environment, install the latest version from CRAN using:
2. Ensure R is discoverable (R_HOME and PATH)
Sometimes rpy2 (which shaprpy relies on) cannot automatically locate your R installation. To ensure proper detection, verify that:
- R is available in your system
PATH, or - The
R_HOMEenvironment variable is set to your R installation directory.
Example:
Quick Demo
from sklearn.ensemble import RandomForestRegressor
from shaprpy import explain
from shaprpy.datasets import load_california_housing
# Load example data
dfx_train, dfx_test, dfy_train, dfy_test = load_california_housing()
# Fit a model
model = RandomForestRegressor()
model.fit(dfx_train, dfy_train.values.flatten())
# Explain predictions
explanation = explain(
model=model,
x_train=dfx_train,
x_explain=dfx_test,
approach="empirical",
phi0=dfy_train.mean().item(),
seed=1
)
explanation.print() # Print the Shapley values
explanation.summary() # Gives a nicely formatted summary of the computation of the explanations
# Extract results as a dictionary
results = explanation.get_results()
shapley_values = results["shapley_est"]
# Plotting (requires the 'shap' library)
# Convert to a SHAP Explanation object
shap_exp = explanation.to_shap()
import shap
shap.plots.waterfall(shap_exp[0]) # Plot the first observationSupported Models
shaprpy can explain predictions from models built with:
scikit-learn-
keras(Sequential API) xgboost
For other model types, you can supply:
- A custom
predict_modelfunction - (Optionally) a custom
get_model_specsfunction toshaprpy.explain.
Examples
See the /examples folder for runnable examples, including:
- Basic usage with
scikit-learnmodels - Usage with
xgboostmodels - Usage with
kerasmodels - A custom PyTorch model
- Usage of the
Shaprclass for exploration of explanation results and plotting through theshappackage - The regression paradigm described in Olsen et al. (2024), which shows:
- How to specify the regression model
- How to enable automatic cross-validation of hyperparameters
- How to apply pre-processing steps before fitting regression models