# LIME — Explain Any Machine Learning Prediction > LIME (Local Interpretable Model-agnostic Explanations) is a Python library that explains predictions of any classifier or regressor by learning an interpretable local model around each prediction. ## Install Save as a script file and run: # LIME — Explain Any Machine Learning Prediction ## Quick Use ```bash pip install lime ``` ```python from lime.lime_tabular import LimeTabularExplainer import sklearn.ensemble # Train a model, then explain a single prediction explainer = LimeTabularExplainer(X_train, feature_names=feature_names) exp = explainer.explain_instance(X_test[0], model.predict_proba, num_features=5) exp.show_in_notebook() ``` ## Introduction LIME is a model-agnostic explanation technique introduced in the 2016 paper "Why Should I Trust You?" by Marco Tulio Ribeiro, Sameer Singh, and Carlos Guestrin. It explains individual predictions by perturbing the input and fitting a simple, interpretable model around the neighborhood of each data point. LIME works with any black-box classifier or regressor. ## What LIME Does - Explains individual predictions for tabular, text, and image data - Generates human-readable feature importance for any model - Works with scikit-learn, XGBoost, TensorFlow, PyTorch, and any predict function - Highlights which input features drove a specific prediction - Supports submodular pick (SP-LIME) for selecting representative explanations ## Architecture Overview LIME perturbs the input around a data point, obtains predictions from the black-box model for each perturbation, and fits a weighted linear model locally. The weights decrease with distance from the original point. For text, it removes words; for images, it masks superpixels; for tabular data, it samples from the training distribution. The resulting sparse linear model coefficients become feature importances. ## Self-Hosting & Configuration - Install via pip: `pip install lime` - Requires Python 3.6+ with NumPy and scikit-learn - No external services or APIs needed - Works in Jupyter notebooks with built-in HTML visualizations - Configurable: number of features, perturbation count, and kernel width ## Key Features - Truly model-agnostic: works with any classifier that exposes a predict function - Built-in support for tabular, text, and image explanation modes - Generates interactive HTML explanations for notebooks - SP-LIME picks a diverse set of representative explanations for a dataset - Lightweight with minimal dependencies ## Comparison with Similar Tools - **SHAP** — provides global and local explanations with Shapley values; LIME is faster for single predictions - **Captum** — PyTorch-specific attribution methods; LIME is framework-agnostic - **InterpretML** — Microsoft's unified interpretability; LIME focuses on local explanations - **Anchor** — by the same authors, provides rule-based explanations with coverage guarantees ## FAQ **Q: How is LIME different from SHAP?** A: LIME fits a local linear model around each prediction. SHAP computes Shapley values, offering stronger theoretical guarantees but at higher computational cost for some model types. **Q: Can LIME explain deep learning models?** A: Yes. As long as the model exposes a prediction function, LIME can explain it regardless of architecture. **Q: Is LIME suitable for production use?** A: LIME is commonly used in production for model auditing and compliance. Explanation latency depends on the number of perturbations configured. **Q: Does LIME support regression?** A: Yes. Use LimeTabularExplainer with mode set to regression. ## Sources - https://github.com/marcotcr/lime - https://arxiv.org/abs/1602.04938 --- Source: https://tokrepo.com/en/workflows/asset-dbb36480 Author: Script Depot