← All algorithms

Classification

Random Forest

2023-12-19

Ensemble learning combines predictions from multiple models, which may use different algorithms or different instances of the same algorithm. For classification, majority voting is one way to combine their predictions. Ensemble learning can help reduce overfitting.

Random Forest can be considered as an ensemble model of decision trees. Random Forest consists of sampling the source parameters and the training data for training each tree and making predictions based on the majority vote of these predictions.

​In this blog, we assume readers are familiar with the decision tree in other blog.

The steps to apply the Random Forest are as follows:

(step 1) Randomly select n number of the bootstrap samples from the entire dataset (i.e., with the possibility of duplicates)

(step 2) Select d number of the parameters from the entire Source Para without allowing duplicates.

(step 3) Construct a decision tree using the data from (step 1 & 2).

​(step 4) Repeat (step 1, 2, & 3) to build k number of the decision trees.

(step 5) Perform inference to the new sample. Each tree will generate one prediction. Final estimation is carried out by a majority vote for the Classification, or arithmetic average of all the predictions for Regression.

For the demonstration of the Random Forest in Excel, we used the Iris dataset with n=100, d=2, & k=10. In other words, each tree uses size of 100 samples and two source parameters to build, and nine trees are planted for the majority vote.

​

Iris dataset is formatted as the table below.​

​

(step 1) Randomly pick 100 samples from the entire dataset using RANDBETWEEN(1,150) function.

(step 2) Randomly choose two source parameters from the entire source parameter using RANDBETWEEN(2,5) function.

(step 3) The decision tree uses the same worksheet as the previous "decision tree" and only replaces the test set with the entire data.

(Step 4) Copy each decision tree from tree_1 to tree_10 onto each Worksheet.

(Step 5) For each test data, calculate the mode value from the 10 predicted values obtained from each decision tree using the MODE() function.

​Following are results after the majority vote for the entire data.

​

They are also presented in the following confusion matrix.

The d, n, and k used in Random Forest are hyperparameters that can control bias-variance trade-off. Generally, as k gets larger (i.e., more trees are planted), the performance improves. On the other hand, as n gets smaller, the variance increases, reducing overfitting but compromising performance (accuracy). The commonly used value for d is sqrt(the number of features in the dataset).