-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbagging.py
109 lines (88 loc) · 4.29 KB
/
bagging.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 multiprocessing as mp
from functools import partial
import numpy as np
from ..base import BaseClassifier,BaseRegressor
from ..ml.decison_tree import DecisionTreeClassifier,DecisionTreeRegressor
class BaseBagging:
def __init__(self,base_model,basemodel_params={},n_estimators = 20,samples_ratio=0.8,features_ratio=0.5,parallelize=False):
self.base_model = base_model
self.basemodel_params = basemodel_params
self.parallelize = parallelize
self.n_estimators = n_estimators
self.samples_ratio = samples_ratio
self.features_ratio = features_ratio
self.estimators = []
def fit(self,X,y):
# Draw bootstrap samples
n_sample = int(X.shape[0] * self.samples_ratio)
samples_idx = np.random.choice(X.shape[0],size=self.n_estimators*n_sample ,replace=True).reshape(self.n_estimators,n_sample)
# Draw bootstrap features
n_feature = int(X.shape[1] * self.features_ratio)
self.features_idx = np.random.choice(X.shape[1],size=self.n_estimators *n_feature ,replace=True).reshape(self.n_estimators,n_feature)
# Create base estimators
self.estimators = []
[self.estimators.append(self.base_model(**self.basemodel_params)) for _ in range(self.n_estimators)]
# Fit the estimators
if self.parallelize :
fit_func = [partial(self.estimators[i].fit,X[samples_idx[i]][:,self.features_idx[i]],y[samples_idx[i]]) for i in range(self.n_estimators)]
pool = mp.Pool(mp.cpu_count())
[pool.apply(fit_func[i]) for i in range(self.n_estimators)]
else :
for i in range(self.n_estimators) :
self.estimators[i].fit(X[samples_idx[i]][:,self.features_idx[i]],y[samples_idx[i]])
def predict_all_learners(self,X):
'''Make predictions with all learners) '''
if self.parallelize :
pool = mp.Pool(mp.cpu_count())
for i in range(self.n_estimators):
res = [pool.apply(self.estimators[i].predict,(X[:,self.features_idx[i]],)) for i in range(self.n_estimators)]
else :
res = []
for i in range(self.n_estimators):
res.append(self.estimators[i].predict(X[:,self.features_idx[i]]))
return res
class BagginClassifier(BaseBagging,BaseClassifier):
''' Bagging Classfier
Parameters
----------
basetree : object,
decision tree classifier object with fit and predict methods
(may be also another base estimator)
basetree_params : dict,
parameters of base tree
parallelize : bool,
n_estimators : int,
number of trees in the forest
'''
def __init__(self,base_model=DecisionTreeClassifier,basemodel_params={},n_estimators = 20,samples_ratio=0.8,features_ratio=0.5,parallelize=True):
super().__init__(base_model=base_model,basemodel_params=basemodel_params,n_estimators=n_estimators,samples_ratio=samples_ratio,features_ratio=features_ratio,parallelize=parallelize)
def fit(self,X,y):
self.labels = np.unique(y)
return super().fit(X,y)
def predict(self,X):
res = super().predict_all_learners(X)
# Take the most common value
res = np.array(res)
decision = []
for col in range(res.shape[1]):
values,counts = np.unique(res[:,col],return_counts=True)
decision.append(values[counts.argmax()])
return decision
class BaggingRegressor(BaseBagging,BaseRegressor):
''' Bagging Classfier
Parameters
----------
basetree : object,
decision tree regressor object with fit and predict methods
(may be also another base estimator)
basetree_params : dict,
parameters of base tree
parallelize : bool,
n_estimators : int,
number of trees in the forest
'''
def __init__(self,base_model=DecisionTreeRegressor,basemodel_params={},n_estimators = 20,samples_ratio=0.8,features_ratio=0.5,parallelize=True):
super().__init__(base_model=base_model,basemodel_params=basemodel_params,n_estimators=n_estimators,samples_ratio=samples_ratio,features_ratio=features_ratio,parallelize=parallelize)
def predict(self,X):
res = self.predict_all_learners(X)
return np.mean(res,axis=0)