library(mlr3)
library(mlr3learners)
library(mlr3tuning)
library(DALEX)
library(DALEXtra)
library(ALEPlot)Interpretable Machine Learning
Exercise on Functional Decomposition
In the lecture, we discussed local and global explanations as well as functional decompositions. In this exercise we want to take a deeper look via simulated data.
- Load the relevant packages.
- We can use our own implementation of PDP
## 1-dim pdp plot at a fixed point
pdp_xk <- function(data,learner,k,x_k){
newX <- data
newX[,k] <- x_k
mean(predict(learner, as.data.frame(newX)))
}
## 2-dim pdp plot at a fixed point
pdp_xjk <- function(data,learner,j,k,x_j,x_k){
newX <- data
newX[,k] <- x_k
newX[,j] <- x_j
mean(predict(learner, as.data.frame(newX)))
}
#### calcualtes 1-dim pdp plot over a equi-distant grid of length I
pdp <- function(dat,learner,k, I) {
my_grid <- seq(min(dat[,k]) , max(dat[,k]), length.out=I)
return(list(x_k=my_grid, pdp_k=sapply(1:I, function(j) pdp_xk(dat,learner,k,my_grid[j]))))
}- We generate data according to the following (noiseless) mechanism \[ Y=X_1 + X_2^2+ X_1X_2 \]
- \(X_1\) , \(X_2\) are uniformly distributed on \([0,1]\) along a segment of the line \(x_2 = x_1\) with independent \(N(0, 0.05^2)\) noise added to both predictors. We generate 1000 training observations.
m <- function(x){ x[,1]+x[,2]^2+x[,1]*x[,2] }
generate_data <- function(n){
X1 <- X2 <- runif(n)
X1 <- X1 + rnorm(n,sd=0.05)
X2 <- X2 + rnorm(n,sd=0.05)
X <- cbind(X1,X2)
Y <- m(X) ### no noise
dat <- data.frame(X=X, Y=Y)
return(dat)
}
n <- 1000
dat <- generate_data(n)- Complete the following tasks
- Train an auto tuned ranger model on the data
- Produce and visualize a SHAP explanation for the observation
dat[100,-3] - Produce and plot 1-dim PDPs for feature 1 and feature 2.
- Evaluate 1-dim and 2-dim PDPs at the fixed point
dat[100,-3]. - Use the result in 4. together with Lemma 7.4.4 of the lecture notes to calculate the functional components \(\hat m_0\), \(\hat m_1\), \(\hat m_2\), \(\hat m_{12}\) identified via the marginal identification.
- Use the functional components from 5. together with Corollary 7.4.5 from the lecture notes to calculate the interventional SHAP values \(\hat \phi_1, \hat \phi_2\). Compare the values to the result from 1.