This is code that will encompany 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 xgboost and lime
The first part has been published here.
The second part has been published here.
Below, you will find the code for the third part: Text classification with lime.
# data wrangling
library(tidyverse)
library(readr)
# plotting
library(ggthemes)
theme_set(theme_minimal())
# text prep
library(text2vec)
# ml
library(caret)
library(xgboost)
# explanation
library(lime)
Text classification models
Here I am using another Kaggle dataset: Women’s e-commerce cloting reviews. The data contains a text review of different items of clothing, as well as some additional information, like rating, division, etc.
In this example, I will use the review title and text in order to classify whether or not the item was liked. I am creating the response variable from the rating: every item rates with 5 stars is considered “liked” (1), the rest as “not liked” (0). I am also combining review title and text.
clothing_reviews %
mutate(Liked = as.factor(ifelse(Rating == 5, 1, 0)),
text = paste(Title, `Review Text`),
text = gsub(&quot;NA&quot;, &quot;&quot;, text))
## Parsed with column specification:
## cols(
## X1 = col_integer(),
## `Clothing ID` = col_integer(),
## Age = col_integer(),
## Title = col_character(),
## `Review Text` = col_character(),
## Rating = col_integer(),
## `Recommended IND` = col_integer(),
## `Positive Feedback Count` = col_integer(),
## `Division Name` = col_character(),
## `Department Name` = col_character(),
## `Class Name` = col_character()
## )
glimpse(clothing_reviews)
## Observations: 23,486
## Variables: 13
## $ X1 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11...
## $ `Clothing ID` 767, 1080, 1077, 1049, 847, 1080, 85...
## $ Age 33, 34, 60, 50, 47, 49, 39, 39, 24, ...
## $ Title NA, NA, &quot;Some major design flaws&quot;, &quot;...
## $ `Review Text` &quot;Absolutely wonderful - silky and se...
## $ Rating 4, 5, 3, 5, 5, 2, 5, 4, 5, 5, 3, 5, ...
## $ `Recommended IND` 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, ...
## $ `Positive Feedback Count` 0, 4, 0, 0, 6, 4, 1, 4, 0, 0, 14, 2,...
## $ `Division Name` &quot;Initmates&quot;, &quot;General&quot;, &quot;General&quot;, &quot;...
## $ `Department Name` &quot;Intimate&quot;, &quot;Dresses&quot;, &quot;Dresses&quot;, &quot;B...
## $ `Class Name` &quot;Intimates&quot;, &quot;Dresses&quot;, &quot;Dresses&quot;, &quot;...
## $ Liked 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, ...
## $ text &quot; Absolutely wonderful - silky and s...
Whether an item was liked or not will thus be my response variable or label for classification.
clothing_reviews %__%
ggplot(aes(x = Liked, fill = Liked)) +
geom_bar(alpha = 0.8) +
scale_fill_tableau(palette = &quot;tableau20&quot;) +
guides(fill = FALSE)
Let’s split the data into train and test sets:
set.seed(42)
idx