def test_polynomial_detrending(): import numpy as np import pandas as pd from sktime.forecasting.tests.test_trend import get_expected_polynomial_coefs from sktime.forecasting.trend import PolynomialTrendForecaster from sktime.transformations.series.detrend import Detrender y = pd.Series(np.arange(20) * 0.5) + np.random.normal(0, 1, size=20) forecaster = PolynomialTrendForecaster(degree=1, with_intercept=True) transformer = Detrender(forecaster) transformer.fit(y) # check coefficients actual_coefs = transformer.forecaster_.regressor_.steps[-1][-1].coef_ expected_coefs = get_expected_polynomial_coefs(y, degree=1, with_intercept=True)[::-1] np.testing.assert_array_almost_equal(actual_coefs, expected_coefs) # check trend expected_trend = expected_coefs[0] + np.arange(len(y)) * expected_coefs[1] actual_trend = transformer.forecaster_.predict(-np.arange(len(y))) np.testing.assert_array_almost_equal(actual_trend, expected_trend) # check residuals actual = transformer.transform(y) expected = y - expected_trend np.testing.assert_array_almost_equal(actual, expected)
def compute_expected_y_pred(y_train, fh): # fitting yt = y_train.copy() t1 = Deseasonalizer(sp=12, model="multiplicative") yt = t1.fit_transform(yt) t2 = Detrender(PolynomialTrendForecaster(degree=1)) yt = t2.fit_transform(yt) forecaster = NaiveForecaster() forecaster.fit(yt, fh=fh) # predicting y_pred = forecaster.predict() y_pred = t2.inverse_transform(y_pred) y_pred = t1.inverse_transform(y_pred) return y_pred
def test_pipeline(): y = load_airline() y_train, y_test = temporal_train_test_split(y) forecaster = TransformedTargetForecaster([ ("t1", Deseasonalizer(sp=12, model="multiplicative")), ("t2", Detrender(PolynomialTrendForecaster(degree=1))), ("forecaster", NaiveForecaster()), ]) fh = np.arange(len(y_test)) + 1 forecaster.fit(y_train, fh=fh) actual = forecaster.predict() def compute_expected_y_pred(y_train, fh): # fitting yt = y_train.copy() t1 = Deseasonalizer(sp=12, model="multiplicative") yt = t1.fit_transform(yt) t2 = Detrender(PolynomialTrendForecaster(degree=1)) yt = t2.fit_transform(yt) forecaster = NaiveForecaster() forecaster.fit(yt, fh=fh) # predicting y_pred = forecaster.predict() y_pred = t2.inverse_transform(y_pred) y_pred = t1.inverse_transform(y_pred) return y_pred expected = compute_expected_y_pred(y_train, fh) np.testing.assert_array_equal(actual, expected)
def get_test_params(cls): """Return testing parameter settings for the estimator. Returns ------- params : dict or list of dict, default = {} Parameters to create testing instances of the class Each dict are parameters to construct an "interesting" test instance, i.e., `MyClass(**params)` or `MyClass(**params[i])` creates a valid test instance. `create_test_instance` uses the first (or only) dictionary in `params` """ from sktime.transformations.series.detrend import Detrender return {"transformer": Detrender()}
def get_test_params(cls, parameter_set="default"): """Return testing parameter settings for the estimator. Parameters ---------- parameter_set : str, default="default" Name of the set of test parameters to return, for use in tests. If no special parameters are defined for a value, will return `"default"` set. Returns ------- params : dict or list of dict, default = {} Parameters to create testing instances of the class Each dict are parameters to construct an "interesting" test instance, i.e., `MyClass(**params)` or `MyClass(**params[i])` creates a valid test instance. `create_test_instance` uses the first (or only) dictionary in `params` """ from sktime.transformations.series.detrend import Detrender return {"transformer": Detrender()}
def test_nesting_pipelines(): """Test that nesting of pipelines works.""" from sktime.forecasting.ets import AutoETS from sktime.transformations.series.boxcox import LogTransformer from sktime.transformations.series.compose import OptionalPassthrough from sktime.transformations.series.detrend import Detrender from sktime.utils._testing.scenarios_forecasting import ( ForecasterFitPredictUnivariateWithX, ) pipe = ForecastingPipeline(steps=[ ("logX", OptionalPassthrough(LogTransformer())), ("detrenderX", OptionalPassthrough(Detrender(forecaster=AutoETS()))), ( "etsforecaster", TransformedTargetForecaster(steps=[ ("log", OptionalPassthrough(LogTransformer())), ("autoETS", AutoETS()), ]), ), ]) scenario = ForecasterFitPredictUnivariateWithX() scenario.run(pipe, method_sequence=["fit", "predict"])
( "transformer2", SeriesToSeriesRowTransformer(SERIES_TO_SERIES_TRANSFORMER, check_transformer=False), ), ] REGRESSOR = LinearRegression() TIME_SERIES_CLASSIFIER = TimeSeriesForest(n_estimators=3) TIME_SERIES_CLASSIFIERS = [ ("tsf1", TIME_SERIES_CLASSIFIER), ("tsf2", TIME_SERIES_CLASSIFIER), ] FORECASTER = ExponentialSmoothing() FORECASTERS = [("ses1", FORECASTER), ("ses2", FORECASTER)] STEPS = [ ("transformer", Detrender(ThetaForecaster())), ("forecaster", NaiveForecaster()), ] ESTIMATOR_TEST_PARAMS = { OnlineEnsembleForecaster: { "forecasters": FORECASTERS }, FeatureUnion: { "transformer_list": TRANSFORMERS }, DirectRegressionForecaster: { "regressor": REGRESSOR }, MultioutputRegressionForecaster: { "regressor": REGRESSOR },
best_idx = gscv.best_index_ assert best_idx == actual.argmin() best_params = gscv.best_params_ assert best_params == param_grid[best_idx] # Check if best parameters are contained in best forecaster. best_forecaster_params = gscv.best_forecaster_.get_params() best_params = gscv.best_params_ assert best_params.items() <= best_forecaster_params.items() NAIVE = NaiveForecaster(strategy="mean") NAIVE_GRID = {"window_length": TEST_WINDOW_LENGTHS} PIPE = TransformedTargetForecaster([ ("transformer", Detrender(PolynomialTrendForecaster())), ("forecaster", ARIMA()), ]) PIPE_GRID = { "transformer__forecaster__degree": [1, 2], "forecaster__with_intercept": [True, False], } CVs = [ *[SingleWindowSplitter(fh=fh) for fh in TEST_OOS_FHS], SlidingWindowSplitter(fh=1, initial_window=15), ] @pytest.mark.parametrize("forecaster, param_grid", [(NAIVE, NAIVE_GRID), (PIPE, PIPE_GRID)]) @pytest.mark.parametrize("scoring", TEST_METRICS)
}, Imputer: { "method": "mean" }, HampelFilter: { "window_length": 3 }, OptionalPassthrough: { "transformer": BoxCoxTransformer(), "passthrough": False }, FeatureSelection: { "method": "all" }, ColumnwiseTransformer: { "transformer": Detrender() }, AggrDist: { "transformer": ScipyDist() }, PyODAnnotator: { "estimator": ANOMALY_DETECTOR }, ClaSPSegmentation: { "period_length": 5, "n_cps": 1 }, ClaSPTransformer: { "window_length": 5 }, }
y_test_subset = y_test.loc[ y_pred.index ] # select only time points which we predicted scores[i] = scoring(y_test_subset, y_pred) return scores @pytest.mark.parametrize( "forecaster, param_dict", [ (NaiveForecaster(strategy="mean"), {"window_length": TEST_WINDOW_LENGTHS}), # atomic estimator ( TransformedTargetForecaster( [ # composite estimator ("t", Detrender(PolynomialTrendForecaster())), ("f", ReducedForecaster(LinearRegression(), scitype="regressor")), ] ), { "f__window_length": TEST_WINDOW_LENGTHS, "f__step_length": TEST_STEP_LENGTHS, }, ), # multiple params ], ) @pytest.mark.parametrize( "scoring", [sMAPE(), make_forecasting_scorer(mean_squared_error, greater_is_better=False)], ) @pytest.mark.parametrize(
SeriesToSeriesRowTransformer( SERIES_TO_SERIES_TRANSFORMER, check_transformer=False ), ), ] REGRESSOR = LinearRegression() ANOMALY_DETECTOR = KNN() TIME_SERIES_CLASSIFIER = TSFC(n_estimators=3) TIME_SERIES_CLASSIFIERS = [ ("tsf1", TIME_SERIES_CLASSIFIER), ("tsf2", TIME_SERIES_CLASSIFIER), ] FORECASTER = ExponentialSmoothing() FORECASTERS = [("ses1", FORECASTER), ("ses2", FORECASTER)] STEPS_y = [ ("transformer", Detrender(ThetaForecaster())), ("forecaster", NaiveForecaster()), ] STEPS_X = [ ("transformer", TabularToSeriesAdaptor(StandardScaler())), ("forecaster", NaiveForecaster()), ] ESTIMATOR_TEST_PARAMS = { ColumnEnsembleForecaster: {"forecasters": FORECASTER}, OnlineEnsembleForecaster: {"forecasters": FORECASTERS}, FeatureUnion: {"transformer_list": TRANSFORMERS}, DirectTabularRegressionForecaster: {"estimator": REGRESSOR}, MultioutputTabularRegressionForecaster: {"estimator": REGRESSOR}, RecursiveTabularRegressionForecaster: {"estimator": REGRESSOR}, DirRecTabularRegressionForecaster: {"estimator": REGRESSOR}, DirectTimeSeriesRegressionForecaster: {