library(knitr)
opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
set.seed(123)

featureImportance: Model-agnostic permutation feature importance with the mlr package

CRAN Status Badge CRAN Downloads Build Status codecov

Results of the article "Visualizing the Feature Importance for Black Box Models"

This R package was developed as a part of the article "Visualizing the Feature Importance for Black Box Models" accepted at the ECML-PKDD 2018 conference track. The results of the application section of this article can be reproduced with the code provided here.

Installation of the package

Install the development version from GitHub (using devtools)

install.packages("devtools")
devtools::install_github("giuseppec/featureImportance")

Introduction

The featureImportance package is an extension for the mlr package and allows to compute the permutation feature importance in a model-agnostic manner. The focus is on performance-based feature importance measures:

Use case: Compute importance based on test data

This use case computes the feature importance of a model based on a single test data set. For this purpose, we first build a model (here a random forest) on training data:

library(mlr)
library(mlbench)
library(ggplot2)
library(gridExtra)
library(featureImportance)
set.seed(2018)

# Get boston housing data and look at the data
data(BostonHousing, package = "mlbench")
str(BostonHousing)

# Create regression task for mlr
boston.task = makeRegrTask(data = BostonHousing, target = "medv")

# Specify the machine learning algorithm with the mlr package
lrn = makeLearner("regr.randomForest", ntree = 100)

# Create indices for train and test data
n = getTaskSize(boston.task)
train.ind = sample(n, size = 0.6*n)
test.ind = setdiff(1:n, train.ind)

# Create test data using test indices
test = getTaskData(boston.task, subset = test.ind)

# Fit model on train data using train indices
mod = train(lrn, boston.task, subset = train.ind)

In general, there are two ways how the feature importance can be computed:

  1. Using fixed feature values: Here, the feature values are set to fixed values of the observation specified by replace.ids.
  2. Permuting the feature values: Here, the values of the feature are randomly permuted n.feat.perm times.

Using fixed feature values

Visualizing the feature importance using fixed feature values is analogous to partial dependece plots and has the advantage that the local feature importance is calculated for each observation in the test data at the same feature values:

# Use feature values of 20 randomly chosen observations from test data to plot the importance curves
obs.id = sample(1:nrow(test), 20)

# Measure feature importance on test data
imp = featureImportance(mod, data = test, replace.ids = obs.id, local = TRUE)
summary(imp)

# Plot PI and ICI curves for the lstat feature
pi.curve = plotImportance(imp, feat = "lstat", mid = "mse", individual = FALSE, hline = TRUE)
ici.curves = plotImportance(imp, feat = "lstat", mid = "mse", individual = TRUE, hline = FALSE)
grid.arrange(pi.curve, ici.curves, nrow = 1)

Permuting the feature

Instead of using fixed feature values, the feature importance can also be computed by permuting the feature values. Here, the PI curve and ICI curves are evaluated on different randomly selected feature values. Thus, a smoother is internally used for plotting the curve:

# Measure feature importance on test data
imp = featureImportance(mod, data = test, n.feat.perm = 20, local = TRUE)
summary(imp)

# Plot PI and ICI curves for the lstat feature
pi.curve = plotImportance(imp, feat = "lstat", mid = "mse", individual = FALSE, hline = TRUE)
ici.curves = plotImportance(imp, feat = "lstat", mid = "mse", individual = TRUE, hline = FALSE)
grid.arrange(pi.curve, ici.curves, nrow = 1)

Use case: Compute importance using a resampling technique

Instead of computing the feature importance of a model based on a single test data set, one can repeat this process by embedding the feature importance calculation within a resampling procedure. The resampling procedure creates multiple models using different training sets, and the corresponding test sets can be used to calculate the feature importance. For example, using 5-fold cross-validation results in 5 different models, one for each cross-validation fold.

rdesc = makeResampleDesc("CV", iter = 5)
res = resample(lrn, boston.task, resampling = rdesc, models = TRUE)
imp = featureImportance(res, data = getTaskData(boston.task), n.feat.perm = 20, local = TRUE)
summary(imp)
plotImportance(imp, feat = "lstat", mid = "mse", individual = FALSE, hline = TRUE)


giuseppec/featureImportance documentation built on June 1, 2021, 11:04 a.m.