eli5: Explain ML Predictions

0.16.0 · active · verified Fri Apr 17

eli5 (Explain Like I'm 5) is a Python library that helps to debug machine learning classifiers and explain their predictions. It provides a unified API for inspecting weights and predictions of various ML models, including scikit-learn, Keras, LightGBM, XGBoost, and more. The current version is 0.16.0. Development is active, with releases typically tied to feature development and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to train a RandomForestRegressor on the California Housing dataset and then use `eli5.show_weights` to explain global feature importances and `eli5.show_prediction` to explain a specific prediction. Note the use of `feature_names` for clearer output.

import eli5
from sklearn.datasets import fetch_california_housing
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split

# Load a modern dataset with feature names
housing = fetch_california_housing(as_frame=True)
X, y = housing.data, housing.target
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

# Train a simple model
model = RandomForestRegressor(random_state=42, n_estimators=10)
model.fit(X_train, y_train)

print("\n--- Feature Importances (Weights) ---")
# Explain feature importances (weights)
# In a Jupyter notebook, this would render as HTML.
# For console output, the string representation is printed.
print(eli5.show_weights(model, feature_names=X.columns.tolist()))

print("\n--- Prediction Explanation for First Test Sample ---")
# Explain a single prediction
print(eli5.show_prediction(model, X_test.iloc[0], feature_names=X.columns.tolist()))

view raw JSON →