This repository has been archived by the owner on Oct 3, 2022. It is now read-only.
/
bagging.py
272 lines (215 loc) · 8.63 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
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
from collections import Counter
import random
import numpy as np
from sklearn.ensemble.base import BaseEnsemble
from sklearn.ensemble.forest import ForestClassifier, RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
def get_asym_task(X, y):
# assume only 2 classes
large_class, small_class = [pair[0] for pair in Counter(y).most_common()]
X_small = np.array([X[i] for (i, cls) in enumerate(y)
if cls == small_class])
X_large = np.array([X[i] for (i, cls) in enumerate(y)
if cls == large_class])
y_new = np.array(([small_class] * len(X_small)) +
([large_class] * len(X_small)))
return X_small, X_large, y_new
class BalanceForcedRandomForestClassifier(ForestClassifier):
def __init__(self,
n_estimators=10,
criterion="gini",
max_depth=None,
min_samples_split=2,
min_samples_leaf=1,
min_density=0.1,
max_features="auto",
bootstrap=True,
compute_importances=False,
oob_score=False,
n_jobs=1,
random_state=None,
verbose=0):
super(BalanceForcedRandomForestClassifier, self).__init__(
base_estimator=DecisionTreeClassifier(),
n_estimators=n_estimators,
estimator_params=("criterion", "max_depth", "min_samples_split",
"min_samples_leaf", "min_density",
"max_features", "random_state"),
bootstrap=bootstrap,
compute_importances=compute_importances,
oob_score=oob_score,
n_jobs=n_jobs,
random_state=random_state,
verbose=verbose)
self.criterion = criterion
self.max_depth = max_depth
self.min_samples_split = min_samples_split
self.min_samples_leaf = min_samples_leaf
self.min_density = min_density
self.max_features = max_features
def fit(self, X, y, sample_weight=None):
if sample_weight is not None:
# undersampling will force balance
sample_weight = None
X_small, X_large, y_new = get_asym_task(X, y)
large_subset = np.array(random.sample(X_large, len(X_small)))
X_new = np.vstack((X_small, large_subset))
# shuffle data
zipped = zip(X_new, y_new)
random.shuffle(zipped)
X_new, y_new = zip(*zipped)
#print "fit %s on (%s, %s) from (%s, %s)" % (
# id(self),
# len(X_new), len(y_new),
# len(X), len(y)
#)
#print "kept: %s/%s | %s/%s" % (
# len(X_small), len(X_small),
# len(large_subset), len(X_large)
#)
return super(BalanceForcedRandomForestClassifier, self).fit(
X_new, y_new,
sample_weight)
class AsymBaggingRFCs(BaseEnsemble):
"""Addresses class imbalance by training an ensemble of RFCs
on all of the small class, and a random subset of the large class."""
def __init__(self,
asym_estimators=5,
# for RFC
n_estimators=10,
criterion="gini",
max_depth=None,
min_samples_split=2,
min_samples_leaf=1,
min_density=0.1,
max_features="auto",
bootstrap=True,
compute_importances=False,
oob_score=False,
n_jobs=1,
random_state=None,
verbose=0):
super(AsymBaggingRFCs, self).__init__(
base_estimator=BalanceForcedRandomForestClassifier(),
n_estimators=n_estimators,
estimator_params=("n_estimators", "criterion", "max_depth", "min_samples_split",
"min_samples_leaf", "min_density", "bootstrap", "compute_importances",
"oob_score", "n_jobs", "verbose",
"max_features", "random_state"),
)
self.asym_estimators = asym_estimators
self.n_estimators = n_estimators
self.criterion = criterion
self.max_depth = max_depth
self.min_samples_split = min_samples_split
self.min_samples_leaf = min_samples_leaf
self.min_density = min_density
self.bootstrap = bootstrap
self.compute_importances = compute_importances
self.oob_score = oob_score
self.n_jobs = n_jobs
self.verbose = verbose
self.max_features = max_features
self.random_state = random_state
# hack to make us look like a classifier
# this tells CV to use stratified shuffles
self.estimator = RandomForestClassifier()
def fit(self, X, y, sample_weight=None):
# sample weight is ignored - it'll be forced uniform
# clear a clone
self.estimators_ = []
for i in range(self.asym_estimators):
self._make_estimator()
for clf in self.estimators_:
clf.fit(X, y)
return self
def predict_proba(self, X):
all_proba = [clf.predict_proba(X) for clf in self.estimators_]
# reduce by average
proba = all_proba[0]
for j in xrange(1, len(all_proba)):
for k in xrange(2):
proba[k] += all_proba[j][k]
for k in xrange(2):
proba[k] /= self.asym_estimators
return proba
def predict(self, X):
proba = self.predict_proba(X)
return np.array([max(enumerate(a),
key=lambda x: x[1])[0]
for a in proba])
"""
class OldAsymBaggingRFCs(BaseEnsemble):
def __init__(self,
asym_estimators=5,
# for RFC
n_estimators=10,
criterion="gini",
max_depth=None,
min_samples_split=2,
min_samples_leaf=1,
min_density=0.1,
max_features="auto",
bootstrap=True,
compute_importances=False,
oob_score=False,
n_jobs=1,
random_state=None,
verbose=0):
super(AsymBaggingRFCs, self).__init__(
base_estimator=RandomForestClassifier(),
n_estimators=n_estimators,
estimator_params=("n_estimators", "criterion", "max_depth", "min_samples_split",
"min_samples_leaf", "min_density", "bootstrap", "compute_importances",
"oob_score", "n_jobs", "verbose",
"max_features", "random_state"),
)
self.asym_estimators = asym_estimators
self.n_estimators = n_estimators
self.criterion = criterion
self.max_depth = max_depth
self.min_samples_split = min_samples_split
self.min_samples_leaf = min_samples_leaf
self.min_density = min_density
self.bootstrap = bootstrap
self.compute_importances = compute_importances
self.oob_score = oob_score
self.n_jobs = n_jobs
self.verbose = verbose
self.max_features = max_features
self.random_state = random_state
# hack to make us look like a classifier
self.estimator = RandomForestClassifier()
def fit(self, X, y):
# remove from clone
self.estimators_ = []
for i in range(self.asym_estimators):
self._make_estimator()
X_small, X_large, y_new = get_asym_task(X, y)
for clf in self.estimators_:
# sample without replacement
large_subset = np.array(random.sample(X_large, len(X_small)))
X_new = np.vstack((X_small, large_subset))
# shuffle data
#zipped = zip(X_new, y_new[:])
#random.shuffle(zipped)
#X_new, y_new = zip(*zipped)
clf.fit(X_new, y_new)
def predict_proba(self, X):
return self.estimators_[0].predict_proba(X)
#all_proba = [clf.predict_proba(X) for clf in self.estimators_]
## reduce by average
#proba = all_proba[0]
#for j in xrange(1, len(all_proba)):
# for k in xrange(2):
# proba[k] += all_proba[j][k]
#for k in xrange(2):
# proba[k] /= self.asym_estimators
#return proba
def predict(self, X):
return self.estimators_[0].predict(X)
#proba = self.predict_proba(X)
#return np.array([max(enumerate(a),
# key=lambda x: x[1])[0]
# for a in proba])
"""