Understanding Partial Dependence Plots (PDPs)

agnostic method to interpret any ml model

Mehul Gupta
Data Science in your pocket

--

https://learning.oreilly.com/library/view/interpretable-ai/9781617297649/OEBPS/Text/03.htm

Interpretable AI has been the talk of the town. Considering the real-world scenarios, the need to understand the output of ml models becomes very crucial. We can interpret the model’s results for white-box models like Linear Regression (observing weights assigned to different features), Decision Trees (feature_importance attribute for the trained model in scikit-learn implementation) easily.

My debut book “LangChain in your Pocket” is out now

But how can we

interpret black-box(complex) models like deep neural networks?

Also,

can we have a common method (rather than different methods for different models) to interpret any model, be it white-box or black-box?

PDPs are the answer to both questions !!

PDPs belong to the agnostic methods family for interpreting any ml model. By agnostic, we mean universal. PDPs are based on the idea of visualizing the average effect of the values of a particular feature by marginalizing all other features in the feature set.

Let’s understand how PDPs are plotted with a sample dataset alongside the maths:

For this, we will be considering a binary classification problem with 4 features in the feature set as shown below

What we would be doing is finding the impact of feature ‘A’ while predicting the ‘Target’ variable using an SVM.

Talking about the maths, we would be following the below steps to find the impact of any feature ’n’ by

  • Train the model on the training dataset
  • Assume our feature of interest is ‘A’ (for which we need to find the impact on target variable prediction by the model), then we will
  1. Replicate the entire dataset (excluding the feature of interest i.e. ‘A’) for ‘m’ a number of times where ‘m’ is the total unique value in the feature of interest ‘A’ and set each unique value of ‘A’ once to one replica of the dataset. Hence, if the dataset has 100 samples & ‘A’ has 10 unique samples, then we will have 100x10 total samples where every unique value ‘A’ is assigned to 100 samples.
  2. Get prediction for this new upsampled dataset from the trained model
  3. The average output for each unique value of ‘A’. So if we get 100 predictions per unique value, do an average over the 100 outputs to get a final output value for each unique value of ‘A’
  4. Plot unique values of ‘A’ against the averaged prediction for each value

Taadaaaa !!

Let’s get over the codes quickly & the output as well. First train the model

from sklearn import svm
clf = svm.SVC()
clf.fit(df,target)

Next, calculate the partial dependence using pdpbox library from the python

from pdpbox import pdppdp_A = pdp.pdp_isolate(model=clf,                  
dataset=df,
model_features=['A','B','C','D']
feature='A')

Slight formatting & plot of the Partial Dependence calculated above

#basic aestheticsplot_params = {
'title_fontsize': 15,
'subtitle_fontsize': 12,
'contour_color': 'white',
'font_family': 'Arial',
'cmap': 'viridis',
'inter_fill_alpha': 0.8,
'inter_fontsize': 9}

fig, axes = pdp.pdp_plot(pdp_isolate_out=pdp_A,feature_name='feature A', center=True, x_quantile=False, ncols=2,plot_lines=False, frac_to_plot=100,plot_params=plot_params, figsize=(8, 6))

The output

As one can see, As the value of A increases, the model’s prediction moves from 0 to 1.

Similarly, we can plot for other features as well

As you saw, how PDPs are super easy to implement & interpret as well to understand feature impact in a model’s prediction. Also, being agnostic in nature, PDPs can be used to interpret any ML model, be it white-box or black-box.

A big thanks to Interpretable AI from Oreilly Media before signing off !!

--

--