Random Forest Regression: When Does It Fail and Why?
Tags: machinelearning, python, datascience, regression
Published: 2020-08-18 22:33:26

*This article was originally written by [Derrick Mwiti](https://www.linkedin.com/in/mwitiderrick/) and posted on the [Neptune blog](https://neptune.ai/blog/random-forest-regression-when-does-it-fail-and-why?utm_source=devto&utm_medium=crosspost&utm_campaign=blog-random-forest-regression-when-does-it-fail-and-why).*

---

In this article, we’ll look at a major problem with using Random Forest for Regression which is **extrapolation.** 

We’ll cover the following items:

* Random Forest Regression vs Linear Regression
* Random Forest Regression Extrapolation Problem
* Potential solutions
* Should you use Random Forest for Regression?

Let’s dive in.

---

# Random Forest Regression vs Linear Regression

Random Forest Regression is quite a robust algorithm, however, the question is **should you use it for regression?**

Why not use linear regression instead?  The function in a Linear Regression can easily be written as y=mx + c while a function in a complex Random Forest Regression seems like a black box that can’t easily be represented as a function. 

Generally, Random Forests produce better results, work well on large datasets, and are able to work with missing data by creating estimates for them. However, they pose a major challenge that is that they can’t extrapolate outside unseen data. We’ll dive deeper into these challenges in a minute 

### Decision Tree Regression

Decision Trees are great for obtaining non-linear relationships between input features and the target variable.

The **inner working of a Decision Tree can be thought of as a bunch of if-else conditions.**

It starts at the very top with one node. This node then splits into a left and right node — decision nodes. These nodes then split into their respective right and left nodes. 

At the end of the leaf node, the average of the observation that occurs within that area is computed. The most bottom nodes are referred to as leaves or terminal nodes.

The value in the leaves is usually the mean of the observations occurring within that specific region. For instance in the right most leaf node below, 552.889 is the average of the 5 samples.

![image](https://lh5.googleusercontent.com/d5QSiPN3bIKdLCWkdOlnRLpLleMMo5ut904gtGUBP3Q3244u1BVMHgqkcXeEo9HtLoRU6agt--Y_U_aG1Oxosf7voq9YBcxJOIQ6cW2YiSmQZ2zLLZO-CcVsK46powAHxlPrzoDC)

How far this splitting goes is what is known as the depth of the tree. This is one of the hyperparameters that can be tuned. The maximum depth of the tree is specified so as to prevent the tree from becoming too deep — a scenario that leads to overfitting. 

### Random Forest Regression

Random forest is an ensemble of decision trees. This is to say that many trees, constructed in a certain “random” way form a Random Forest. 

* Each tree is created from a different sample of rows and at each node, a different sample of features is selected for splitting. 
* Each of the trees makes its own individual prediction. 
* These predictions are then averaged to produce a single result. 

![image](https://lh5.googleusercontent.com/ZzEagZv-a3KFCSr610aEvXIPTRTWV_cFdgpXoDFlj_r7A8ex5L0aE33aLuQptfngJLiT5xX3yk8LwGAyvOVY9rIsqe1ZZmIvg71yQIdXuVxuPpgmvm85aAxmP32M-ODq6_E3uxrV)
*source: [Wikimedia](https://commons.wikimedia.org/wiki/File:Random_forest_diagram_complete.png)*

The averaging makes a Random Forest better than a single Decision Tree hence improves its accuracy and reduces overfitting. 

A prediction from the Random Forest Regressor is an average of the predictions produced by the trees in the forest. 

### Example of trained Linear Regression and Random Forest

In order to dive in further, let’s look at an example of a Linear Regression and a Random Forest Regression. For this, we’ll apply the Linear Regression and a Random Forest Regression to the same dataset and compare the result. 

Let’s take this example dataset where you should predict the price of diamonds based on other features like carat, depth, table, x, y and z. If we look at the distribution of price below:

![image](https://lh5.googleusercontent.com/6XreM_NDuvsLtTK-nyeaT2kEbD6-ENHcc5DCbjM4_n04kVXQIzPwU_-rb18OKK_uZfqRQfq7x1CjcJxSJxdENi5KVVaVVuiUZZ3ahOxqWGI96UaEUr955FGw8Tri0KSam_31FvHS)

![image](https://lh3.googleusercontent.com/FQ3TkJhrVdNFzo40aYyHgO2bwXYAbD7kgX6EHi_bvH08kbGEcu55OKNGMKCU5_iSWEaCW71oq5ysyyWFCC0rw0Ahi-miGxWvyFe_TwP2Msjo1E1iOHcdMygFmLKirbuOA7IS8alq)

We can see that the **price ranges from 326 to 18823.**

Let’s train the Linear Regression model and run predictions on the validation set.

The distribution of predicted prices is the following:

![image](https://lh3.googleusercontent.com/K_XtAJSvytdOYMrU6gqmhAPzYwhYd7-PKxfegUceQP3pj8ePXcPDK9QtOVwjeT8SbfQr3RCDG8aKst02qGnvfQ49C8FC1nyx0joQddgEgIlUal4o9Gpgcn7CKfxvhW5NM-Nx51Oq)

![image](https://lh6.googleusercontent.com/bYkQceurBuITEmOiGaaRY6e0W8DItepqjEWP3QCVS-Wn-UP0EaXr3hjVOSX0-1NtcZ_oxECfSNOSZ_UH5_FWOgc31W0axILap77zFFo3ciEhECzL3At-tX4fgjO69P3GhYjQkmlZ)

**Predicted prices are clearly outside the range of values of ''price'' seen in the training dataset.**

A Linear Regression model, just like the name suggests, created a linear model on the data. A simple way to think about it is in the form of `y = mx+C`. Therefore, since it fits a linear model, it is able to obtain values outside the training set during prediction. **It is able to extrapolate based on the data.**

Let’s now look at the results obtained from a Random Forest Regressor using the same dataset.

![image](https://lh6.googleusercontent.com/93-amJ52_eGP2poE6wMoMG4RWkvH5Jw-w7fwSfq0unViy0TttQy8pUSHzzAKv9nSrqjU2LJqrht18N4tdRnylHQ5w_A4MGGkAiXzMmL1N_Jd1JuecRTH7d71oav74iv-Ca1Ysxt7)

![image](https://lh3.googleusercontent.com/cirOkDSeWWMEd947nOxhcQf7jE5nxpiIDd4SsXkTUmigVvCS9-wVenoAdS34EmPomFyR75WsJbwnRkPudVgMY8NUTsk2ahwsJxVdtQmMQxGP_FkyZYhlgDVi4HRT09Yq1ixuuFMK)

These values are clearly **within the range of 326 and 18823**  -  just like in our training set. There are no values outside that range. **Random Forest cannot extrapolate.**

---

# Extrapolation Problem 

As you have seen above, when using a Random Forest Regressor, the predicted values are never outside the training set values for the target variable.

If you look at prediction values they will look like this:

![image](https://lh3.googleusercontent.com/aTc9sANC3x-luOlDmSVMy9rUkSp6K1JMYgODnWl_2iPaAfPgk-ee8Sm2orKIxl-LDnVss8u11_IxgpuLuFhBF_4yOcwl2LwsDXJ2xHHQZS_DUghDK-jU2kX1-tgX3s24WZz-euja)

*[source](https://www.researchgate.net/publication/327298817_Random_forest_as_a_generic_framework_for_predictive_modeling_of_spatial_and_spatio-temporal_variables): Hengl, Tomislav et. al “Random forest as a generic framework for predictive modeling of spatial and spatio-temporal variables”. PeerJ. 6. e5518. 10.7717/peerj.5518.*

Wondering why?

Let’s explore that phenomenon here. The data used above has the following columns carat, depth, table, x, y, z for predicting the price.

The diagram below shows one decision tree from the Random Forest Regressor. 

![image](https://lh4.googleusercontent.com/geF5UxG5sX2r4OSXvoRW0An6lsrucPUdOzC3_qP2yoVNreLRhx-Q6XOZmEgGiQknDAlh1cIxbubJISwxy2of_QM-KVBvBEWA6PzOcICek3Ol9wqoLb8NWbvbG_lUzn-okQmX1UiP)

Let’s zoom in to a smaller section of this tree. For example, there are 4 samples with depth <= 62.75, x <= 5.545, carat <= 0.905, and z <= 3.915. The price being predicted for these is 2775.75. This figure represents the mean of all these four samples. Therefore, **any value in the test set that falls in this leaf will be predicted as 2775.75.**

![image](https://lh4.googleusercontent.com/tpwTzOrAuYhpaWu-sybNBCQkFBdaTZhP845A-YhNs_4fTBV-2b4FcAcuMIUqKycwP5RbT7vmPGEhXNm3AdeGVwyhPfDS_yZyO15pRhlZaslocc8scoasQtdsDtXbTP_gd9z4FweF)

This is to say that when the Random Forest Regressor is tasked with the problem of predicting for values not previously seen, it will always predict an average of the values seen previously. Obviously the average of a sample can not fall outside the highest and lowest values in the sample. 

The **Random Forest Regressor is unable to discover trends that would enable it in extrapolating values that fall outside the training set.** When faced with such a scenario, the regressor assumes that the prediction will fall close to the maximum value in the training set. Figure above illustrates that.

### Potential Solutions

Ok, so how can you deal with this extrapolation problem?

There are a couple of options:
* Use a linear model such as SVM regression, Linear Regression, etc,
* Build a deep learning model because neural nets are able to extrapolate (they are basically stacked linear regression models on steroids),
* Combine predictors using stacking. For example, you can create a stacking regressor using a Linear model and a Random Forest Regressor.,
* **Use modified versions of random forest**.

One of such extensions is [Regression-Enhanced Random Forests](https://arxiv.org/pdf/1904.10416.pdf) (RERFs). The authors of this paper propose a technique borrowed from the strengths of penalized parametric regression to give better results in extrapolation problems.

Specifically there are two steps to the process:

* run Lasso before Random Forest, 
* train a Random Forest on the residuals from Lasso. 

Since Random Forest is a fully nonparametric predictive algorithm, it may not efficiently incorporate known relationships between the response and the predictors. The response values are the observed values Y1, . . . , Yn  from the training data. RERFs are able to incorporate known relationships between the responses and the predictors which is another benefit of using Regression-Enhanced Random Forests for regression problems. 

![image](https://lh6.googleusercontent.com/dbxTPJM7BudrFSYY4lSDneKHYwxkt5XvXgRRU-3FMAzDflmOr96tVdcBW6idIjj556UqOwqLHOkPVF9Fm_pOPWPrxnjI4U8kAoOXFXB9nHWNepcK_ZjQtXfdIFAmRszn-0lF94MO)

*[source](https://arxiv.org/abs/1904.10416): Haozhe Zhang et. al 2019, Regression-Enhanced Random Forests*

---

# Final Thoughts
At this point, I am sure you might be wondering whether or not you should use a Random Forest for regression problems.

Let’s look at that. 

### When to use it
* When the data has a non-linear trend and extrapolation outside the training data is not important.

### When not to use it
* When your data is in time series form. Time series problems require identification of a growing or decreasing trend that a Random Forest Regressor will not be able to formulate. 

Hopefully, this article gave you some background into the inner workings of Random Forest Regression.

---

*This article was originally written by [Derrick Mwiti](https://www.linkedin.com/in/mwitiderrick/) and posted on the [Neptune blog](https://neptune.ai/blog/random-forest-regression-when-does-it-fail-and-why?utm_source=devto&utm_medium=crosspost&utm_campaign=blog-random-forest-regression-when-does-it-fail-and-why) where you can find more in-depth articles for machine learning practitioners.*