tezvyn:

SHAP: Explaining Black Box Model Predictions

AI-drafted, machine-checkedSource: shap.readthedocs.iointermediate

SHAP explains a model's prediction by treating features as players in a game and fairly distributing credit for the outcome. Use it to understand why a specific loan was denied or an image was misclassified. The footgun: SHAP explains the model, not reality.

WHY IT EXISTS Complex models like neural networks are often "black boxes." We know they're accurate, but we don't know how they arrive at a specific prediction. This makes them hard to debug, trust, or audit for fairness and compliance. SHAP was created to provide a mathematically sound way to peek inside and understand the "why" behind any single prediction for any model.

THE MENTAL MODEL Imagine a team of employees (features) works on a project, and they earn a final bonus (the model's prediction score). How do you fairly divide the bonus among them? SHAP answers this by calculating each employee's marginal contribution across all possible combinations of teammates. A feature's SHAP value is its average contribution to the prediction, providing a fair "credit allocation."

HOW IT WORKS SHAP connects game theory's Shapley values to machine learning. For a single prediction, it considers every possible subset (or "coalition") of features. It then calculates the model's prediction with and without a specific feature for each subset. The SHAP value for that feature is the weighted average of its marginal contribution across all these coalitions. This process guarantees that the sum of all feature SHAP values equals the difference between the model's prediction and the average prediction.

WHEN TO USE IT Use SHAP when you need local, instance-level explanations. It's ideal for answering questions like "Why was this specific user's transaction flagged as fraud?" or "Which patient symptoms contributed most to their high-risk diagnosis?" It is model-agnostic, working with everything from simple linear models to complex deep learning models for text, images, and tabular data.

WHEN NOT TO USE IT SHAP can be computationally expensive, especially for models with many features, as the number of feature subsets grows exponentially. For simple, inherently interpretable models like logistic regression, you can just look at the coefficients. Also, be careful using it for global feature importance; it's designed for local explanations, though aggregating local values can provide global insights.

ONE CANONICAL EXAMPLE A bank uses a gradient boosting model to predict loan defaults. A customer is denied. The loan officer uses SHAP to explain the decision. The SHAP plot shows a large negative contribution from "low income" and "high debt-to-income ratio," and a smaller negative contribution from "short credit history." It also shows a small positive contribution from "stable employment." This allows the officer to give the applicant specific, actionable feedback.

Read the original → shap.readthedocs.io

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.