def test_fit_dummy(make_whas500): whas500 = make_whas500(with_mean=False, with_std=False, to_numeric=True) base_estimators = [ ('dummy_0', DummySurvivalRegressor(strategy="mean")), ('dummy_1', DummySurvivalRegressor(strategy="median")), ('dummy_2', DummySurvivalRegressor(strategy="quantile", quantile=0.1)), ('dummy_3', DummySurvivalRegressor(strategy="quantile", quantile=0.9)), ('dummy_4', DummySurvivalRegressor(strategy="quantile", quantile=0.89)), ('dummy_5', DummySurvivalRegressor(strategy="quantile", quantile=0.91)), ] meta = EnsembleSelectionRegressor(base_estimators, n_estimators=1, min_score=5, cv=5, scorer=_score_rmse) with pytest.raises( ValueError, match="no base estimator exceeds min_score, try decreasing it" ): meta.fit(whas500.x, whas500.y)
def test_invalid_scorer(make_whas500): whas500 = make_whas500(with_mean=False, with_std=False, to_numeric=True) base_estimators = [ ('dummy_0', DummySurvivalRegressor(strategy="mean")), ('dummy_1', DummySurvivalRegressor(strategy="median")), ] def _score(est, X_test, y_test, **predict_params): return 'invalid' meta = EnsembleSelectionRegressor(base_estimators, n_estimators=1, min_score=5, cv=5, scorer=_score) with pytest.raises(ValueError, match=r"scoring must return a number, got invalid " r"\(<class 'str'>\) instead\."): meta.fit(whas500.x, whas500.y)
def test_invalid_scorer(self): base_estimators = [ ('dummy_0', DummySurvivalRegressor(strategy="mean")), ('dummy_1', DummySurvivalRegressor(strategy="median")), ] def _score(est, X_test, y_test, **predict_params): return 'invalid' meta = EnsembleSelectionRegressor(base_estimators, n_estimators=1, min_score=5, cv=5, scorer=_score) self.assertRaisesRegex(ValueError, r"scoring must return a number, got invalid \(<class 'str'>\) instead.", meta.fit, self.x, self.y)
def test_fit_dummy(self): base_estimators = [ ('dummy_0', DummySurvivalRegressor(strategy="mean")), ('dummy_1', DummySurvivalRegressor(strategy="median")), ('dummy_2', DummySurvivalRegressor(strategy="quantile", quantile=0.1)), ('dummy_3', DummySurvivalRegressor(strategy="quantile", quantile=0.9)), ('dummy_4', DummySurvivalRegressor(strategy="quantile", quantile=0.89)), ('dummy_5', DummySurvivalRegressor(strategy="quantile", quantile=0.91)), ] meta = EnsembleSelectionRegressor(base_estimators, n_estimators=1, min_score=5, cv=5, scorer=_score_rmse) self.assertRaisesRegex(ValueError, "no base estimator exceeds min_score, try decreasing it", meta.fit, self.x, self.y)
def _create_regression_ensemble(): aft_grid = ParameterGrid({"alpha": 2. ** numpy.arange(-2, 12, 2)}) svm_grid = ParameterGrid({"alpha": 2. ** numpy.arange(-12, 0, 2)}) base_estimators = [] for i, params in enumerate(aft_grid): model = IPCRidge(max_iter=1000, **params) base_estimators.append(("aft_%d" % i, model)) for i, params in enumerate(svm_grid): model = FastSurvivalSVM(rank_ratio=0, fit_intercept=True, max_iter=100, random_state=1, **params) base_estimators.append(("svm_%d" % i, model)) cv = KFold(n_splits=4, shuffle=True, random_state=0) meta = EnsembleSelectionRegressor(base_estimators, n_estimators=0.4, scorer=_score_rmse, cv=cv, n_jobs=1) return meta