# Feature importance in random forests when features are correlated

Random forests [1] are highly accurate classifiers and regressors in machine learning.

A strong advantage of random forests is interpretability; we can extract a measure of the importance of each feature in decreasing the error. One method to extract feature importance is to randomly permute a given feature and observe how the classification/regression changes. Another method is to keep track of the reduction in impurity or mean-square error, for classification or regression, respectively, that is attributed to each feature as the data falls through the trees in the forest. This is the feature importance measure implemented in scikit-learn, according to this Stack Overflow question.

This post investigates the impact of correlations between features on the feature importance measure.

Consider using a random forest as a model for a function f(x,y)$f(x,y)$ of two variables x[0,1]$x\in[0,1]$ and y[0,1]$y\in[0,1]$:

f(x,y)=2+x+y+ϵ,

where ϵ$\epsilon$ is normally distributed noise.

I generated data according to the above model f$f$ and trained a random forest on this data. Below is the training data set.

I created a grid in the x$x$-y$y$ plane to visualize the surface learned by the random forest. It mimics the model f$f$.

The comparison between the out of bag prediction and the true value of f$f$ in the training data is shown in the following 2D histogram. The blue diagonal line is a perfect prediction. I added a noise term ϵ$\epsilon$ with variance 0.1, so the root mean square error from the out of bag examples is excellent.

Further, the variable importance from scikit-learn gives what we’d expect; x$x$ and y$y$ are equally important in reducing the mean-square error.

Now, what happens when we introduce a third feature, z$z$, into our training data that is generated by the same underlying model f(x,y)$f(x,y)$?

Case 1: z$z$ has zero correlation with x$x$ and y$y$

I simulated a case where z$z$ is not correlated with x$x$ or y$y$ at all by generating z$z$ as an independent, uniformly distributed number. I then trained a random forest on the feature [x,y,z]$[x,y,z]$. The forest still performs very well on the training data, despite having an irrelevant variable thrown into the mix in my attempt to confuse the trees. Its root mean square error is increased by only 0.01 from when we trained on the feature [x,y]$[x,y]$. This is another advantage of decision forests; decision trees are not so confused by irrelevant variables.

For the feature importance, the trees picked up on the fact z$z$ is irrelevant, as the trees just ignored z$z$ by not considering it for making splits.

Case 2: z$z$ is correlated with y$y$

Next, in my generation of the training data, I used the same model f(x,y)$f(x,y)$ but let z=y$z=y$ so that z$z$ and y$y$ are perfectly, positively correlated. I then trained a random forest using the feature [x,y,z]$[x,y,z]$. Again, the forest’s prediction was very good, with the same mean square error as in Case 1.

However, when considering the feature importance, it looks very different from Case 1.

We now have that x$x$, y$y$, and z$z$ have roughly equal importance. This is intuitive, as x$x$ and y$y$ have equal importance in the model f$f$, and essentially we could write the model as f(x,z)=2+x+z+ϵ$f(x,z)=2+x+z+\epsilon$ since z$z$ is a proxy for y$y$.

Still, in some philosophical sense, z$z$ is not important at all, as we could remove z$z$ from the feature vector and get the same quality of prediction!

The IPython Notebook for this analysis can be viewed here and downloaded on Github.

[1] Breiman, L. Machine learning 2001, 45, 5-32.