We implement a random forest algorithm using a modified decision tree algorithm from the previous chapter. We also add an option to set a verbose mode within the program that can describe the whole process of how the algorithm works on a specific input—how a random forest is constructed with its random decision trees, and how this constructed random forest is used to classify other features.
You are encouraged to consult the decision_tree.construct_general_tree function from the previous chapter:
# source_code/4/random_forest.py
import math
import random
import sys
sys.path.append('../common')
import common # noqa
import decision_tree # noqa
from common import printfv # noqa
#Random forest construction
def sample_with_replacement(population, size):
sample = []
for i in range(0, size):
sample.append(population...