Skip to content

Commit 4d665ab

Browse files
glemaitreGuillaume Lemaitre
authored andcommitted
Solve issue #116 - Create proper RandomState in EasyEnsemble (#117)
Conflicts: imblearn/ensemble/easy_ensemble.py
1 parent 85acb8b commit 4d665ab

File tree

6 files changed

+32
-6
lines changed

6 files changed

+32
-6
lines changed

doc/whats_new.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Changelog
1515
API
1616
~~~
1717

18+
- In :class:`ensemble.EasyEnsemble`, correction of the `random_state` generation.
1819
- First release of the stable API.
1920

2021
New methods

imblearn/ensemble/easy_ensemble.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33

44
import numpy as np
55

6-
from ..base import SamplerMixin
6+
from sklearn.utils import check_random_state
7+
8+
from ..base import BaseMulticlassSampler
79
from ..under_sampling import RandomUnderSampler
810

911

10-
class EasyEnsemble(SamplerMixin):
12+
MAX_INT = np.iinfo(np.int32).max
13+
14+
15+
class EasyEnsemble(BaseMulticlassSampler):
1116
"""Create an ensemble sets by iteratively applying random under-sampling.
1217
1318
This method iteratively select a random subset and make an ensemble of the
@@ -98,19 +103,26 @@ def _sample(self, X, y):
98103
99104
"""
100105

106+
# Check the random state
107+
random_state = check_random_state(self.random_state)
108+
101109
X_resampled = []
102110
y_resampled = []
103111
if self.return_indices:
104112
idx_under = []
105113

106-
for s in range(self.n_subsets):
107-
self.logger.debug('Creation of the set #%s', s)
114+
self.samplers_ = []
108115

109-
# Create the object for random under-sampling
116+
for _ in range(self.n_subsets):
110117
rus = RandomUnderSampler(ratio=self.ratio,
111118
return_indices=self.return_indices,
112-
random_state=self.random_state,
119+
random_state=random_state.randint(
120+
MAX_INT),
113121
replacement=self.replacement)
122+
self.samplers_.append(rus)
123+
124+
for rus in self.samplers_:
125+
114126
if self.return_indices:
115127
sel_x, sel_y, sel_idx = rus.fit_sample(X, y)
116128
else:
0 Bytes
Binary file not shown.

imblearn/ensemble/tests/data/ee_x.npy

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
@@ -161,6 +161,19 @@ def test_fit_sample_half():
161161
assert_array_equal(y_resampled, y_gt)
162162

163163

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

0 commit comments

Comments
 (0)