{"id":5989,"library":"lime","title":"LIME: Local Interpretable Model-Agnostic Explanations","description":"LIME (Local Interpretable Model-agnostic Explanations) is a Python library designed to explain individual predictions of machine learning classifiers and regressors. It works for tabular, text, and image data by building local, interpretable surrogate models around the instance to be explained. The current version is 0.2.0.1 and the library is actively maintained.","status":"active","version":"0.2.0.1","language":"en","source_language":"en","source_url":"http://github.com/marcotcr/lime","tags":["interpretability","machine learning","xai","explanation","model-agnostic","tabular","text","image"],"install":[{"cmd":"pip install lime","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Essential for data manipulation, especially with tabular data explainers.","package":"numpy","optional":false},{"reason":"Commonly used for training models that LIME then explains; many examples rely on it.","package":"scikit-learn","optional":false}],"imports":[{"note":"Explainer classes are located within sub-modules (e.g., lime_tabular, lime_text, lime_image), not directly under the top-level 'lime' package.","wrong":"from lime import LimeTabularExplainer","symbol":"LimeTabularExplainer","correct":"from lime.lime_tabular import LimeTabularExplainer"},{"note":"Explainer classes are located within sub-modules (e.g., lime_tabular, lime_text, lime_image), not directly under the top-level 'lime' package.","symbol":"LimeTextExplainer","correct":"from lime.lime_text import LimeTextExplainer"},{"note":"Explainer classes are located within sub-modules (e.g., lime_tabular, lime_text, lime_image), not directly under the top-level 'lime' package.","symbol":"LimeImageExplainer","correct":"from lime.lime_image import LimeImageExplainer"}],"quickstart":{"code":"import numpy as np\nimport sklearn\nimport sklearn.ensemble\nfrom sklearn.datasets import load_iris\nfrom sklearn.model_selection import train_test_split\n\nimport lime\nimport lime.lime_tabular\n\n# 1. Prepare Data and Model\niris = load_iris()\nX = iris.data\ny = iris.target\nfeature_names = iris.feature_names\nclass_names = iris.target_names\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\nmodel = sklearn.ensemble.RandomForestClassifier(n_estimators=100, random_state=42)\nmodel.fit(X_train, y_train)\n\n# 2. Create a LIME Explainer for Tabular Data\nexplainer = lime.lime_tabular.LimeTabularExplainer(\n    training_data=X_train, \n    feature_names=feature_names, \n    class_names=class_names, \n    mode='classification'\n)\n\n# 3. Choose an instance to explain\ni = np.random.randint(0, X_test.shape[0])\ninstance_to_explain = X_test[i]\n\n# 4. Generate the explanation\nexplanation = explainer.explain_instance(\n    data_row=instance_to_explain, \n    predict_fn=model.predict_proba, \n    num_features=2\n)\n\n# 5. Print the explanation\nprint(f\"Explaining instance: {instance_to_explain}\")\nprint(f\"Predicted class: {iris.target_names[model.predict(instance_to_explain.reshape(1, -1))[0]]}\")\nprint(\"--- Local Explanation ---\")\nprint(explanation.as_list())\n\n# For visualization in a Jupyter Notebook:\n# explanation.show_in_notebook(show_table=True)","lang":"python","description":"This quickstart demonstrates how to use `LimeTabularExplainer` to explain an individual prediction from a scikit-learn RandomForestClassifier on the Iris dataset. It covers data preparation, model training, explainer initialization, and generating/displaying the explanation."},"warnings":[{"fix":"Ensure your project uses Python 3.5 or a later version. Upgrade your Python environment if necessary.","message":"Python 2 support was dropped in version 0.2.0.0. LIME now requires Python 3.5 or newer.","severity":"breaking","affected_versions":">=0.2.0.0"},{"fix":"Always provide `LimeTabularExplainer` with a representative sample of your training data (as a NumPy array) to ensure accurate feature statistics are calculated.","message":"For `LimeTabularExplainer`, `training_data` is crucial. It is used to compute statistics (mean, std dev, frequencies) for feature perturbation and discretization. Providing a non-representative or empty `training_data` can lead to inaccurate or misleading explanations.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Verify that your `predict_fn` matches the expected output type for LIME based on your model's task (probabilities for classification, raw values for regression).","message":"The `predict_fn` passed to `explain_instance` must return probabilities for classification tasks (e.g., `model.predict_proba`) and raw predicted values for regression tasks (e.g., `model.predict`). Mismatching this can cause errors or incorrect explanations.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Experiment with different hyperparameters to assess the stability of explanations. Consider the theoretical limitations and potential for local instability, especially in high-dimensional or complex feature spaces.","message":"LIME explanations can be sensitive to hyperparameters like `num_samples` (number of perturbed samples) and the choice of distance metric, potentially leading to varied or unstable explanations across runs or slight changes in settings.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}