def _make_transformer(self, random_state=None): """Make and configure a copy of the `base_transformer` attribute. Warning: This method should be used to properly instantiate new sub-estimators. """ transformer = clone(self.base_transformer_) if random_state is not None: _set_random_states(transformer, random_state) return transformer
def _make_sampler_estimator(self, random_state=None): """Make and configure a copy of the `base_estimator_` attribute. Warning: This method should be used to properly instantiate new sub-estimators. """ estimator = clone(self.base_estimator_) estimator.set_params(**dict( (p, getattr(self, p)) for p in self.estimator_params)) sampler = clone(self.base_sampler_) if random_state is not None: _set_random_states(estimator, random_state) _set_random_states(sampler, random_state) return estimator, sampler
def _make_sampler_estimator(self, random_state=None): """Make and configure a copy of the `base_estimator_` attribute. Warning: This method should be used to properly instantiate new sub-estimators. """ estimator = clone(self.base_estimator_) estimator.set_params(**dict((p, getattr(self, p)) for p in self.estimator_params)) sampler = clone(self.base_sampler_) if random_state is not None: _set_random_states(estimator, random_state) _set_random_states(sampler, random_state) return estimator, sampler
def _make_sampler_estimator(self, append=True, random_state=None): """Make and configure a copy of the `base_estimator_` attribute. Warning: This method should be used to properly instantiate new sub-estimators. """ estimator = clone(self.base_estimator_) estimator.set_params( **{p: getattr(self, p) for p in self.estimator_params}) sampler = clone(self.base_sampler_) if random_state is not None: _set_random_states(estimator, random_state) _set_random_states(sampler, random_state) if append: self.estimators_.append(estimator) self.samplers_.append(sampler) self.pipelines_.append( make_pipeline(deepcopy(sampler), deepcopy(estimator))) return estimator, sampler
def _make_sampler_estimator(self, append=True, random_state=None): """Make and configure a copy of the `base_estimator_` attribute. Warning: This method should be used to properly instantiate new sub-estimators. """ estimator = clone(self.base_estimator_) estimator.set_params(**dict( (p, getattr(self, p)) for p in self.estimator_params)) sampler = clone(self.base_sampler_) if random_state is not None: _set_random_states(estimator, random_state) _set_random_states(sampler, random_state) if append: self.estimators_.append(estimator) self.samplers_.append(sampler) self.pipelines_.append( make_pipeline(deepcopy(sampler), deepcopy(estimator))) # do not return the indices within a pipeline self.pipelines_[-1].named_steps['randomundersampler'].set_params( return_indices=False) return estimator, sampler
def test_set_random_states(): # Linear Discriminant Analysis doesn't have random state: smoke test _set_random_states(LinearDiscriminantAnalysis(), random_state=17) clf1 = Perceptron(tol=1e-3, random_state=None) assert_equal(clf1.random_state, None) # check random_state is None still sets _set_random_states(clf1, None) assert_true(isinstance(clf1.random_state, int)) # check random_state fixes results in consistent initialisation _set_random_states(clf1, 3) assert_true(isinstance(clf1.random_state, int)) clf2 = Perceptron(tol=1e-3, random_state=None) _set_random_states(clf2, 3) assert_equal(clf1.random_state, clf2.random_state) # nested random_state def make_steps(): return [('sel', SelectFromModel(Perceptron(tol=1e-3, random_state=None))), ('clf', Perceptron(tol=1e-3, random_state=None))] est1 = Pipeline(make_steps()) _set_random_states(est1, 3) assert_true(isinstance(est1.steps[0][1].estimator.random_state, int)) assert_true(isinstance(est1.steps[1][1].random_state, int)) assert_not_equal(est1.get_params()['sel__estimator__random_state'], est1.get_params()['clf__random_state']) # ensure multiple random_state paramaters are invariant to get_params() # iteration order class AlphaParamPipeline(Pipeline): def get_params(self, *args, **kwargs): params = Pipeline.get_params(self, *args, **kwargs).items() return OrderedDict(sorted(params)) class RevParamPipeline(Pipeline): def get_params(self, *args, **kwargs): params = Pipeline.get_params(self, *args, **kwargs).items() return OrderedDict(sorted(params, reverse=True)) for cls in [AlphaParamPipeline, RevParamPipeline]: est2 = cls(make_steps()) _set_random_states(est2, 3) assert_equal(est1.get_params()['sel__estimator__random_state'], est2.get_params()['sel__estimator__random_state']) assert_equal(est1.get_params()['clf__random_state'], est2.get_params()['clf__random_state'])