Skip to main content

Python wrapper for the R package shapr (via rpy2)

Project description

pyshapr

pyshapr is a Python wrapper for the R package shapr, using the rpy2 Python library to access R from within Python.

Renamed: This package was previously published as shaprpy. It has been renamed to pyshapr. The old shaprpy package remains available on PyPI for a transition period and simply forwards to pyshapr. Please switch to pip install pyshapr and import pyshapr.

Note: This wrapper is not as comprehensively tested as the R package. rpy2 has limited support on Windows, and the same therefore applies to pyshapr. pyshapr has only been tested on Linux (and WSL - Windows Subsystem for Linux), and the below instructions assume a Linux environment.

Requirement: Python 3.11 or later is required to use pyshapr.

Changelog

For a list of changes and updates to the pyshapr package, see the pyshapr CHANGELOG.


Installation

These instructions assume you already have pip and R installed and available to the Python environment in which you want to run pyshapr.

  • Official instructions for installing pip can be found here.
  • Official instructions for installing R can be found here.

On Debian/Ubuntu-based systems, R can also be installed via:

sudo apt update
sudo apt install r-base r-base-dev -y

1. Install the R package shapr

pyshapr requires the R package shapr (version 1.0.5 or newer). In your R environment, install the latest version from CRAN using:

Rscript -e 'install.packages("shapr", repos="https://cran.rstudio.com")'

2. Ensure R is discoverable (R_HOME and PATH)

Sometimes rpy2 (which pyshapr relies on) cannot automatically locate your R installation. To ensure proper detection, verify that:

  • R is available in your system PATH, or
  • The R_HOME environment variable is set to your R installation directory.

Example:

export R_HOME=$(R RHOME)
export PATH=$PATH:$(R RHOME)/bin

3. Install the Python wrapper

Install directly from PyPI with:

pip install pyshapr

Local development install (for contributors)

If you have cloned the repository and want to install in development mode for local changes, navigate to the ./python directory and run:

pip install -e .

The -e flag installs in editable mode, allowing local code changes to be reflected immediately.


Quick Demo

from sklearn.ensemble import RandomForestRegressor
from pyshapr import explain
from pyshapr.datasets import load_california_housing

# Load example data
dfx_train, dfx_explain, dfy_train, dfy_explain = 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_explain,
    approach="gaussian",
    phi0=dfy_train.mean().item(),
    seed=1
)

explanation.print() # Print the Shapley values

# Get a summary object with computation details
summary = explanation.summary()
print(summary)  # Displays a formatted summary (also available directly via explanation.summary())

# Access specific summary attributes (available with tab-completion in Jupyter)
summary['approach']     # Approach used
summary['timing_summary']['total_time_secs']  # Total computation time

# Extract one or more specific result objects directly
explanation.get_results("proglang") # Programming language used (Python/R)
explanation.get_results("approach") # Approach used
explanation.get_results().keys()  # All available result objects

# 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 observation

Supported Models

pyshapr can explain predictions from models built with:

For other model types, you can supply:

  • A custom predict_model function
  • (Optionally) a custom get_model_specs function to pyshapr.explain.

Supported Approaches

pyshapr forwards all approach-specific arguments to shapr::explain(). Commonly used approaches include:

  • "arf", "categorical", "copula", "ctree", "empirical", "gaussian", "regression_separate", "regression_surrogate", "vaeac"
  • "independence" (not recommended)

"arf", "ctree", "regression_separate", "regression_surrogate" and "vaeac" support mixed numerical/categorical feature sets, "categorical" supports categorical features only, while "copula", "empirical", "gaussian" and "independence" support numerical features only.


Examples

See the examples folder on GitHub for runnable examples, including:

  • Basic usage with scikit-learn models
  • Usage with xgboost models
  • Usage with keras models
  • A custom PyTorch model
  • Usage of the Shapr class and associated ShaprSummary class for exploration and extraction of explanation results.
  • Plotting functionality for the Shapley values through the shap package
  • ARF and VAEAC examples for both numerical and mixed categorical feature sets
  • 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyshapr-0.5.0.tar.gz (22.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyshapr-0.5.0-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

Details for the file pyshapr-0.5.0.tar.gz.

File metadata

  • Download URL: pyshapr-0.5.0.tar.gz
  • Upload date:
  • Size: 22.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for pyshapr-0.5.0.tar.gz
Algorithm Hash digest
SHA256 17a73087661d573fe9bbc44777ec423e4dbc925d261d56e24f4505e2d5a528db
MD5 706cd556160400fc283418288c8d6cc2
BLAKE2b-256 508dfb2c833b46c839e11aa033c4c6a8eb579d2397e2c6ca30f1882fd93ff070

See more details on using hashes here.

File details

Details for the file pyshapr-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: pyshapr-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 19.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for pyshapr-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 88588aae79dc1c917c0ac1072c8df5173ee615aac8a4208ed958facfc18958d0
MD5 abc63a258d102b54db22988c8b46a2d5
BLAKE2b-256 47666a68930b4ff259b22fa394612080b6e08f9eb318a99e75792a6198fd4b23

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page