Decision tree
Random Forest is a method for classification, regression, and some kinds of prediction. The method is based on the decision tree definition as a binary tree-like graph of decisions and possible consequences.

The image above shows a possible decision tree for a training set of items with three features X, Y, and Z. Z is the resolution feature to classify items by. Usually, a resolution feature contains categorical values; for numerical versions of algorithms, categorical values should be replaced by numeric codes. A training set is a number of items with known values of Z. Each terminal node contains a number of items from the training set after the decision tree have been generated, and an average value of Z feature can be calculated as an arithmetic mean and may be considered as a predicted value.
For example, there is an item {X=9, Y=3, Z=?} and prediction of a value of Z feature which is required. After the descent, according to the tree graph and conditions, we could say Z is equal to an arithmetic mean of Z values for training items from terminal node #4.
Generation of decision tree
The algorithm to generate a decision tree is recursive. Every iteration finds the best way to split a current training subset into two parts and get a dividing condition. Which division is the best? There are some criteria. Gini coefficient is more suitable for the case of categorical values (classification). Since example values are numerical (regression), the RSS (residual sum of squares) criteria will be applied, but the Gini coefficient is an option as well.
The following steps describe splitting algorithms in general. For each feature (except resolution feature, Z in the example above).
- Calculate RSS for the current subset;
- Sort items by feature value;
- For each possible division into two subsets, calculate RSS for left (L) and right (R) subset;
- Find the division with min(RSS(L)+RSS(R)) and return feature name and value.
Criteria to stop the recursion
- RSS for the current subset is equal to 0;
- Items count in the current subset is less than or equal to the acceptable maximum number of items in a terminal node.
A decision tree will be generated and the training set will be divided into a number of categories (Terminal nodes) as a result.
Random forest
A forest is a number of trees. And what is a "random" forest? It is a number of decision trees generated based on a random subset of the initial training set. The important thing is that any random subset to generate a decision tree can contain duplicate training items. Each tree of a forest makes its prediction; a prediction of a forest is an arithmetic mean of all trees' predictions.
Library
First, prepare Microsoft Excel file with training data in a format like below.


Join the conversation! Your thoughts help the community grow.