Model Explainability with SHAP values and LIME

When working with machine learning models, it is not enough to simply make accurate predictions. We also need to understand how these models are making those predictions. This is where model explainability techniques come into play. They help us interpret the way a model operates and shed light on the factors that contribute the most to its predictions.

In this article, we will explore two popular techniques for model explainability: SHAP (SHapley Additive exPlanations) values and LIME (Local Interpretable Model-agnostic Explanations). Both of these techniques provide valuable insights into the inner workings of a machine learning model, but they differ in their approaches.

Understanding SHAP values

SHAP values are based on a concept from cooperative game theory called Shapley values. In the context of machine learning, SHAP values allocate the contribution each feature makes to a prediction. They provide a unified measure of feature importance that considers all possible feature coalitions.

The core idea behind SHAP values is to compute the conditional expectation of a model's output by averaging predictions made on different subsets of features. This allows us to understand which features are most impactful for a particular prediction.

One of the main benefits of SHAP values is their interpretability. They are defined in the same units as the model output, making it easy to understand the magnitude and direction of a feature's contribution. Additionally, SHAP values satisfy some desirable properties, such as symmetry, which ensures that the importance of a feature is not biased.

To compute SHAP values for a specific model, we can use the SHAP library in Python. This library provides efficient implementations for a variety of models, including trees, linear models, and deep learning models.

Introducing LIME

LIME, on the other hand, takes a different approach to model explainability. Instead of directly computing feature importance, LIME approximates the behavior of a complex model by training a simpler surrogate model on local perturbations of the original data. It then uses this surrogate model to explain individual predictions.

The key advantage of LIME is that it is model-agnostic, meaning it can be applied to any machine learning model regardless of the underlying algorithm. LIME works by sampling instances around a prediction and generating interpretable explanations using these instances.

LIME operates at the instance level, meaning it provides explanations specific to each prediction. This can be particularly useful in scenarios where we want to understand why a particular data point was classified in a certain way.

To use LIME, we need to install the LIME library in Python and create an explainer object based on our trained model. LIME takes care of generating local explanations for each prediction and provides visualizations to help us understand the reasons behind these predictions.

Choosing between SHAP and LIME

Both SHAP values and LIME offer unique advantages and can be used in different scenarios. SHAP values provide a global perspective of feature importance and can be helpful when we want to understand the overall behavior of a model. LIME, on the other hand, focuses on local explanations and provides insights into individual predictions.

It's important to note that the choice between SHAP and LIME should be based on the specific problem at hand and the requirements of the model interpretability. In some cases, using both techniques together can provide a more comprehensive understanding of the model.


Model explainability is a crucial aspect of machine learning, as it helps build trust and understanding in the models we create. SHAP values and LIME are two powerful techniques that enable us to interpret and explain the decisions made by our models.

SHAP values provide us with global feature importance measures, while LIME focuses on local explanations for individual predictions. By leveraging these techniques, we can gain valuable insights into how our models work and enhance their transparency and interpretability.

It's important to experiment with both SHAP values and LIME when interpreting models and choose the appropriate technique based on the context and objectives of the analysis. With these tools at our disposal, we can build more trust in our models, make better decisions, and ensure accountability in the realm of machine learning.

noob to master © copyleft