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 supports XGBoost, LightGBM, CatBoost, scikit-learn Decision Tree, and scikit-learn GBDT regression models. 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

Install the model libraries you need with optional extras:

pip install "qshap[xgboost]"
pip install "qshap[lightgbm]"
pip install "qshap[catboost]"

# Or install all supported boosting backends:
pip install "qshap[all]"

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().

Imports

Use the import that matches the model backend you fit:

import numpy as np
from qshap import gazer, vis

# Pick one or more model libraries:
import xgboost as xgb
import lightgbm as lgb
import catboost as cb

Quick Start with XGBoost

from ISLP import load_data
import numpy as np
import xgboost as xgb
from qshap import gazer, vis

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

The same gazer(model).rsq(x, y) call works for LightGBM:

import lightgbm as lgb
from qshap import gazer

model = lgb.LGBMRegressor(
    max_depth=2,
    n_estimators=50,
    learning_rate=0.1,
    verbose=-1,
    random_state=42,
).fit(x, y)

phi_rsq = gazer(model).rsq(x, y)

And for CatBoost:

import catboost as cb
from qshap import gazer

model = cb.CatBoostRegressor(
    depth=2,
    iterations=50,
    learning_rate=0.1,
    loss_function="RMSE",
    verbose=False,
    allow_writing_files=False,
    random_seed=42,
).fit(x, y)

phi_rsq = gazer(model).rsq(x, y)

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 Distribution

qshap-0.3.8.tar.gz (31.7 kB view details)

Uploaded Source

Built Distribution

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

qshap-0.3.8-cp312-cp312-macosx_11_0_arm64.whl (45.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

Details for the file qshap-0.3.8.tar.gz.

File metadata

  • Download URL: qshap-0.3.8.tar.gz
  • Upload date:
  • Size: 31.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for qshap-0.3.8.tar.gz
Algorithm Hash digest
SHA256 8650c550dcddc49f27d3cd56cc265c00d865046a896ffe5c7852ee510b484cc6
MD5 c2e062e07cdf4952fd214352d32e36af
BLAKE2b-256 f0798a4275c392adde9f44408474b6c22c0a5fdcba8bf9168bde500370011a23

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for qshap-0.3.8-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 de5b276d5beb7fc25d5e2d0d97cea3381fbfbdcd5841737cc73c4f9fa79460a7
MD5 425a601f57c8b517eda1ce89ac1321f4
BLAKE2b-256 35ae9a76419ce1443c7ba2446f8cbea8d5c137e18ac7b0e91cea9b7536ecf220

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