Skip to main content

Contextualizing model's decisions with natural language explanations.

Project description

ConteXAI - Contextualizing model's decisions with natural language explanations

ConteXAI is a Python package developed as part of the Data Science Project Competition 2024.

The library 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

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

dscontexai-1.0.4.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

dscontexai-1.0.4-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file dscontexai-1.0.4.tar.gz.

File metadata

  • Download URL: dscontexai-1.0.4.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.8.10

File hashes

Hashes for dscontexai-1.0.4.tar.gz
Algorithm Hash digest
SHA256 1d571b347a671e8e07fe0081e53c6c8692f2b3810b1c99bfae1d7708f99f8eda
MD5 163cd20340a984b740908bb579ccede8
BLAKE2b-256 3325ab5b96083997b98b87674edd15e40da25f7625aaf320404fcc5e1543a3fd

See more details on using hashes here.

File details

Details for the file dscontexai-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: dscontexai-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.8.10

File hashes

Hashes for dscontexai-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 90a471fca20cf4ec15e776c9557d40e78c758b9b140c1ed54dd5775561494a54
MD5 23c42ac810798c9d43b7778f77d7e685
BLAKE2b-256 b1d417f1d49e93f9ebb85d1d8e24349afd0e78fde8a6e73dbca4a60fef910e2e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page