我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用sklearn.base.RegressorMixin()。
def _check_sklearn_model(model): if not (isinstance(model, BaseEstimator) and isinstance(model, RegressorMixin)): raise RuntimeError('Needs to supply an instance of a scikit-learn ' 'compatible regression class.')
def _generate_bases_test(est, pd_est): def test(self): self.assertTrue(isinstance(pd_est, FrameMixin), pd_est) self.assertFalse(isinstance(est, FrameMixin)) self.assertTrue(isinstance(pd_est, base.BaseEstimator)) try: mixins = [ base.ClassifierMixin, base.ClusterMixin, base.BiclusterMixin, base.TransformerMixin, base.DensityMixin, base.MetaEstimatorMixin, base.ClassifierMixin, base.RegressorMixin] except: if _sklearn_ver > 17: raise mixins = [ base.ClassifierMixin, base.ClusterMixin, base.BiclusterMixin, base.TransformerMixin, base.MetaEstimatorMixin, base.ClassifierMixin, base.RegressorMixin] for mixin in mixins: self.assertEqual( isinstance(pd_est, mixin), isinstance(est, mixin), mixin) return test
def evaluate(self, point): """ Fits model using the particular setting of hyperparameters and evaluates the model validation data. Parameters ---------- * `point`: dict A mapping of parameter names to the corresponding values Returns ------- * `score`: float Score (more is better!) for some specific point """ X_train, y_train, X_test, y_test = ( self.X_train, self.y_train, self.X_test, self.y_test) # apply transformation to model parameters, for example exp transformation point_mapped = {} for param, val in point.items(): point_mapped[param] = self.space[param][1](val) model_instance = self.model(**point_mapped) if 'random_state' in model_instance.get_params(): model_instance.set_params(random_state=self.random_state) min_obj_val = -5.0 # Infeasible parameters are expected to raise an exception, thus the try # catch below, infeasible parameters yield assumed smallest objective. try: model_instance.fit(X_train, y_train) if isinstance(model_instance, RegressorMixin): # r^2 metric y_predicted = model_instance.predict(X_test) score = r2_score(y_test, y_predicted) elif isinstance(model_instance, ClassifierMixin): # log loss y_predicted = model_instance.predict_proba(X_test) score = -log_loss(y_test, y_predicted) # in the context of this function, the higher score is better # avoid any kind of singularitites, eg probability being zero, and thus breaking the log_loss if math.isnan(score): score = min_obj_val score = max(score, min_obj_val) # this is necessary to avoid -inf or NaN except BaseException as ex: score = min_obj_val # on error: return assumed smallest value of objective function return score # this is necessary to generate table for README in the end