Skip to main content

An innovative approach to improve the interpretability of the SHAP method for sequential multi-class classification.

Project description

Sequential SHAP

The SHAP (Shapley Additive exPlanations) method has established itself as a powerful tool in Explainable Artificial Intelligence (XAI). However, its classical approach is often restricted to revealing only positive and negative contributions for a single class, which can limit interpretabilty in sequential multi-class classification problems.

To overcome these constraints, Sequential SHAP proposes a modified alternative approach. This novel method elucidates the model's decision-making mechanism in a more granular manner by categorizing feature effects into distinct semantic groups.

The Sequential Solution

Sequential SHAP addresses standard SHAP restrictions by breaking down the multi-class problem into two distinct binary classification stages based on a provided semantic class hierarchy (order). It categorizes features into three distinct groups:

  • UPPER (Red): Features driving the prediction towards a higher class.
  • LOWER (Blue): Features driving the prediction towards a lower class.
  • AMBIGUOUS (Gray): Features demonstrating inconsistent or uninformative effects across class transitions.

The findings obtained from analyses—such as on an obesity dataset—indicate that this proposed approach offers more profound insights than standard SHAP analysis and enables precise interpretations regarding class transitions. In conclusion, this modification significantly enhances the transparency of the model, presenting a robust analytical framework for complex hierarchical classification scenarios where interpretability is critically important.

Installation

This package intentionally does not auto-install external libraries to avoid altering your environment. Therefore, before installing this package, please ensure the following prerequisite libraries are installed:

pip install numpy pandas matplotlib scikit-learn shap

The library has been developed and tested with the following package versions:

  • pandas == 3.0.1
  • numpy == 2.4.2
  • matplotlib == 3.10.8
  • scikit-learn == 1.8.0
  • shap == 0.51.0

Afterward, you can install the Sequential SHAP package via pip:

pip install sequential-shap-explainer

Note: The current version is optimized for tree-based models (e.g. Random Forest) and uses shap.TreeExplainer under the hood.

Usage Example

from sequential_shap import SequentialSHAP

# 1. Initialize the explainer with your trained model, training data, and the semantic order of your classes
# Example: class_order=['Low', 'Medium', 'High']
explainer = SequentialSHAP(
    model=your_trained_model,
    X_train=X_train,
    y_train=y_train,
    class_order=['Low_Level', 'Medium_Level', 'High_Level'] 
)

# 2. Explain a specific instance by its index in the dataset
results_df = explainer.explain_by_index(index=42)

# 3. View the summarized DataFrame
print(results_df)

# 4. Plot the Sequential SHAP chart (Default behavior)
explainer.plot()

# 5. Plot BOTH the Classical SHAP Waterfall and Sequential SHAP side-by-side
# explainer.plot(show_classical=True)

Technical Features

  • Automatic String Label Conversion: Safely maps string class labels to internal integers.
  • Custom DataFrame Output: Embeds the primary prediction into the DataFrame string representation for cleaner console readability.
  • Intuitive Visualization: Automatically sorts and colors features based on their true directional impact on sequential classes.

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

sequential_shap_explainer-0.1.4.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

sequential_shap_explainer-0.1.4-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file sequential_shap_explainer-0.1.4.tar.gz.

File metadata

File hashes

Hashes for sequential_shap_explainer-0.1.4.tar.gz
Algorithm Hash digest
SHA256 d4c11e31152f020fe747fd9fb004b9b98c287b72fe2ca348720ffb48d403ce70
MD5 1d71bfe6a6794a04752285a8207dce69
BLAKE2b-256 986c70c2c47258e67bd621d7827169bc15fda32164bf6bcbe7bcde3e293d33d0

See more details on using hashes here.

File details

Details for the file sequential_shap_explainer-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for sequential_shap_explainer-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ac166969500fbcb355fee444b7cb48bbce33f29b6e2a2817f97ecbdfe0597253
MD5 c6dd4cb07fb29f571983a0a08bce6098
BLAKE2b-256 8cc8525411e0d2bc053445cd7ac3b3100e9d014eaacf7cfa99ff9b6d41a5ddec

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