---
title: "Model Agnostic Interpretability"
format:
html:
code-fold: show
code-tools: true
editor: visual
---
```{r}
#| include: false
#| warning: false
library (AmesHousing)
ames <- make_ordinal_ames ()
library (tidyverse)
ames <- ames %>% mutate (id = row_number ())
set.seed (4321 )
training <- ames %>% sample_frac (0.7 )
testing <- anti_join (ames, training, by = 'id' )
training <- training %>%
select (Sale_Price,
Bedroom_AbvGr,
Year_Built,
Mo_Sold,
Lot_Area,
Street,
Central_Air,
First_Flr_SF,
Second_Flr_SF,
Full_Bath,
Half_Bath,
Fireplaces,
Garage_Area,
Gr_Liv_Area,
TotRms_AbvGrd)
training.df <- as.data.frame (training)
```
```{python}
#| include: false
#| message: false
training = r.training
testing = r.testing
import pandas as pd
train_dummy = pd.get_dummies(training, columns = ['Street' , 'Central_Air' ])
print (train_dummy)
y_train = train_dummy['Sale_Price' ]
X_train = train_dummy.loc[:, train_dummy.columns != 'Sale_Price' ]
```
# "Interpretability"
# Permutation Importance
::: {.panel-tabset .nav-pills}
## R
```{r}
#| message: false
library (randomForest)
set.seed (12345 )
rf.ames <- randomForest (Sale_Price ~ ., data = training.df, ntree = 250 , importance = TRUE )
varImpPlot (rf.ames,
sort = TRUE ,
n.var = 10 ,
main = "Top 10 - Variable Importance" , type = 1 )
importance (rf.ames)
training.df$ pred_rf <- predict (rf.ames)
```
```{r}
library (iml)
lm.ames <- lm (Sale_Price ~ ., data = training)
summary (lm.ames)
linear_pred <- Predictor$ new (lm.ames, data = training[,- 1 ],
y = training$ Sale_Price, type = "response" )
plot (FeatureImp$ new (linear_pred, loss = "mse" ))
```
## Python
:::
# Individual Conditional Expectation (ICE)
::: {.panel-tabset .nav-pills}
## R
```{r}
library (patchwork)
set.seed (12345 )
forest_pred <- Predictor$ new (rf.ames, data = training[,- 1 ],
y = training$ Sale_Price, type = "response" )
ice_plot <- FeatureEffects$ new (forest_pred, method = "ice" )
ice_plot$ plot (c ("Garage_Area" ))
ice_plot$ plot ()
```
## Python
:::
# Partial Dependence
::: {.panel-tabset .nav-pills}
## R
```{r}
pd_plot <- FeatureEffects$ new (forest_pred, method = "pdp" )
pd_plot$ plot (c ("Garage_Area" ))
pd_plot$ plot ()
```
```{r}
pdice_plot <- FeatureEffects$ new (forest_pred, method = "pdp+ice" )
pdice_plot$ plot (c ("Garage_Area" ))
pdice_plot$ plot ()
pdice_plot$ plot (c ("Mo_Sold" ))
```
## Python
:::
# Accumulated Local Effects (ALE)
::: {.panel-tabset .nav-pills}
## R
```{r}
ale_plot <- FeatureEffects$ new (forest_pred, method = "ale" )
ale_plot$ plot (c ("Garage_Area" ))
ale_plot$ plot ()
```
## Python
:::
# Local Interpretable Model-Agnostic Explanations (LIME)
::: {.panel-tabset .nav-pills}
## R
```{r}
point <- 1328
lime.explain <- LocalModel$ new (forest_pred, x.interest = training[point,- 1 ], k = 5 )
plot (lime.explain) + theme_bw ()
point <- 1000
lime.explain <- LocalModel$ new (forest_pred, x.interest = training[point,- 1 ], k = 5 )
plot (lime.explain)
```
## Python
:::
# Shapley Values
::: {.panel-tabset .nav-pills}
## R
```{r}
point <- 1328
shap <- Shapley$ new (forest_pred, x.interest = training[point,- 1 ])
shap$ plot ()
point <- 1000
shap <- Shapley$ new (forest_pred, x.interest = training[point,- 1 ])
shap$ plot ()
```
## Python
:::