-
Notifications
You must be signed in to change notification settings - Fork 23
/
forest.py
306 lines (252 loc) · 10.9 KB
/
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import math
from collections import Counter
import numpy as np
from pycompss.api.api import compss_wait_on
from pycompss.api.parameter import Type, COLLECTION_IN, Depth
from pycompss.api.task import task
from sklearn.base import BaseEstimator
from sklearn.utils import check_random_state
from dislib.classification.rf.decision_tree import DecisionTreeClassifier
from dislib.data.array import Array
from dislib.utils.base import _paired_partition
from dislib.classification.rf._data import transform_to_rf_dataset
class RandomForestClassifier(BaseEstimator):
"""A distributed random forest classifier.
Parameters
----------
n_estimators : int, optional (default=10)
Number of trees to fit.
try_features : int, str or None, optional (default='sqrt')
The number of features to consider when looking for the best split:
- If "sqrt", then `try_features=sqrt(n_features)`.
- If "third", then `try_features=n_features // 3`.
- If None, then `try_features=n_features`.
Note: the search for a split does not stop until at least one
valid partition of the node samples is found, even if it requires
to effectively inspect more than ``try_features`` features.
max_depth : int or np.inf, optional (default=np.inf)
The maximum depth of the tree. If np.inf, then nodes are expanded
until all leaves are pure.
distr_depth : int or str, optional (default='auto')
Number of levels of the tree in which the nodes are split in a
distributed way.
sklearn_max: int or float, optional (default=1e8)
Maximum size (len(subsample)*n_features) of the arrays passed to
sklearn's DecisionTreeClassifier.fit(), which is called to fit subtrees
(subsamples) of our DecisionTreeClassifier. sklearn fit() is used
because it's faster, but requires loading the data to memory, which can
cause memory problems for large datasets. This parameter can be
adjusted to fit the hardware capabilities.
hard_vote : bool, optional (default=False)
If True, it uses majority voting over the predict() result of the
decision tree predictions. If False, it takes the class with the higher
probability given by predict_proba(), which is an average of the
probabilities given by the decision trees.
random_state : int, RandomState instance or None, optional (default=None)
If int, random_state is the seed used by the random number generator;
If RandomState instance, random_state is the random number generator;
If None, the random number generator is the RandomState instance used
by `np.random`.
Attributes
----------
classes : None or ndarray
Array of distinct classes, set at fit().
trees : list of DecisionTreeClassifier
List of the tree classifiers of this forest, populated at fit().
"""
def __init__(self,
n_estimators=10,
try_features='sqrt',
max_depth=np.inf,
distr_depth='auto',
sklearn_max=1e8,
hard_vote=False,
random_state=None):
self.n_estimators = n_estimators
self.try_features = try_features
self.max_depth = max_depth
self.distr_depth = distr_depth
self.sklearn_max = sklearn_max
self.hard_vote = hard_vote
self.random_state = random_state
def fit(self, x, y):
"""Fits the RandomForestClassifier.
Parameters
----------
x : ds-array, shape=(n_samples, n_features)
The training input samples. Internally, its dtype will be converted
to ``dtype=np.float32``.
y : ds-array, shape=(n_samples, 1)
The target values.
Returns
-------
self : RandomForestClassifier
"""
self.classes = None
self.trees = []
dataset = transform_to_rf_dataset(x, y)
n_features = dataset.get_n_features()
try_features = _resolve_try_features(self.try_features, n_features)
random_state = check_random_state(self.random_state)
self.classes = dataset.get_classes()
if self.distr_depth == 'auto':
dataset.n_samples = compss_wait_on(dataset.get_n_samples())
distr_depth = max(0, int(math.log10(dataset.n_samples)) - 4)
distr_depth = min(distr_depth, self.max_depth)
else:
distr_depth = self.distr_depth
for i in range(self.n_estimators):
tree = DecisionTreeClassifier(try_features, self.max_depth,
distr_depth, self.sklearn_max,
bootstrap=True,
random_state=random_state)
self.trees.append(tree)
for tree in self.trees:
tree.fit(dataset)
return self
def predict_proba(self, x):
"""Predicts class probabilities using a fitted forest.
The probabilities are obtained as an average of the probabilities of
each decision tree.
Parameters
----------
x : ds-array, shape=(n_samples, n_features)
The input samples.
Returns
-------
probabilities : ds-array, shape=(n_samples, n_classes)
Predicted probabilities for the samples to belong to each class.
The columns of the array correspond to the classes given at
self.classes.
"""
assert self.trees is not None, 'The random forest is not fitted.'
prob_blocks = []
for x_row in x._iterator(axis=0):
tree_predictions = []
for tree in self.trees:
tree_predictions.append(tree.predict_proba(x_row))
prob_blocks.append([_join_predictions(*tree_predictions)])
self.classes = compss_wait_on(self.classes)
n_classes = len(self.classes)
probabilities = Array(blocks=prob_blocks,
top_left_shape=(x._top_left_shape[0], n_classes),
reg_shape=(x._reg_shape[0], n_classes),
shape=(x.shape[0], n_classes), sparse=False)
return probabilities
def predict(self, x):
"""Predicts classes using a fitted forest.
Parameters
----------
x : ds-array, shape=(n_samples, n_features)
The input samples.
Returns
-------
y_pred : ds-array, shape=(n_samples, 1)
Predicted class labels for x.
"""
assert self.trees is not None, 'The random forest is not fitted.'
pred_blocks = []
if self.hard_vote:
for x_row in x._iterator(axis=0):
tree_predictions = []
for tree in self.trees:
tree_predictions.append(tree.predict(x_row))
pred_blocks.append(_hard_vote(self.classes, *tree_predictions))
else:
for x_row in x._iterator(axis=0):
tree_predictions = []
for tree in self.trees:
tree_predictions.append(tree.predict_proba(x_row))
pred_blocks.append(_soft_vote(self.classes, *tree_predictions))
y_pred = Array(blocks=[pred_blocks],
top_left_shape=(x._top_left_shape[0], 1),
reg_shape=(x._reg_shape[0], 1), shape=(x.shape[0], 1),
sparse=False)
return y_pred
def score(self, x, y):
"""Accuracy classification score.
Returns the mean accuracy on the given test data.
Parameters
----------
x : ds-array, shape=(n_samples, n_features)
The training input samples.
y : ds-array, shape (n_samples, 1)
The true labels.
Returns
-------
score : float (as future object)
Fraction of correctly classified samples.
"""
assert self.trees is not None, 'The random forest is not fitted.'
partial_scores = []
if self.hard_vote:
for x_row, y_row in _paired_partition(x, y):
tree_predictions = []
for tree in self.trees:
tree_predictions.append(tree.predict(x_row))
subset_score = _hard_vote_score(y_row._blocks, self.classes,
*tree_predictions)
partial_scores.append(subset_score)
else:
for x_row, y_row in _paired_partition(x, y):
tree_predictions = []
for tree in self.trees:
tree_predictions.append(tree.predict_proba(x_row))
subset_score = _soft_vote_score(y_row._blocks, self.classes,
*tree_predictions)
partial_scores.append(subset_score)
return _merge_scores(*partial_scores)
@task(returns=1)
def _resolve_try_features(try_features, n_features):
if try_features is None:
return n_features
elif try_features == 'sqrt':
return int(math.sqrt(n_features))
elif try_features == 'third':
return max(1, n_features // 3)
else:
return int(try_features)
@task(returns=1)
def _join_predictions(*predictions):
aggregate = predictions[0]
for p in predictions[1:]:
aggregate += p
labels = aggregate / len(predictions)
return labels
@task(returns=1)
def _soft_vote(classes, *predictions):
aggregate = predictions[0]
for p in predictions[1:]:
aggregate += p
labels = classes[np.argmax(aggregate, axis=1)]
return labels
@task(returns=1)
def _hard_vote(classes, *predictions):
mode = np.empty((len(predictions[0]),), dtype=int)
for sample_i, votes in enumerate(zip(*predictions)):
mode[sample_i] = Counter(votes).most_common(1)[0][0]
labels = classes[mode]
return labels
@task(y_blocks={Type: COLLECTION_IN, Depth: 2}, returns=1)
def _soft_vote_score(y_blocks, classes, *predictions):
real_labels = Array._merge_blocks(y_blocks).flatten()
aggregate = predictions[0]
for p in predictions[1:]:
aggregate += p
predicted_labels = classes[np.argmax(aggregate, axis=1)]
correct = np.count_nonzero(predicted_labels == real_labels)
return correct, len(real_labels)
@task(y_blocks={Type: COLLECTION_IN, Depth: 2}, returns=1)
def _hard_vote_score(y_blocks, classes, *predictions):
real_labels = Array._merge_blocks(y_blocks).flatten()
mode = np.empty((len(predictions[0]),), dtype=int)
for sample_i, votes in enumerate(zip(*predictions)):
mode[sample_i] = Counter(votes).most_common(1)[0][0]
predicted_labels = classes[mode]
correct = np.count_nonzero(predicted_labels == real_labels)
return correct, len(real_labels)
@task(returns=1)
def _merge_scores(*partial_scores):
correct = sum(subset_score[0] for subset_score in partial_scores)
total = sum(subset_score[1] for subset_score in partial_scores)
return correct / total