Skip to main content

ConteXAI - Contextualizing model's decisions with natural language explanations

The library ConteXAI links local explainability methods with natural language explanations, allowing users to gain detailed insights into model predictions. By incorporating additional domain knowledge, users can obtain even better insights into the model's predictions.

Install

ConteXAI can be installed using pip:

pip install dscontexai

Generate XAI reports

Using this library, you can fully automatically develop your XAI reports. Currently, it supports SHAP values, which is a local feature importance measure, to which we added textual explanations and domain-related context. The library can be used for binary classification problems, for all models that output probabilities.

The report consists of four main segments:

(1) Description of local feature importances,

(2) Explanation of the instance's prediction,

(3) Visualization of local feature importances,

(4) Explanation of the visualization in natural language and context related to the domain and the prediction.

Report structure

Preparing configuration file

To use this tool with your model and dataset, you need to provide a configuration JSON file for a dataset, structured like the one below.

{
  # domain-based context
  "optimal_values": [
  ...
    [0, 6.5],               # HbA1c level optimal range is from 0 to 6.5
    [100, 140]              # Glucose level optimal range is from 100 to 140 
  ],
  "descriptions": {
    "below_optimal": [
      # Context for feature values falling below the optimal range
    ],
    "optimal": [
      # Context for feature values falling within the optimal range
    ],
    "above_optimal": [
      # Context for feature values falling above the optimal range
    ]
  },
  # Data transformations for feature mapping
  "transformation": {
    "0": ["Man", "Woman"],  # Transformation for Gender
    "1": [],                # No transformation for Age (numeric)
    "2": ["No", "Yes"],     # Transformation for Hypertension
    ...
  },
  "feature_names": [
    "Gender",
    "Age",
    "Hypertension",
    "Heart_disease",
    "Smoking_history",
    "BMI",
    "HbA1c_level",
    "Blood_glucose_level"
  ],
  # variables needed for structuring the description
  "target1": "diabetes",    # Target variable the model is predicting
  "target2": "Person",      # The object representing a sample in a data
  "supporting": ["does", "have"]  # Supporting verbs used in descriptions
}

Running the generation

Once you have model, dataset, and config file ready, to generate the report, you can use the following code snippet:

from dscontexai.generate_report import general

general.generate_report(model_path= "path/to/model.pkl", dataset_path="path/to/data.csv", config="path/to/config.json", idx=sample_idx)

After successful generation, you will find a PDF report in the directory prototype/ under the name output_{sample_idx}.pdf.

Example notebooks

Working example, as well as the example of the generated report and the configuration file, can be found here.

Examples

Examples of the generated reports are shown below. Problem domain was diabetes prediction.

Example 1 Example 2

Metadata

Release files for dscontexai 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dscontexai 1.1.0
File Size Uploaded
dscontexai-1.1.0.tar.gz 15.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dscontexai 1.1.0
File Interpreter ABI Platform
dscontexai-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 32.6 kB

Release files / dscontexai-1.1.0.tar.gz

Download URL dscontexai-1.1.0.tar.gz
Size 15.3 kB
Tags Source
SHA-256 checksum
How to use checksums
80b6fbf5287b81f4da41e1c5eaa9613431473031e5b251c9fd4a82b5f5492ffb
BLAKE2b-256 checksum
How to use checksums
9488c390cdc3f944385ce6e4314bae7034cdd48bd29b443621d50e6e91df5af1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.8.10

Release files / dscontexai-1.1.0-py3-none-any.whl

Download URL dscontexai-1.1.0-py3-none-any.whl
Size 17.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cd267f0d8e9a463729a0b5aa68e0475c418f8d4544d7e89063011d7529de85e6
BLAKE2b-256 checksum
How to use checksums
d36df656d0f980f9a87edf2ea0c14bbe21c1aa80310a622ffd0c21ae01465dcc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.8.10

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.1.1

2 release files

0.0.13

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page