eli5: Explain ML Predictions
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
-
ImportError: cannot import name 'load_boston' from 'sklearn.datasets'
cause The `load_boston` dataset was deprecated in scikit-learn 1.0 and removed in scikit-learn 1.2. Your scikit-learn version is too new for this dataset.fixUpdate your code to use a modern dataset such as `fetch_california_housing` or `load_diabetes`. -
ValueError: Feature names passed were not known to the model.
cause `eli5` could not determine the feature names, either because the model was trained without them (e.g., on numpy arrays instead of pandas DataFrames) or `feature_names` were not explicitly provided to the `eli5` explanation function.fixEnsure your training data is a Pandas DataFrame or explicitly pass a list of `feature_names` to `eli5.show_weights` or `eli5.show_prediction`, e.g., `eli5.show_weights(model, feature_names=X.columns.tolist())`. -
ImportError: cannot import name 'explain_weights' from 'eli5.sklearn'
cause You are attempting to use an old API. Prior to `eli5` 0.10.0, explanations were typically done through backend-specific functions like `eli5.sklearn.explain_weights`. These have been unified into generic `eli5.show_weights` and `eli5.show_prediction`.fixUpdate your code to use the generic functions: replace `eli5.sklearn.explain_weights(model)` with `eli5.show_weights(model)` (and similarly for `explain_prediction`).
Warnings
- breaking The old backend-specific explanation functions like `eli5.sklearn.explain_weights` and `eli5.sklearn.explain_prediction` were removed and replaced by generic `eli5.show_weights` and `eli5.show_prediction`.
- gotcha `eli5.show_weights` and `eli5.show_prediction` return `IPython.display.HTML` objects in Jupyter notebooks for rich display. In a standard Python console, a string representation (often HTML source) is printed. If you need programmatic access (e.g., as a DataFrame or plain text), you must explicitly use formatters.
- gotcha Many `scikit-learn` datasets (e.g., `load_boston`) are deprecated or removed in newer `scikit-learn` versions (1.2+). Code relying on these datasets will cause `ImportError` or `FutureWarning`.
Install
-
pip install eli5 -
pip install 'eli5[sklearn,lightgbm,xgboost,keras,nltk]' # Install with common extras
Imports
- show_weights
import eli5 eli5.show_weights(...)
- show_prediction
import eli5 eli5.show_prediction(...)
- PermutationImportance
from eli5.sklearn import PermutationImportance
- as_dataframe
from eli5.formatters import as_dataframe
- explain_weights
from eli5.sklearn import explain_weights
import eli5 eli5.show_weights(...)
Quickstart
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()))