This is code that will accompany an article that will appear in a special edition of a German IT magazine. The article is about explaining black-box machine learning models. In that article I’m showcasing three practical examples:
Explaining supervised classification models built on tabular data using caret and the iml package
Explaining image classification models with keras and lime
Explaining text classification models with lime
The second part has already been published here.
Below, you will find the code for the first part:
# data wrangling
library(tidyverse)
library(readr)
# ml
library(caret)
# plotting
library(gridExtra)
library(grid)
library(ggridges)
library(ggthemes)
theme_set(theme_minimal())
# explaining models
# https://github.com/christophM/iml
library(iml)
# https://pbiecek.github.io/breakDown/
library(breakDown)
# https://pbiecek.github.io/DALEX/
library(DALEX)
Supervised classfication models with tabular data
The data
The example dataset I am using in this part is the wine quality data. Let’s read it in and do some minor housekeeping, like
converting the response variable quality into two categories with roughly equal sizes and
replacing the spaces in the column names with “_&quot; to make it easier to handle in the tidyverse
wine_data %
mutate(quality = as.factor(ifelse(quality __ 6, &quot;qual_low&quot;, &quot;qual_high&quot;)))
## Parsed with column specification:
## cols(
## `fixed acidity` = col_double(),
## `volatile acidity` = col_double(),
## `citric acid` = col_double(),
## `residual sugar` = col_double(),
## chlorides = col_double(),
## `free sulfur dioxide` = col_double(),
## `total sulfur dioxide` = col_double(),
## density = col_double(),
## pH = col_double(),
## sulphates = col_double(),
## alcohol = col_double(),
## quality = col_integer()
## )
colnames(wine_data) %
ggplot(aes(x = y, y = quality, color = quality, fill = quality)) +
facet_wrap( ~ x, scale = &quot;free&quot;, ncol = 3) +
scale_fill_tableau() +
scale_color_tableau() +
geom_density_ridges(alpha = 0.8) +
guides(fill = FALSE, color = FALSE)
grid.arrange(p1, p2, ncol = 2, widths = c(0.3, 0.7))
Model
For modeling, I am splitting the data into 80% for training and 20% for testing.
set.seed(42)
idx