-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeTrees.py
More file actions
41 lines (35 loc) · 1.34 KB
/
Copy pathmakeTrees.py
File metadata and controls
41 lines (35 loc) · 1.34 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
from abc import ABC, abstractmethod
import time
import multiprocessing
import logging
class MakeTrees(ABC):
@abstractmethod
def make_decisions_trees(self,dataset,rf):
pass
class Multiprocessing(MakeTrees):
def _target(self, dataset, nproc,rf):
print('process {} starts'.format(nproc))
initial_depth = 1
tree = rf._make_node(dataset, initial_depth)
print('process {} ends'.format(nproc))
return tree
def make_decisions_trees(self,dataset,rf):
t1 = time.time()
with multiprocessing.Pool() as pool:
list_of_trees = pool.starmap(self._target, [(dataset, nprocess,rf) for nprocess in range(rf.num_trees)])
# use pool.map instead if only one argument for _target
t2 = time.time()
print('{} seconds per tree'.format((t2 - t1) / rf.num_trees))
return list_of_trees
class Lineal(MakeTrees):
def make_decisions_trees(self,dataset,rf):
list_of_trees = []
initial_depth = 1
t1 = time.time()
for _ in range(rf.num_trees):
train_dataset = dataset.select_samples(rf.ratio_samples)
tree = rf._make_node(train_dataset, initial_depth)
list_of_trees.append(tree)
t2 = time.time()
print('{} seconds per tree'.format((t2 - t1) / rf.num_trees))
return list_of_trees