Skip to content

Commit c027b25

Browse files
glemaitrechkoar
authored andcommitted
Solve issue #116 - Create proper RandomState in EasyEnsemble (#117)
1 parent cca1d42 commit c027b25

File tree

6 files changed

+31
-4
lines changed

6 files changed

+31
-4
lines changed

doc/whats_new.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Changelog
4444
API
4545
~~~
4646

47+
- In :class:`ensemble.EasyEnsemble`, correction of the `random_state` generation.
4748
- First release of the stable API.
4849

4950
New methods

imblearn/ensemble/easy_ensemble.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33

44
import numpy as np
55

6+
from sklearn.utils import check_random_state
7+
68
from ..base import BaseMulticlassSampler
79
from ..under_sampling import RandomUnderSampler
810

911

12+
MAX_INT = np.iinfo(np.int32).max
13+
14+
1015
class EasyEnsemble(BaseMulticlassSampler):
16+
1117
"""Create an ensemble sets by iteratively applying random under-sampling.
1218
1319
This method iteratively select a random subset and make an ensemble of the
@@ -117,19 +123,26 @@ def _sample(self, X, y):
117123
118124
"""
119125

126+
# Check the random state
127+
random_state = check_random_state(self.random_state)
128+
120129
X_resampled = []
121130
y_resampled = []
122131
if self.return_indices:
123132
idx_under = []
124133

125-
for s in range(self.n_subsets):
126-
self.logger.debug('Creation of the set #%s', s)
134+
self.samplers_ = []
127135

128-
# Create the object for random under-sampling
136+
for _ in range(self.n_subsets):
129137
rus = RandomUnderSampler(ratio=self.ratio,
130138
return_indices=self.return_indices,
131-
random_state=self.random_state,
139+
random_state=random_state.randint(
140+
MAX_INT),
132141
replacement=self.replacement)
142+
self.samplers_.append(rus)
143+
144+
for rus in self.samplers_:
145+
133146
if self.return_indices:
134147
sel_x, sel_y, sel_idx = rus.fit_sample(X, y)
135148
else:
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

imblearn/ensemble/tests/test_easy_ensemble.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@ def test_fit_sample_half():
163163
assert_array_equal(y_resampled, y_gt)
164164

165165

166+
def test_random_state_none():
167+
"""Test that the processing is going throw with random state being None."""
168+
169+
# Define the ratio parameter
170+
ratio = 0.5
171+
172+
# Create the sampling object
173+
ee = EasyEnsemble(ratio=ratio, random_state=None)
174+
175+
# Get the different subset
176+
X_resampled, y_resampled = ee.fit_sample(X, Y)
177+
178+
166179
def test_sample_wrong_X():
167180
"""Test either if an error is raised when X is different at fitting
168181
and sampling"""

0 commit comments

Comments
 (0)