Page 96 - Data Science Algorithms in a Week
P. 96
Random Forest
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[random.randint(0, len(population) - 1)])
return sample
def construct_random_forest(verbose, heading, complete_data,
enquired_column, m, tree_count):
printfv(2, verbose, "*** Random Forest construction ***\n")
printfv(2, verbose, "We construct a random forest that will " +
"consist of %d random decision trees.\n", tree_count)
random_forest = []
for i in range(0, tree_count):
printfv(2, verbose, "\nConstruction of a random " +
"decision tree number %d:\n", i)
random_forest.append(construct_random_decision_tree(
verbose, heading, complete_data, enquired_column, m))
printfv(2, verbose, "\nTherefore we have completed the " +
"construction of the random forest consisting of %d " +
"random decision trees.\n", tree_count)
return random_forest
def construct_random_decision_tree(verbose, heading, complete_data,
enquired_column, m):
sample = sample_with_replacement(complete_data, len(complete_data))
printfv(2, verbose, "We are given %d features as the input data. " +
"Out of these, we choose randomly %d features with the " +
"replacement that we will use for the construction of " +
"this particular random decision tree:\n" +
str(sample) + "\n", len(complete_data),
len(complete_data))
# The function construct_general_tree from the module decision_tree
# is written in the implementation section in the previous chapter
# on decision trees.
return decision_tree.construct_general_tree(verbose, heading,
sample,
enquired_column, m)
# M is the given number of the decision variables, i.e. properties
# of one feature.
def choose_m(verbose, M):
[ 84 ]