Skip to main content

A library to calculate Shapley values for feature importance in machine learning models.

Project description

Shapley Calculator v0.2.1

A professional Python library to calculate Shapley values for feature importance in machine learning models. Designed for speed, flexibility, and high-quality visualizations in Jupyter Notebooks.

Key Features (v0.2.1)

  • 🚀 Parallel Processing: Built-in support for multi-core processing using concurrent.futures. Speed up calculations by up to 10x.
  • 🎯 Classification Support: Interpret model probabilities (predict_proba) for binary and multi-class classification.
  • 📊 Advanced Visualizations:
    • Summary Plot (Beeswarm): Distribution of feature impacts across the entire dataset.
    • Local Explanation: Detailed breakdown of individual predictions.
  • 📝 Automated Reporting: Rich text console reports for quick analysis in terminal or notebooks.
  • 🔌 Scikit-learn Compatible: Works with any model following the standard predict/predict_proba interface.

Installation

pip install shapley_calculator

Quick Start

from shapley_calculator.shapley import ShapleyValueCalculator
from sklearn.ensemble import RandomForestClassifier

# Initialize calculator (Parallel processing enabled by default)
calculator = ShapleyValueCalculator(
    model, X_test, 
    model_type='classification', 
    class_idx=1, 
    n_jobs=-1
)

# 1. Get a comprehensive text report
print(calculator.get_summary_report(feature_names=features))

# 2. Visualize global impact distribution
calculator.plot_summary(feature_names=features)

# 3. Analyze a specific prediction
calculator.plot_local(sample_idx=0, feature_names=features)

Advanced Usage

Parallel Execution

Control the number of CPU cores used for calculations:

calculator = ShapleyValueCalculator(model, X, n_jobs=4)

Normalization

Ensure Shapley values sum to 100% (or 1.0) for easier interpretation:

shap_values = calculator.get_shapley_values(normalize=True)

Technical Details

The library uses a sampling-based estimation algorithm for Shapley values, which provides a good balance between calculation speed and estimation accuracy.

  • Computational Complexity: O(Samples * Features * num_samples / n_jobs)
  • Normalization: Preserves signs while ensuring unit sum.

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

shapley_calculator-0.2.1.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

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

shapley_calculator-0.2.1-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file shapley_calculator-0.2.1.tar.gz.

File metadata

  • Download URL: shapley_calculator-0.2.1.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for shapley_calculator-0.2.1.tar.gz
Algorithm Hash digest
SHA256 40b09d229e4ff9e2dc4d1429bfaccaa702fe57121481c51e973cf06995553668
MD5 01fa1902b41219fe69d10e3b2d056bfd
BLAKE2b-256 3f6b57773e14125593057540d1e9bfbac211cc68f61db753eb8f8a72479e831b

See more details on using hashes here.

File details

Details for the file shapley_calculator-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for shapley_calculator-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 623d715078a74a5c75b926bb6b43717bec3d66f4389186d5f1cc6df68ad7d9a4
MD5 0e04b4b63290cdfeb24f54555eacf748
BLAKE2b-256 9028a027ee3b8d1ffd561b3c3fd8b1b932d7ccf86d61b0ac1587ebc95c6bcdad

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