-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_forest.py
109 lines (78 loc) · 2.95 KB
/
random_forest.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import numpy as np
from decision_tree import DecisionTreeClassifier, DecisionTreeRegressor
class RandomForestClassifier:
"""
A random forest classifier.
"""
def __init__(self, n_trees=100, criterion="gini", max_depth=2, min_samples_split=2):
self.n_trees = n_trees
self.criterion = criterion
self.max_depth = max_depth
self.min_samples_split = min_samples_split
# Plant trees
self.trees = []
for _ in range(n_trees):
tree = DecisionTreeClassifier(criterion, max_depth, min_samples_split)
self.trees.append(tree)
def fit(self, X, y):
# Train each tree with a random data subset
subsets = self.get_random_subsets(X, y, self.n_trees)
for i in range(self.n_trees):
X_subset, y_subset = subsets[i]
self.trees[i].fit(X_subset, y_subset)
def predict(self, X):
# Majority vote
tree_preds = np.array([tree.predict(X) for tree in self.trees])
tree_preds = np.swapaxes(tree_preds, 0, 1)
y_pred = [self.get_most_common(tree_pred) for tree_pred in tree_preds]
return y_pred
def get_random_subsets(self, X, y, n_subsets):
n_samples = X.shape[0]
subsets = []
for _ in range(n_subsets):
indices = np.arange(n_samples)
np.random.shuffle(indices)
X_ = X[indices]
y_ = y[indices]
subsets.append((X_, y_))
return subsets
def get_most_common(self, y):
if len(y) == 0:
return None
return np.bincount(y).argmax()
class RandomForestRegressor:
"""
A random forest regressor.
"""
def __init__(self, n_trees=100, criterion="mse", max_depth=2, min_samples_split=2):
self.n_trees = n_trees
self.criterion = criterion
self.max_depth = max_depth
self.min_samples_split = min_samples_split
# Plant trees
self.trees = []
for _ in range(n_trees):
tree = DecisionTreeRegressor(criterion, max_depth, min_samples_split)
self.trees.append(tree)
def fit(self, X, y):
# Train each tree with a random data subset
subsets = self.get_random_subsets(X, y, self.n_trees)
for i in range(self.n_trees):
X_subset, y_subset = subsets[i]
self.trees[i].fit(X_subset, y_subset)
def predict(self, X):
# Average
tree_preds = np.array([tree.predict(X) for tree in self.trees])
tree_preds = np.swapaxes(tree_preds, 0, 1)
y_pred = [np.mean(tree_pred) for tree_pred in tree_preds]
return y_pred
def get_random_subsets(self, X, y, n_subsets):
n_samples = X.shape[0]
subsets = []
for _ in range(n_subsets):
indices = np.arange(n_samples)
np.random.shuffle(indices)
X_ = X[indices]
y_ = y[indices]
subsets.append((X_, y_))
return subsets