Get startedGet started for free

Visualizing SHAP explainability

1. Visualizing SHAP explainability

SHAP offers visualization tools for enhanced model explainability.

2. Dataset

Assume we have a random forest regressor trained on the insurance dataset to predict charges. For our tree-based model, we utilize TreeExplainer to generate shap_values.

3. Feature importance plot

To explain the model, we start with feature importance plots which aggregate SHAP values across all data points to display each feature's contribution to the model output. This is what we've been recreating with matplotlib so far. Using shap.summary_plot with our shap_values and feature matrix X, specifying 'bar' as the plot type, we generate a bar chart ranking features by their importance. We observe that 'smoker' is the most significant predictor of charges, while gender has minimal impact.

4. Beeswarm plot

Next, we examine beeswarm plots, which offer a detailed view of the distribution of SHAP values for each feature across observations. This plot type illustrates the direction and magnitude of a feature's impact on the prediction, providing a more comprehensive view than bar plots, which average these effects and obscure variations. In a beeswarm plot, each point represents a SHAP value for a specific feature and sample. Points are color-coded: red indicates higher feature values, and blue indicates lower values. Positive SHAP values suggest a feature increases the prediction outcome, while negative values indicate a decrease.

5. Beeswarm plot

For instance, high 'smoker' values, shown in red with positive SHAP values, indicate higher insurance charges.

6. Beeswarm plot

Conversely, low values depicted in blue, indicating non-smokers, with negative SHAP values, indicate lower charges. To create a beeswarm plot, we use shap.summary_plot as before, changing only the plot_type to 'dot'.

7. Partial dependence plot

Finally, we explore partial dependence plots, which demonstrate the relationship between a specific feature and the predicted outcome. These plots are vital for understanding not only the average effect of a feature but also how its impact varies across its range, aiding in verifying if the relationships between features and outcomes are as expected.

8. Partial dependence plot

Suppose we want to observe the effect of age on insurance charges. Partial dependence plots operate as follows: For each sample, the age value is varied across its range while other features are held constant, and the outcomes are predicted. After repeating this for all samples, results are averaged for each age value. The shap.partial_dependence_plot function takes the variable of interest, the model.predict function that generates the charges, and the data. The dark blue line shows the average, indicating that higher ages correlate with higher charges, while lighter lines show individual samples where only age varies.

9. Partial dependence plot

The gray bars at the bottom provide context for the distribution of age values within the dataset. This helps us see where the model has more data to base its predictions, with more data points for certain ages potentially leading to more confident predictions in those ranges.

10. Let's practice!

Time to practice!