Skip to main content

Python wrapper for the R package shapr (via rpy2)

Project description

shaprpy

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. rpy2 has limited support on Windows, and the same therefore applies to shaprpy. shaprpy has 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.

Changelog

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


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

shaprpy 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 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_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 shaprpy

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

Supported Models

shaprpy 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 shaprpy.explain.

Examples

See the /examples folder 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 for exploration of explanation results and plotting through the shap package
  • 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

shaprpy-0.4.0.tar.gz (20.4 kB view details)

Uploaded Source

Built Distribution

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

shaprpy-0.4.0-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

Details for the file shaprpy-0.4.0.tar.gz.

File metadata

  • Download URL: shaprpy-0.4.0.tar.gz
  • Upload date:
  • Size: 20.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for shaprpy-0.4.0.tar.gz
Algorithm Hash digest
SHA256 ab35285e601272095235b7b06bdc848a37eec43574cc15120e2a951876de4a36
MD5 21191fda1650e37476a0ef7d08307cc7
BLAKE2b-256 222204e0fbe4c7a2d44b157e1be2337dc77ae73cd7e489376e323c862b47c523

See more details on using hashes here.

File details

Details for the file shaprpy-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: shaprpy-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 18.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for shaprpy-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2165940f670bd11e3c953c95b9843717e7f094c55f7cd52a1fa3580bd61966fc
MD5 1c10059291aa30f514524235523c7c03
BLAKE2b-256 d1dda15629243757fd7f80ee0413deba622e0d9ed4b5d28f9d8d9cf9d330c9a0

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