{"id":9721,"library":"eli5","title":"eli5: Explain ML Predictions","description":"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.","status":"active","version":"0.16.0","language":"en","source_language":"en","source_url":"https://github.com/eli5-org/eli5","tags":["machine-learning","explanation","xai","interpretability","ml-debug","model-explanation","feature-importance"],"install":[{"cmd":"pip install eli5","lang":"bash","label":"Basic Installation"},{"cmd":"pip install 'eli5[sklearn,lightgbm,xgboost,keras,nltk]' # Install with common extras","lang":"bash","label":"Installation with common ML backends"}],"dependencies":[{"reason":"Core numerical operations for array handling.","package":"numpy"},{"reason":"Scientific computing functionalities, used by various ML models and eli5 internally.","package":"scipy"},{"reason":"Primary supported machine learning library for model explanations (required >=0.20).","package":"scikit-learn"},{"reason":"Data manipulation, especially for handling DataFrames and feature names.","package":"pandas"},{"reason":"Used for formatting tabular data output in text mode.","package":"tabulate"},{"reason":"Optional: Required for explaining LightGBM models. Install with `pip install eli5[lightgbm]`.","package":"lightgbm","optional":true},{"reason":"Optional: Required for explaining XGBoost models. Install with `pip install eli5[xgboost]`.","package":"xgboost","optional":true},{"reason":"Optional: Required for explaining Keras/TensorFlow models. Install with `pip install eli5[keras]`.","package":"keras","optional":true}],"imports":[{"symbol":"show_weights","correct":"import eli5\neli5.show_weights(...)"},{"symbol":"show_prediction","correct":"import eli5\neli5.show_prediction(...)"},{"symbol":"PermutationImportance","correct":"from eli5.sklearn import PermutationImportance"},{"note":"Useful for programmatic access to explanation results, especially outside Jupyter notebooks.","symbol":"as_dataframe","correct":"from eli5.formatters import as_dataframe"},{"note":"Old API: `explain_weights` and `explain_prediction` (and similar backend-specific functions) were replaced by generic `eli5.show_weights` and `eli5.show_prediction`.","wrong":"from eli5.sklearn import explain_weights","symbol":"explain_weights","correct":"import eli5\neli5.show_weights(...)"}],"quickstart":{"code":"import eli5\nfrom sklearn.datasets import fetch_california_housing\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.model_selection import train_test_split\n\n# Load a modern dataset with feature names\nhousing = fetch_california_housing(as_frame=True)\nX, y = housing.data, housing.target\nX_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)\n\n# Train a simple model\nmodel = RandomForestRegressor(random_state=42, n_estimators=10)\nmodel.fit(X_train, y_train)\n\nprint(\"\\n--- Feature Importances (Weights) ---\")\n# Explain feature importances (weights)\n# In a Jupyter notebook, this would render as HTML.\n# For console output, the string representation is printed.\nprint(eli5.show_weights(model, feature_names=X.columns.tolist()))\n\nprint(\"\\n--- Prediction Explanation for First Test Sample ---\")\n# Explain a single prediction\nprint(eli5.show_prediction(model, X_test.iloc[0], feature_names=X.columns.tolist()))\n","lang":"python","description":"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."},"warnings":[{"fix":"Update your code to use `eli5.show_weights(...)` for global explanations and `eli5.show_prediction(...)` for individual predictions.","message":"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`.","severity":"breaking","affected_versions":"<0.10.0 (removed in 0.10.0)"},{"fix":"For DataFrame output: `from eli5.formatters import as_dataframe; df = as_dataframe(eli5.show_weights(...))`. For plain text: `from eli5.formatters import as_text; text = as_text(eli5.show_weights(...))`.","message":"`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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use modern datasets like `fetch_california_housing` or `load_diabetes` (the non-deprecated version) for compatibility.","message":"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`.","severity":"gotcha","affected_versions":"scikit-learn >= 1.2.0 (affects examples using old datasets)"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Update your code to use a modern dataset such as `fetch_california_housing` or `load_diabetes`.","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.","error":"ImportError: cannot import name 'load_boston' from 'sklearn.datasets'"},{"fix":"Ensure 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())`.","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.","error":"ValueError: Feature names passed were not known to the model."},{"fix":"Update your code to use the generic functions: replace `eli5.sklearn.explain_weights(model)` with `eli5.show_weights(model)` (and similarly for `explain_prediction`).","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`.","error":"ImportError: cannot import name 'explain_weights' from 'eli5.sklearn'"}]}