The real power of model explainability isn’t about understanding why a model made a specific prediction, it’s about understanding how the model learned to make predictions in the first place.

Let’s say you’ve trained a model to predict customer churn. You feed it data on customer demographics, usage patterns, support interactions, and billing history. The model spits out a probability of churn for each customer. Now, you want to understand why a particular customer, Alice, has a high churn probability.

Here’s a simplified view of what SHAP and LIME help you do.

LIME (Local Interpretable Model-agnostic Explanations)

Imagine you want to understand why LIME predicted a specific house price. LIME works by taking your prediction for Alice and then creating a bunch of "nearby" hypothetical Alices. These might be Alices with slightly different usage or billing dates. LIME then asks your original, complex model how it would predict churn for these hypothetical Alices.

# Example using LIME (conceptual, requires actual model and data)
import lime
import lime.lime_tabular
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

# Assume X_train, y_train, X_test, y_test are your prepared data
# Assume model is your trained RandomForestClassifier
# Assume feature_names = ['age', 'monthly_charges', 'support_calls', ...]
# Assume class_names = ['No Churn', 'Churn']

explainer = lime.lime_tabular.LimeTabularExplainer(
    training_data=X_train.values,
    feature_names=feature_names,
    class_names=class_names,
    mode='classification'
)

# Pick a specific instance to explain (e.g., the first customer in your test set)
instance_idx = 0
instance_to_explain = X_test.iloc[instance_idx].values

# Get the explanation for the 'Churn' class
explanation = explainer.explain_instance(
    data_row=instance_to_explain,
    predict_fn=model.predict_proba,
    num_features=5 # Number of top features to show
)

# To visualize (often done in a notebook or web app)
# explanation.show_in_notebook(show_table=True, show_all=False)

# Or get the explanation as a list of (feature, weight) tuples
print(explanation.as_list())

LIME then builds a simple, interpretable model (like a linear regression) that locally approximates the complex model’s behavior around Alice. It finds the features that are most influential for Alice’s specific prediction. For Alice, LIME might say: "Her high churn probability is mainly due to her high monthly charges and recent increase in support calls." This simple model is easy to understand.

SHAP (SHapley Additive exPlanations)

SHAP takes a more game-theory-inspired approach. Imagine all the features in your dataset are "players" in a game, and their goal is to contribute to the model’s prediction. SHAP calculates the "Shapley value" for each feature. This value represents how much that feature contributed to pushing the prediction away from a baseline (e.g., the average prediction across all customers).

# Example using SHAP (conceptual, requires actual model and data)
import shap
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

# Assume X_train, y_train, X_test, y_test are your prepared data
# Assume model is your trained RandomForestClassifier
# Assume feature_names = ['age', 'monthly_charges', 'support_calls', ...]

# For tree-based models, TreeExplainer is efficient
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X_test) # This will be a list of arrays if multi-class

# For a binary classification, shap_values[1] is for the positive class ('Churn')
shap_values_for_churn = shap_values[1]

# To visualize the impact of features for a single instance (e.g., the first customer)
instance_idx = 0
shap.initjs() # Initialize Javascript for plotting
shap.force_plot(explainer.expected_value[1], shap_values_for_churn[instance_idx,:], X_test.iloc[instance_idx,:])

# To visualize the overall feature importance across all instances
# shap.summary_plot(shap_values_for_churn, X_test, feature_names=feature_names)

SHAP provides a unified measure of feature importance. For Alice, SHAP might show that "monthly_charges" contributed +0.5 to her churn probability, while "tenure" contributed -0.3. This allows you to see not just the direction but also the magnitude of each feature’s impact. SHAP values are additive, meaning the sum of all feature SHAP values for an instance, plus the expected (average) prediction, equals the actual prediction for that instance.

The Mental Model: From Black Box to "Fuzzy Box"

Think of your complex model (like a deep neural network or a gradient boosting machine) as a "black box." You put data in, you get predictions out, but the internal workings are opaque.

  • LIME offers a "magnifying glass" that lets you zoom in on a single prediction. It creates a temporary, simple model that mimics the black box only in that tiny local region. It answers: "For this specific prediction, which features were most important, and in what direction?"

  • SHAP offers a "fairness audit" for your predictions. It distributes the "credit" or "blame" for a prediction among all the features, based on game theory. It answers: "For this specific prediction, how much did each feature contribute to pushing the outcome away from the average?"

Both methods provide insights into individual predictions. However, SHAP’s global summary plots (like shap.summary_plot) are incredibly powerful for understanding the overall behavior of the model across your entire dataset, showing which features are generally important and whether their impact is positive or negative.

The most surprising thing is how often these explainability methods reveal that your complex model is relying on features or feature interactions you didn’t expect. For instance, a model predicting loan defaults might show that "zip code" has a high SHAP value, indicating it’s learning proxies for socioeconomic factors that you might not have explicitly engineered. This isn’t necessarily bad, but it’s critical to discover and understand.

Once you’ve mastered interpreting individual predictions and global feature importance with SHAP and LIME, the next step is to leverage these insights for model debugging and improving fairness.

Want structured learning?

Take the full MLOps & AI DevOps course →