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

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 from 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.0.tar.gz (3.3 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.0-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: xaicsv-0.1.0.tar.gz
  • Upload date:
  • Size: 3.3 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.0.tar.gz
Algorithm Hash digest
SHA256 7f41a2d0bcc943f4a117a565b1da2a1dead876337797ca897e376af0289c916a
MD5 647ca40dffe1471a6b2335e7ee841bfe
BLAKE2b-256 4bb008395b83b83ba7eb650553567180fbe7abfbf28c5d721c908d92b0d9098f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: xaicsv-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.8 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b43492582968e2822fba95638f1a3c2185176e07652f0d51b9ba2b9b64840eb3
MD5 9db3f06cad588272bf3a811135ca28bd
BLAKE2b-256 e34dc23937d9d509099ac1ea6787ff6482e72c4f03a77e65f7d6a6f1eda61765

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