Skip to main content

Exact computation of Shapley R-squared for tree ensembles in polynomial time

Project description

Q-SHAP: Feature-Specific $R^2$ Values for Tree Ensembles

PyPI Downloads

This package is used to compute feature-specific $R^2$ values, following Shapley decomposition of the total $R^2$, for tree ensembles in polynomial time based on the paper.

This version only takes outputs from XGBoost, LightGBM, scikit-learn Decision Tree, and scikit-learn GBDT. We are working to update it for random forests in the next version. Please check Q-SHAP Tutorial for more details using Q-SHAP.

Installation

qshap can be installed through PyPI:

pip install qshap

Q-SHAP uses a compiled C++ backend for the core second-order tree calculation when available. To force the original numba implementation for comparison or debugging, pass backend="numba" to gazer.loss() or gazer.rsq().

Quick Start

# Import necessary libraries
from ISLP import load_data
from qshap import gazer, vis
import xgboost as xgb
import numpy as np

boston = load_data("Boston")

# ---- Load Boston Housing from ISLP ----
y = boston["medv"].to_numpy(dtype=np.float64)

# Features = everything except medv
X_df = boston.drop(columns=["medv"])
x = X_df.to_numpy(dtype=np.float64)

feature_names = X_df.columns.to_numpy()

# ---- Fit a XGBoost regressor ----
model = xgb.XGBRegressor(
    max_depth=2,
    n_estimators=50,
    random_state=42,
    learning_rate=0.1,
).fit(x, y)

# ---- Obtain feature-specific R^2 using qshap ----
g = gazer(model)

phi_rsq = g.rsq(x, y)


# ---- Visualize top feature-specific R^2 ----
vis.rsq(
    phi_rsq,
    label=feature_names,
    rotation=30,
    save_name="boston_housing",
    color_map_name="Pastel2"
)

Citation

@inproceedings{10.5555/3762387.3762469,
author = {Jiang, Zhongli and Zhang, Min and Zhang, Dabao},
title = {Fast calculation of feature contributions in boosting trees},
year = {2025},
publisher = {JMLR.org},
numpages = {17},
location = {Rio de Janeiro, Brazil},
series = {UAI '25}
}

Reference

  • Jiang, Z., Zhang, M., & Zhang, D. (2025). Fast calculation of feature contributions in boosting trees. Proceedings of the 41st Conference on Uncertainty in Artificial Intelligence (UAI), 82:1859 - 1875

Container Images

We provide pre-built images, available for both Docker and Singularity, with all necessary packages for Q-SHAP in Python 3.12:

  • Docker:
    You can pull the Docker image using the following command:
    docker pull catstat/xai
    
  • Singularity:
    You can pull the Docker image using the following command:
    singularity pull docker://catstat/xai:0.1
    

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

qshap-0.3.7-cp312-cp312-macosx_11_0_arm64.whl (44.0 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

Details for the file qshap-0.3.7-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for qshap-0.3.7-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 95d3dc086b5339dc4bd2cda97a3d08c89e60bee8397fddb5f6f38099bba67ae8
MD5 e8679439c83f90d8ffb22521b2fad525
BLAKE2b-256 fd888d59340ac0756d427776ebbcd76e6570bfc91dde842237c0a60f52490b3c

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