Skip to content

Example-based explainability

WoodTapper example-based explainability modules enables seamless integration of interpretable rule extraction into existing machine learning workflows. Here are an example on Iris data set.

Import modules

First, import necessary modules:

import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import average_precision_score, accuracy_score,roc_auc_score

from woodtapper.example_sampling import RandomForestClassifierExplained

Load data

iris = load_iris()
X = pd.DataFrame(iris.data, columns=iris.feature_names)
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

Train RandomForestClassifierExplained

## RandomForestClassifier rules extraction
RFExplained = RandomForestClassifierExplained(n_estimators=100,random_state=0)
RFExplained.fit(X_train,y_train)

Generate example-based explainability

Xy_explain = RFExplained.explanation(X_test,to_pandas=True, feature_names=iris.feature_names) # Get the 5 most similar samples for each test sample
X_test.iloc[0,:]  # First test sample

Output

sepal length (cm)    5.8
sepal width (cm)     2.8
petal length (cm)    5.1
petal width (cm)     2.4
Name: 114, dtype: float64
Xy_explain[0] # Explanation for the first test sample

Output

sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
5.8 2.7 5.1 1.9 2
5.8 2.7 5.1 1.9 2
5.7 2.5 5.0 2.0 2
5.9 3.0 5.1 1.8 2
6.3 3.4 5.6 2.4 2

Load an existing RandomForestClassifier into the explainer

RF = RandomForestClassifier(n_estimators=100) # Standard RandomForestClassifier
RF.fit(X_train, y_train)

# Load an existing RandomForestClassifier into the explainer
RFExplained = RandomForestClassifierExplained.load_forest(RandomForestClassifierExplained,RF,X_train,y_train)
Xy_explain = RFExplained.explanation(X_test)