<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/explaining-black-box-machine-learning-models-code-part-1-tabular-data-caret-iml/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://shirinsplayground.netlify.com/2018/07/explaining_ml_models_code_caret_iml/"> Shirin's playgRound</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>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:</p>
<ol>
<li>Explaining supervised classification models built on tabular data using <code>caret</code> and the <code>iml</code> package</li>
<li>Explaining image classification models with <code>keras</code> and <code>lime</code></li>
<li>Explaining text classification models with <code>lime</code></li>
</ol>
<p>The second part has already been published <a rel="nofollow" href="https://shirinsplayground.netlify.com/2018/06/keras_fruits_lime/" target="_blank">here</a>.</p>
<p>Below, you will find the code for the first part:</p>
<pre><code># 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)</code></pre>
<div>
<h2>Supervised classfication models with tabular data</h2>
<div>
<h3>The data</h3>
<p>The example dataset I am using in this part is the <a rel="nofollow" href="https://www.kaggle.com/uciml/red-wine-quality-cortez-et-al-2009" target="_blank">wine quality data</a>. Let’s read it in and do some minor housekeeping, like</p>
<ul>
<li>converting the response variable <code>quality</code> into two categories with roughly equal sizes and</li>
<li>replacing the spaces in the column names with “_&quot; to make it easier to handle in the tidyverse</li>
</ul>
<pre><code>wine_data &lt;- read_csv(&quot;/Users/shiringlander/Documents/Github/ix_lime_etc/winequality-red.csv&quot;) %>%
  mutate(quality = as.factor(ifelse(quality &lt; 6, &quot;qual_low&quot;, &quot;qual_high&quot;)))</code></pre>
<pre><code>## 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()
## )</code></pre>
<pre><code>colnames(wine_data) &lt;- gsub(&quot; &quot;, &quot;_&quot;, colnames(wine_data))
glimpse(wine_data)</code></pre>
<pre><code>## Observations: 1,599
## Variables: 12
## $ fixed_acidity        </code></pre></div></div>