-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathrus.py
332 lines (268 loc) · 11 KB
/
rus.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
from collections import Counter
import numpy as np
from sklearn.base import is_regressor
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble._forest import BaseForest
from sklearn.preprocessing import normalize
from sklearn.tree import BaseDecisionTree
from sklearn.utils import check_random_state
from sklearn.utils import check_X_y
#from sklearn.utils import shuffle
class RandomUnderSampler(object):
"""Implementation of random undersampling (RUS).
Undersample the majority class(es) by randomly picking samples with or
without replacement.
Parameters
----------
with_replacement : bool, optional (default=True)
Undersample with replacement.
return_indices : bool, optional (default=False)
Whether or not to return the indices of the samples randomly selected
from the majority class.
random_state : int or None, optional (default=None)
If int, random_state is the seed used by the random number generator.
If None, the random number generator is the RandomState instance used
by np.random.
"""
def __init__(
self,
with_replacement=True,
return_indices=False,
random_state=None,
):
self.return_indices = return_indices
self.with_replacement = with_replacement
self.random_state = random_state
def sample(self, n_samples):
"""Perform undersampling.
Parameters
----------
n_samples : int
Number of samples to remove.
Returns
-------
S : array, shape = [n_majority_samples - n_samples, n_features]
Returns synthetic samples.
"""
np.random.seed(seed=self.random_state)
if self.n_majority_samples <= n_samples:
n_samples = self.n_majority_samples
idx = np.random.choice(
self.n_majority_samples,
size=self.n_majority_samples - n_samples,
replace=self.with_replacement,
)
if self.return_indices:
return self.X[idx], idx
else:
return self.X[idx]
def fit(self, X):
"""Train model based on input data.
Parameters
----------
X : array-like, shape = [n_majority_samples, n_features]
Holds the majority samples.
"""
self.X = X
self.n_majority_samples, self.n_features = self.X.shape
return self
class RUSBoost(AdaBoostClassifier):
"""Implementation of RUSBoost.
RUSBoost introduces data sampling into the AdaBoost algorithm by
undersampling the majority class using random undersampling (with or
without replacement) on each boosting iteration [1].
This implementation inherits methods from the scikit-learn
AdaBoostClassifier class, only modifying the `fit` method.
Parameters
----------
n_samples : int, optional (default=100)
Number of new synthetic samples per boosting step.
min_ratio : float (default=1.0)
Minimum ratio of majority to minority class samples to generate.
with_replacement : bool, optional (default=True)
Undersample with replacement.
base_estimator : object, optional (default=DecisionTreeClassifier)
The base estimator from which the boosted ensemble is built.
Support for sample weighting is required, as well as proper `classes_`
and `n_classes_` attributes.
n_estimators : int, optional (default=50)
The maximum number of estimators at which boosting is terminated.
In case of perfect fit, the learning procedure is stopped early.
learning_rate : float, optional (default=1.)
Learning rate shrinks the contribution of each classifier by
``learning_rate``. There is a trade-off between ``learning_rate`` and
``n_estimators``.
algorithm : {'SAMME', 'SAMME.R'}, optional (default='SAMME.R')
If 'SAMME.R' then use the SAMME.R real boosting algorithm.
``base_estimator`` must support calculation of class probabilities.
If 'SAMME' then use the SAMME discrete boosting algorithm.
The SAMME.R algorithm typically converges faster than SAMME,
achieving a lower test error with fewer boosting iterations.
random_state : int or None, optional (default=None)
If int, random_state is the seed used by the random number generator.
If None, the random number generator is the RandomState instance used
by np.random.
References
----------
.. [1] C. Seiffert, T. M. Khoshgoftaar, J. V. Hulse, and A. Napolitano.
"RUSBoost: Improving Classification Performance when Training Data
is Skewed". International Conference on Pattern Recognition
(ICPR), 2008.
"""
def __init__(
self,
n_samples=100,
min_ratio=1.0,
with_replacement=True,
base_estimator=None,
n_estimators=50,
learning_rate=1.,
algorithm="SAMME.R",
random_state=None,
):
self.n_samples = n_samples
self.min_ratio = min_ratio
self.algorithm = algorithm
self.rus = RandomUnderSampler(
with_replacement=with_replacement,
return_indices=True,
random_state=random_state,
)
super(RUSBoost, self).__init__(
base_estimator=base_estimator,
n_estimators=n_estimators,
learning_rate=learning_rate,
random_state=random_state,
)
def fit(self, X, y, sample_weight=None, minority_target=None):
"""Build a boosted classifier/regressor from the training set (X, y),
performing random undersampling during each boosting step.
Parameters
----------
X : {array-like, sparse matrix} of shape = [n_samples, n_features]
The training input samples. Sparse matrix can be CSC, CSR, COO,
DOK, or LIL. COO, DOK, and LIL are converted to CSR. The dtype is
forced to DTYPE from tree._tree if the base classifier of this
ensemble weighted boosting classifier is a tree or forest.
y : array-like of shape = [n_samples]
The target values (class labels in classification, real numbers in
regression).
sample_weight : array-like of shape = [n_samples], optional
Sample weights. If None, the sample weights are initialized to
1 / n_samples.
minority_target : int
Minority class label.
Returns
-------
self : object
Returns self.
Notes
-----
Based on the scikit-learn v0.18 AdaBoostClassifier and
BaseWeightBoosting `fit` methods.
"""
# Check that algorithm is supported.
if self.algorithm not in ("SAMME", "SAMME.R"):
raise ValueError("algorithm %s is not supported" % self.algorithm)
# Check parameters.
if self.learning_rate <= 0:
raise ValueError("learning_rate must be greater than zero")
if (self.base_estimator is None or isinstance(
self.base_estimator, (BaseDecisionTree, BaseForest)
)):
DTYPE = np.float64 # from fast_dict.pxd
dtype = DTYPE
accept_sparse = "csc"
else:
dtype = None
accept_sparse = ["csr", "csc"]
X, y = check_X_y(
X,
y,
accept_sparse=accept_sparse,
dtype=dtype,
y_numeric=is_regressor(self),
)
if sample_weight is None:
# Initialize weights to 1 / n_samples.
sample_weight = np.empty(X.shape[0], dtype=np.float64)
sample_weight[:] = 1. / X.shape[0]
else:
sample_weight = check_array(sample_weight, ensure_2d=False)
# Normalize existing weights.
sample_weight = sample_weight / sample_weight.sum(dtype=np.float64)
# Check that the sample weights sum is positive.
if sample_weight.sum() <= 0:
raise ValueError(
"Attempting to fit with a non-positive "
"weighted number of samples."
)
if minority_target is None:
# Determine the minority class label.
stats_c_ = Counter(y)
min_c_ = min(stats_c_, key=stats_c_.get)
self.minority_target = min_c_
else:
self.minority_target = minority_target
# Check parameters.
self._validate_estimator()
# Clear any previous fit results.
self.estimators_ = []
self.estimator_weights_ = np.zeros(self.n_estimators, dtype=np.float64)
self.estimator_errors_ = np.ones(self.n_estimators, dtype=np.float64)
random_state = check_random_state(self.random_state)
for iboost in range(self.n_estimators):
# Random undersampling step.
X_maj = X[np.where(y != self.minority_target)]
X_min = X[np.where(y == self.minority_target)]
self.rus.fit(X_maj)
n_maj = X_maj.shape[0]
n_min = X_min.shape[0]
if n_maj - self.n_samples < int(n_min * self.min_ratio):
self.n_samples = n_maj - int(n_min * self.min_ratio)
X_rus, X_idx = self.rus.sample(self.n_samples)
y_rus = y[np.where(y != self.minority_target)][X_idx]
y_min = y[np.where(y == self.minority_target)]
sample_weight_rus = sample_weight[
np.where(y != self.minority_target)
][X_idx]
sample_weight_min = sample_weight[
np.where(y == self.minority_target)
]
# Combine the minority and majority class samples.
X = np.vstack((X_rus, X_min))
y = np.append(y_rus, y_min)
# Combine the weights.
sample_weight = np.append(
sample_weight_rus, sample_weight_min
).reshape(-1, 1)
sample_weight = np.squeeze(
normalize(sample_weight, axis=0, norm="l1")
)
#X, y, sample_weight = shuffle(
# X, y, sample_weight, random_state=random_state
#)
# Boosting step.
sample_weight, estimator_weight, estimator_error = self._boost(
iboost,
X,
y,
sample_weight,
random_state,
)
# Early termination.
if sample_weight is None:
break
self.estimator_weights_[iboost] = estimator_weight
self.estimator_errors_[iboost] = estimator_error
# Stop if error is zero.
if estimator_error == 0:
break
sample_weight_sum = np.sum(sample_weight)
# Stop if the sum of sample weights has become non-positive.
if sample_weight_sum <= 0:
break
if iboost < self.n_estimators - 1:
# Normalize.
sample_weight /= sample_weight_sum
return self