Skip to main content

Functions to convert XAI like LIME & SHAP Weights into Dictionary Object for easy CSV manipulation.

Project description

About The Project

XaiCSV is a collection of functions to convert XAI (eXplainable AI) like SHAP & LIME into Dictionary Object. The resulting Dictionary is used for easy CSV manipulation such as using Pandas. This package also includes visualization for the Dictionary. Example of Dictionary result :

 {
 "class1": [
     ("feature1",weights1),
     ("feature2",weights2)
 ],
 "class2": [...]
 }

Getting Started

Examples can be found in docs and results folder

  1. Tabular Regression & Classification for LIME & SHAP
  2. Text Classification for LIME

Prerequisites

Python >=3.9 Required

Installation

pip install xaicsv

Usage

  1. Use lime_values_to_weights_dict to get Dictionary of LIME Weights.
  2. Use shap_values_to_weights_dict to get Dictionary of SHAP Weights.
  3. Use weights_dict_to_pyplot to visualize Dictionary values.

Import the xai_csv_utils

 from xaicsv import xai_csv_utils

LIME

  1. lime_explanation . LIME Explanation Object.
  2. class_names . list of classes . set ["target"] if Regression.

Using iris dataset for classification task and using SVC model.

 list_labels = iris.target_names.tolist()
 list_index_labels= list(range(0, len(list_labels)))

 lime_weights = []
 for i in range(len(df_test)):
 exp = explainer.explain_instance(df_test.iloc[0].values,
                                 svc_iris.predict_proba,
                                 num_features=len(iris.feature_names),
                                 labels= list_index_labels)

 weight_val = xai_csv_utils.lime_values_to_weights_dict(
                                         lime_explanation=exp,
                                         class_names=list_labels)
 lime_weights.append(weight_val)

Add the resulting Dictionary object into a Dataframe

 df['lime_weights'] = lime_weights

SHAP

  1. classification . set True if Classification Task. set False if Regression Task.
  2. shap_values . SHAP values of one sample.
  3. class_names . list of classes . set ["target"] if Regression.
  4. feature_names . list of feature names.

Using iris dataset for classification task and using SVC model.

 explainer_shap = shap.KernelExplainer(svc_iris.predict_proba,
                                     train,
                                     feature_names = iris.feature_names)
 shap_values = explainer_shap.shap_values(test)

 list_labels = iris.target_names.tolist()

 shap_weights = []
 for i in range(len(shap_values)):
 weight_val = shap_values_to_weights_dict(class_names=list_labels,
                             classification=True,
                             shap_values=shap_values[i],
                             feature_names=explainer_shap.data_feature_names)

 shap_weights.append(weight_val)

Add the resulting Dictionary object into a Dataframe

 df['shap_weights'] = shap_weights

Weights Visualization

Add the resulting Dictionary object into a Dataframe

 sample_weights = df.iloc[25]["shap_weights"]
 for label in (list_labels):
     fig = weights_dict_to_pyplot(sample_weights, label)

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

xaicsv-0.1.1.tar.gz (703.7 kB view details)

Uploaded Source

Built Distribution

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

xaicsv-0.1.1-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file xaicsv-0.1.1.tar.gz.

File metadata

  • Download URL: xaicsv-0.1.1.tar.gz
  • Upload date:
  • Size: 703.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for xaicsv-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a179cf6b0d4cc90c75ea8e1e029a56ec823a98e61bb6ee0a5ca55802d7150a46
MD5 46df612830c38205d7bec39e0a010561
BLAKE2b-256 104963b282c56d434f067ef7048a47274f0392324c1af6e5dcfd73083d7b4dac

See more details on using hashes here.

File details

Details for the file xaicsv-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: xaicsv-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for xaicsv-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cd7d1d46e509a7e0bccc0d8723d9526859cb531168f949bf13abd7480243316b
MD5 9a557b9e23f7d9928755257cda74fec7
BLAKE2b-256 c926cb3d30507351c3c5a475360d0c464555882e6a25d93e3291fc284cf52bc7

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