Convert Figma logo to code with AI

EthicalML logoxai

XAI - An eXplainability toolbox for machine learning

1,097
168
1,097
4

Top Related Projects

Fit interpretable models. Explain blackbox machine learning.

22,455

A game theoretic approach to explain the output of any machine learning model.

11,502

Lime: Explaining the predictions of any machine learning classifier

2,371

Algorithms for explaining machine learning models

Model analysis tools for TensorFlow

Quick Overview

EthicalML/xai is an open-source Python library for explainable AI and machine learning. It provides a set of tools and algorithms to help data scientists and machine learning practitioners interpret and explain their models, focusing on fairness, accountability, and transparency in AI systems.

Pros

  • Comprehensive suite of explainability techniques for various ML models
  • Easy integration with popular ML frameworks like scikit-learn and TensorFlow
  • Actively maintained with regular updates and community support
  • Includes visualization tools for better interpretation of results

Cons

  • Learning curve may be steep for beginners in explainable AI
  • Some advanced features may require additional dependencies
  • Documentation could be more extensive for certain modules
  • Performance may be slower for very large datasets or complex models

Code Examples

  1. Creating a model-agnostic explainer:
from xai import Explainer

# Assuming 'model' is your trained ML model and 'X' is your feature dataset
explainer = Explainer(model, X)
explanation = explainer.explain_instance(X[0])
  1. Generating a feature importance plot:
import matplotlib.pyplot as plt
from xai.plots import feature_importance_plot

# Assuming 'explanation' is the result from the previous example
feature_importance_plot(explanation)
plt.show()
  1. Calculating fairness metrics:
from xai.fairness import demographic_parity

# Assuming 'y_true' are true labels, 'y_pred' are predicted labels,
# and 'protected_attribute' is a binary protected attribute
dp_score = demographic_parity(y_true, y_pred, protected_attribute)
print(f"Demographic Parity Score: {dp_score}")

Getting Started

To get started with EthicalML/xai, follow these steps:

  1. Install the library:
pip install ethical-xai
  1. Import the necessary modules:
from xai import Explainer
from xai.plots import feature_importance_plot
from xai.fairness import demographic_parity
  1. Create an explainer for your model:
explainer = Explainer(your_model, X_train)
  1. Generate explanations and visualizations:
explanation = explainer.explain_instance(X_test[0])
feature_importance_plot(explanation)

Competitor Comparisons

Fit interpretable models. Explain blackbox machine learning.

Pros of interpret

  • More comprehensive set of interpretability techniques, including SHAP, LIME, and EBM
  • Better documentation and tutorials for getting started
  • Actively maintained with frequent updates and contributions

Cons of interpret

  • Steeper learning curve due to more complex API
  • Primarily focused on tabular data, less support for other data types
  • Larger package size and more dependencies

Code Comparison

interpret:

from interpret import set_visualize_provider
from interpret.provider import InlineProvider
set_visualize_provider(InlineProvider())

from interpret.glassbox import ExplainableBoostingClassifier
ebm = ExplainableBoostingClassifier()
ebm.fit(X_train, y_train)

ebm_global = ebm.explain_global()
ebm_global.visualize()

xai:

from xai.explainer import TabularExplainer

explainer = TabularExplainer(model, X_train, feature_names=feature_names)
shap_values = explainer.shap_values(X_test)

explainer.plot_summary(shap_values)

Both libraries offer explainable AI capabilities, but interpret provides a wider range of techniques and better documentation. xai has a simpler API and focuses more on specific use cases. The choice between them depends on the complexity of your project and the specific interpretability needs.

22,455

A game theoretic approach to explain the output of any machine learning model.

Pros of shap

  • More comprehensive and widely adopted for model interpretability
  • Offers a broader range of visualization tools for SHAP values
  • Supports a wider variety of machine learning models and frameworks

Cons of shap

  • Steeper learning curve for beginners
  • Can be computationally intensive for large datasets or complex models
  • Less focus on ethical considerations and bias detection

Code Comparison

xai example:

from xai.explainer import TabularExplainer

explainer = TabularExplainer(model, X_train)
shap_values = explainer.shap_values(X_test)

shap example:

import shap

explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X)
shap.summary_plot(shap_values, X)

Summary

While xai focuses on ethical AI and bias detection, shap provides a more comprehensive toolkit for model interpretability across various ML frameworks. xai is more beginner-friendly and emphasizes ethical considerations, whereas shap offers advanced visualization tools and supports a wider range of models. The choice between the two depends on the specific needs of the project and the user's expertise level.

11,502

Lime: Explaining the predictions of any machine learning classifier

Pros of LIME

  • More focused on a specific explainability technique (Local Interpretable Model-agnostic Explanations)
  • Widely adopted and well-established in the XAI community
  • Extensive documentation and examples for various use cases

Cons of LIME

  • Limited to a single explainability method
  • May require more setup and configuration for complex models
  • Less comprehensive in terms of overall AI ethics and governance

Code Comparison

LIME example:

from lime import lime_tabular
explainer = lime_tabular.LimeTabularExplainer(X_train)
exp = explainer.explain_instance(X_test[0], clf.predict_proba)

XAI example:

from xai.explainer import TabularExplainer
explainer = TabularExplainer(model, X_train)
explanation = explainer.explain(X_test[0])

Summary

LIME is a specialized tool focusing on a specific explainability technique, while XAI offers a broader range of explainability methods and AI ethics features. LIME is more established and widely used, but XAI provides a more comprehensive approach to responsible AI development. The choice between the two depends on the specific needs of the project and the desired level of ethical considerations in AI development.

2,371

Algorithms for explaining machine learning models

Pros of Alibi

  • More comprehensive and actively maintained library with a wider range of explainability methods
  • Better documentation and examples, including Jupyter notebooks
  • Supports both TensorFlow and PyTorch models

Cons of Alibi

  • Steeper learning curve due to more complex API
  • Heavier dependencies, which may increase project size
  • Slower execution for some methods compared to xai

Code Comparison

Alibi example:

from alibi.explainers import AnchorTabular
explainer = AnchorTabular(predict_fn, feature_names)
explanation = explainer.explain(X)

xai example:

from xai.explainer import TabularExplainer
explainer = TabularExplainer(model, X_train)
shap_values = explainer.shap_values(X_test)

Both libraries offer explainability methods for machine learning models, but Alibi provides a more extensive set of tools and better documentation. xai is simpler to use and has lighter dependencies, making it suitable for smaller projects or quick prototyping. Alibi is better suited for more complex use cases and production environments where a wider range of explainability techniques is required.

Model analysis tools for TensorFlow

Pros of model-analysis

  • Comprehensive suite of tools for evaluating TensorFlow models
  • Seamless integration with TensorFlow ecosystem
  • Supports large-scale, production-ready model analysis

Cons of model-analysis

  • Limited to TensorFlow models
  • Steeper learning curve for users not familiar with TensorFlow
  • Less focus on ethical considerations compared to xai

Code Comparison

xai:

from xai.explainer import TabularExplainer

explainer = TabularExplainer(model, X_train)
shap_values = explainer.shap_values(X_test)

model-analysis:

import tensorflow_model_analysis as tfma

eval_config = tfma.EvalConfig(model_specs=[tfma.ModelSpec()])
eval_result = tfma.run_model_analysis(eval_config, eval_shared_model)

Summary

xai focuses on explainable AI and ethical considerations across various ML frameworks, while model-analysis provides in-depth analysis tools specifically for TensorFlow models. xai offers a more accessible approach for users working with different ML libraries, whereas model-analysis excels in TensorFlow-specific, production-scale model evaluation.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

GitHub GitHub GitHub GitHub

XAI - An eXplainability toolbox for machine learning

XAI is a Machine Learning library that is designed with AI explainability in its core. XAI contains various tools that enable for analysis and evaluation of data and models. The XAI library is maintained by The Institute for Ethical AI & ML, and it was developed based on the 8 principles for Responsible Machine Learning.

You can find the documentation at https://ethicalml.github.io/xai/index.html. You can also check out our talk at Tensorflow London where the idea was first conceived - the talk also contains an insight on the definitions and principles in this library.

YouTube video showing how to use XAI to mitigate undesired biases

This video of the talk presented at the PyData London 2019 Conference which provides an overview on the motivations for machine learning explainability as well as techniques to introduce explainability and mitigate undesired biases using the XAI Library.
Do you want to learn about more awesome machine learning explainability tools? Check out our community-built "Awesome Machine Learning Production & Operations" list which contains an extensive list of tools for explainability, privacy, orchestration and beyond.

0.1.0

If you want to see a fully functional demo in action clone this repo and run the Example Jupyter Notebook in the Examples folder.

What do we mean by eXplainable AI?

We see the challenge of explainability as more than just an algorithmic challenge, which requires a combination of data science best practices with domain-specific knowledge. The XAI library is designed to empower machine learning engineers and relevant domain experts to analyse the end-to-end solution and identify discrepancies that may result in sub-optimal performance relative to the objectives required. More broadly, the XAI library is designed using the 3-steps of explainable machine learning, which involve 1) data analysis, 2) model evaluation, and 3) production monitoring.

We provide a visual overview of these three steps mentioned above in this diagram:

XAI Quickstart

Installation

The XAI package is on PyPI. To install you can run:

pip install xai

Alternatively you can install from source by cloning the repo and running:

python setup.py install 

Usage

You can find example usage in the examples folder.

1) Data Analysis

With XAI you can identify imbalances in the data. For this, we will load the census dataset from the XAI library.

import xai.data
df = xai.data.load_census()
df.head()

View class imbalances for all categories of one column

ims = xai.imbalance_plot(df, "gender")

View imbalances for all categories across multiple columns

im = xai.imbalance_plot(df, "gender", "loan")

Balance classes using upsampling and/or downsampling

bal_df = xai.balance(df, "gender", "loan", upsample=0.8)

Perform custom operations on groups

groups = xai.group_by_columns(df, ["gender", "loan"])
for group, group_df in groups:    
    print(group) 
    print(group_df["loan"].head(), "\n")

Visualise correlations as a matrix

_ = xai.correlations(df, include_categorical=True, plot_type="matrix")

Visualise correlations as a hierarchical dendogram

_ = xai.correlations(df, include_categorical=True)

Create a balanced validation and training split dataset

# Balanced train-test split with minimum 300 examples of 
#     the cross of the target y and the column gender
x_train, y_train, x_test, y_test, train_idx, test_idx = \
    xai.balanced_train_test_split(
            x, y, "gender", 
            min_per_group=300,
            max_per_group=300,
            categorical_cols=categorical_cols)

x_train_display = bal_df[train_idx]
x_test_display = bal_df[test_idx]

print("Total number of examples: ", x_test.shape[0])

df_test = x_test_display.copy()
df_test["loan"] = y_test

_= xai.imbalance_plot(df_test, "gender", "loan", categorical_cols=categorical_cols)

2) Model Evaluation

We are able to also analyse the interaction between inference results and input features. For this, we will train a single layer deep learning model.

model = build_model(proc_df.drop("loan", axis=1))

model.fit(f_in(x_train), y_train, epochs=50, batch_size=512)

probabilities = model.predict(f_in(x_test))
predictions = list((probabilities >= 0.5).astype(int).T[0])

Visualise permutation feature importance

def get_avg(x, y):
    return model.evaluate(f_in(x), y, verbose=0)[1]

imp = xai.feature_importance(x_test, y_test, get_avg)

imp.head()

Identify metric imbalances against all test data

_= xai.metrics_plot(
        y_test, 
        probabilities)

Identify metric imbalances across a specific column

_ = xai.metrics_plot(
    y_test, 
    probabilities, 
    df=x_test_display, 
    cross_cols=["gender"],
    categorical_cols=categorical_cols)

Identify metric imbalances across multiple columns

_ = xai.metrics_plot(
    y_test, 
    probabilities, 
    df=x_test_display, 
    cross_cols=["gender", "ethnicity"],
    categorical_cols=categorical_cols)

Draw confusion matrix

xai.confusion_matrix_plot(y_test, pred)

Visualise the ROC curve against all test data

_ = xai.roc_plot(y_test, probabilities)

Visualise the ROC curves grouped by a protected column

protected = ["gender", "ethnicity", "age"]
_ = [xai.roc_plot(
    y_test, 
    probabilities, 
    df=x_test_display, 
    cross_cols=[p],
    categorical_cols=categorical_cols) for p in protected]

Visualise accuracy grouped by probability buckets

d = xai.smile_imbalance(
    y_test, 
    probabilities)

Visualise statistical metrics grouped by probability buckets

d = xai.smile_imbalance(
    y_test, 
    probabilities,
    display_breakdown=True)

Visualise benefits of adding manual review on probability thresholds

d = xai.smile_imbalance(
    y_test, 
    probabilities,
    bins=9,
    threshold=0.75,
    manual_review=0.375,
    display_breakdown=False)