-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomForest.py
More file actions
53 lines (42 loc) · 1.97 KB
/
Copy pathRandomForest.py
File metadata and controls
53 lines (42 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from collections import defaultdict
from DecisionTree import DecisionTree
import random
class RandomForest:
# initializes random forest
def __init__(self, data: list, num_feat: int, num_trees = 20, min_sample_split = 10):
self.tree_features: dict[DecisionTree, list[int]] = {}
self.data: list = data
# initialize all trees in the forest
for i in range(num_trees):
random_sample, features = self.extract_random_sample(num_feat)
tree = DecisionTree(random_sample, min_sample_split)
tree.build()
self.tree_features[tree] = features
# extracts a sample to train a single tree
def extract_random_sample(self, num_feat: int) -> tuple[list, list]:
result = []
num_rows = len(self.data)
feature_indices = random.sample(range(len(self.data[0]) - 1), num_feat)
# extract subset of data with same amount of rows as input data
for i in range(num_rows):
row_idx = random.randint(0, num_rows - 1)
row = self.data[row_idx]
# sample num_feat features from the data
result.append([row[i] for i in feature_indices] + [row[-1]])
return result, feature_indices
# classify guess using this random forest
def predict(self, guess: list) -> str:
maj_pred = None
maj_occurences = 0
pred_occ = defaultdict(int)
# predict guess for each tree
for tree in self.tree_features.keys():
# only use the features that this tree was trained on
tree_input = [guess[i] for i in self.tree_features[tree]]
prediction = tree.predict(tree_input)
pred_occ[prediction] += 1
# majority prediction logic
if (pred_occ[prediction] > maj_occurences):
maj_occurences = pred_occ[prediction]
maj_pred = prediction
return maj_pred