GBM
R programming
variable importance
machine learning
classification

GBM R function get variable importance separately for each class

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The gbm package can report variable importance through summary.gbm(), but that output is global for the fitted model. If you want importance values separately for each class in a multiclass problem, gbm does not expose that directly, so you need a workaround such as one-vs-rest models or a model-agnostic importance calculation.

What summary.gbm() Gives You

For a standard gbm fit, variable importance is based on relative influence across all trees in the model:

r
1library(gbm)
2
3set.seed(42)
4
5model <- gbm(
6  Species ~ .,
7  data = iris,
8  distribution = "multinomial",
9  n.trees = 200,
10  interaction.depth = 2,
11  shrinkage = 0.05,
12  n.minobsinnode = 5,
13  verbose = FALSE
14)
15
16summary(model, plotit = FALSE)

That output is useful, but it is not class-specific. It tells you which predictors matter overall, not which predictors matter most for setosa versus virginica.

Why Class-Specific Importance Is Harder

In a multiclass GBM, the model is optimized jointly for the full response. The built-in relative influence calculation summarizes split improvement across the fitted model as a whole. The package does not return a separate variable-importance table for each class label.

So if the question is “does gbm have a built-in switch for per-class importance,” the practical answer is no.

Workaround 1: Fit One-Vs-Rest GBMs

The simplest class-specific strategy is to train one binary model per class. Each model answers a focused question: how important is each feature for separating this class from all others?

r
1library(gbm)
2
3set.seed(42)
4
5classes <- levels(iris$Species)
6
7importance_by_class <- lapply(classes, function(cls) {
8  binary_df <- iris
9  binary_df$target <- as.integer(binary_df$Species == cls)
10
11  fit <- gbm(
12    target ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width,
13    data = binary_df,
14    distribution = "bernoulli",
15    n.trees = 200,
16    interaction.depth = 2,
17    shrinkage = 0.05,
18    n.minobsinnode = 5,
19    verbose = FALSE
20  )
21
22  summary(fit, plotit = FALSE)
23})
24
25names(importance_by_class) <- classes
26
27importance_by_class$setosa
28importance_by_class$virginica

This gives you a separate importance table for each class. It is not identical to “per-class importance inside one multinomial model,” but it is often the most practical answer and usually the easiest to interpret.

Interpreting the One-Vs-Rest Results

Suppose Petal.Length is dominant for setosa while Sepal.Width matters more for versicolor. That tells you different features are useful for different binary separation tasks, even if the global multinomial model only reports one overall ranking.

This is often exactly what analysts want when they ask for class-specific importance.

Workaround 2: Model-Agnostic Importance Per Class

If you want to keep one multinomial GBM and still study importance by class, use a model-agnostic method such as permutation importance on class probabilities.

The idea is:

  1. predict probabilities for one class
  2. measure a class-specific score such as log loss or AUC
  3. permute one predictor
  4. recompute the score
  5. treat the score drop as importance for that class

This requires more custom code, but it stays tied to a single fitted model.

In practice, many teams choose one-vs-rest GBMs because they are simpler and easier to explain.

When Global Importance Is Still Enough

Do not assume per-class importance is always necessary. If your goal is overall feature screening or general interpretability, the standard summary.gbm() output may already be sufficient.

Ask what decision the importance numbers need to support:

  • overall feature selection
  • class-specific interpretation
  • error analysis for one class
  • stakeholder reporting

The right importance method depends on that use case.

Common Pitfalls

The biggest mistake is assuming summary.gbm() on a multinomial model already gives class-specific importance. It does not.

Another issue is comparing one-vs-rest importance tables as if they were on exactly the same probabilistic scale as the original multinomial fit. They are related, but they come from different modeling setups.

Developers also sometimes over-interpret tiny differences in relative influence. Importance values are heuristic diagnostics, not causal proof.

Finally, if you use permutation importance, compute it on a proper validation set. Doing it on the training data can make the model look more stable and more certain than it really is.

Summary

  • 'summary.gbm() returns overall variable importance, not per-class importance.'
  • The gbm package does not provide a direct built-in per-class importance report for multinomial models.
  • One-vs-rest GBMs are the simplest practical workaround.
  • Permutation-based importance can provide class-specific analysis for a single fitted model.
  • Pick the method that matches the interpretation question you actually need to answer.

Course illustration
Course illustration

All Rights Reserved.