# Is there a way to explain a prediction from a random forest model?

Say I've got a predictive classification model based on a random forest (using the randomForest package in R). I'd like to set it up so that end-users can specify an item to generate a prediction for, and it'll output a classification likelihood. So far, no problem.

But it would be useful/cool to be able to output something like a variable importance graph, but for the specific item being predicted, not for the training set. Something like:

Item X is predicted to be a Dog (73% likely)
Because:
Legs=4
Fur=short
Food=nasty

You get the point. Is there a standard, or at least justifiable, way of extracting this information from a trained random forest? If so, does anyone have code that will do this for the randomForest package?

• Some complexity... You could imagine counting the number of times the Legs variable was part of the decision path. But would you just do that for trees that predicted the majority answer, or all of them? Or the difference? Apr 7, 2011 at 23:53
• And changing all of the m predictors one by one and looking to see how the forest predicts differently seems sorta expensive. There's gotta be a better way. Apr 8, 2011 at 1:03
• my first thought was to wonder how what you are wanting to do differs from the variable importance of the training data? Are you looking to say that given the other values as they were, what was the sensitivity of the prediction on legs =4 versus legs=2 or legs=0? Have you looked at the partial plot function in the randomforest package? Apr 8, 2011 at 1:22
• The variable importance is usually defined conditional on the whole training set (or the assumed population, or something). But what I want is the variable importance for a single predicted item. Imagine a case where the forest consists of very lopsided decision trees. Test Instance 1 could be explained by 1 or very few decision nodes, while Test Instance 2 could be explained by a much larger set of decision nodes. I want a very simple human-interpretable version of that, like a ranked set of decisions, the top 5 of which I can provide. For a single decision tree, I'd just read them off. Apr 8, 2011 at 1:59

First idea is just to mimic the knock-out strategy from variable importance and just test how mixing each attribute will degenerate the forest confidence in object classification (on OOB and with some repetitions obviously). This requires some coding, but is certainly achievable.

However, I feel it is just a bad idea -- the result will be probably variable like hell (without stabilizing impact of averaging over objects), noisy (for not-so-confident objects the nonsense attributes could have big impacts) and hard to interpret (two or more attribute cooperative rules will probably result in random impacts of each contributing attributes).

Not to leave you with negative answer, I would rather try to look at the proximity matrix and the possible archetypes it may reveal -- this seems much more stable and straightforward.

• The cooperative rules/correlated predictors point is an excellent criticism. To make this work, it might be necessary to train the RF on some sort of pre-reduced set of variables, or incorporate some sort of penalization strategy to cause the RF to focus on a subset of predictors. Mar 9, 2013 at 15:02

I would try with the lime framework.

It works with many models (including random forest). It can be used for local interpretation (that is, explaining a single prediction) or for global interpretation (that is, explaining a whole model).

Quoting from the asbtract

In this work, we propose LIME, a novel explanation technique that explains the predictions of any classifier in an interpretable and faithful manner, by learning an interpretable model locally around the prediction. We also propose a method to explain models by presenting representative individual predictions and their explanations in a non-redundant way, framing the task as a submodular optimization problem.

It has packages both for R and python, and many examples if you google it.