def _make_sampler(self, append=True, random_state=None, **overwrite_kwargs): """Make and configure a copy of the `base_sampler_` attribute. Warning: This method should be used to properly instantiate new sub-samplers. """ sampler = clone(self.base_sampler_) if hasattr(self, 'sampler_kwargs_'): sampler.set_params(**self.sampler_kwargs_) # Arguments passed to _make_sampler function have higher priority, # they will overwrite the self.sampler_kwargs_ sampler.set_params(**overwrite_kwargs) if random_state is not None: _set_random_states(sampler, random_state) if append: self.samplers_.append(sampler) return sampler
def _clone_estimator(base_estimator, random_state=None): estimator = clone(base_estimator) if random_state is not None: _set_random_states(estimator, random_state) return estimator
def _clone_estimator(base_estimator, random_state=None, idx=0): estimator = clone(base_estimator) if random_state is not None: random_state = 255 if random_state == 0 else random_state _set_random_states(estimator, random_state * 37 * (idx + 1)) return estimator
def _make_estimator(self, append=True, random_state=None): """Make and configure a copy of the `estimator_` attribute. Warning: This method should be used to properly instantiate new sub-estimators. """ estimator = clone(self.estimator_) estimator.set_params(**{p: getattr(self, p) for p in self.estimator_params}) if random_state is not None: _set_random_states(estimator, random_state) if append: self.estimators_.append(estimator) return estimator
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( **{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 _validate_estimator(self, random_state): """Private function to create the classifier""" if (self.estimator is not None and isinstance(self.estimator, ClassifierMixin) and hasattr(self.estimator, "predict_proba")): self.estimator_ = clone(self.estimator) _set_random_states(self.estimator_, random_state) elif self.estimator is None: self.estimator_ = RandomForestClassifier( n_estimators=100, random_state=self.random_state, n_jobs=self.n_jobs, ) else: raise ValueError( f"Invalid parameter `estimator`. Got {type(self.estimator)}.")
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 test_set_random_states(): # Linear Discriminant Analysis doesn't have random state: smoke test _set_random_states(LinearDiscriminantAnalysis(), random_state=17) clf1 = Perceptron(random_state=None) assert clf1.random_state is None # check random_state is None still sets _set_random_states(clf1, None) assert isinstance(clf1.random_state, int) # check random_state fixes results in consistent initialisation _set_random_states(clf1, 3) assert isinstance(clf1.random_state, int) clf2 = Perceptron(random_state=None) _set_random_states(clf2, 3) assert clf1.random_state == clf2.random_state # nested random_state def make_steps(): return [('sel', SelectFromModel(Perceptron(random_state=None))), ('clf', Perceptron(random_state=None))] est1 = Pipeline(make_steps()) _set_random_states(est1, 3) assert isinstance(est1.steps[0][1].estimator.random_state, int) assert isinstance(est1.steps[1][1].random_state, int) assert (est1.get_params()['sel__estimator__random_state'] != est1.get_params()['clf__random_state']) # ensure multiple random_state parameters 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 (est1.get_params()['sel__estimator__random_state'] == est2.get_params()['sel__estimator__random_state']) assert (est1.get_params()['clf__random_state'] == est2.get_params()['clf__random_state'])