SHAP explainability
1. SHAP explainability
Great work so far! Next, let's get introduced to SHAP.2. Introduction to SHAP
SHAP, which stands for SHapley Additive exPlanations, is a powerful model-agnostic technique that explains the contribution of individual features in a given machine learning model's predictions. SHAP relies on game theory principles, specifically shapley values, which aim to fairly distribute the "payout" among players based on their contribution to the total outcome. In machine learning, SHAP values quantify how much each feature contributes to the prediction.3. Splitting profit among band members
Imagine splitting profits from a hit song among band members based on their contributions to the song's success; SHAP values divide the prediction output across the input features according to their impact.4. SHAP explainers
We utilize SHAP explainers to calculate these values. While SHAP is model-agnostic, its explainers can be broadly categorized into two major kinds: general explainers which can be applied to any model, and type-specific explainers, each optimized for specific categories of models. For now, we'll focus on the Tree Explainer, optimized for tree-based models, and introduce a general explainer in the next video.5. Admissions dataset
Let's derive SHAP values for two models trained on the graduate admissions dataset. A random forest regressor, predicting the chance of admission, and a random forest classifier predicting acceptance. Features are in the dataframe X.6. Deriving SHAP values
First, we import the shap library. Then, we create tree explainers using shap.TreeExplainer, passing in the model we want to explain. For deriving SHAP values, we call the .shap_values method on the explainer, supplying the dataset.7. Understanding SHAP output
Printing the shape of the regression shap_values array reveals that it forms a matrix mirroring the shape of our dataset X. Each row represents a sample, and each column corresponds to a feature. The values in the matrix indicate how much each feature in a sample influenced the model's output, either increasing or decreasing it.8. Understanding SHAP output
For classification, there is a slight difference: the output matrix includes an additional dimension to accommodate the classes. In our case of binary classification, there are two classes, and we have to select one to explain. Typically, we use the index [1] which corresponds to the positive class, in order to understand how the features influence the probability of a sample belonging to this class.9. Feature importance
To gauge overall feature importance, we aggregate SHAP values across all data points by taking the mean of the absolute SHAP values across the dataset. This highlights features that have the most influence on the model’s predictions, regardless of the direction of that influence. Plotting the results we see that through SHAP, the explanations are similar to those derived with all other methods, giving higher importance for CGPA and test scores.10. Let's practice!
Time to practice!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.