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 test_pipeline(): """Test results of TransformedTargetForecaster.""" y = load_airline() y_train, y_test = temporal_train_test_split(y) forecaster = TransformedTargetForecaster([ ("t1", ExponentTransformer()), ("t2", TabularToSeriesAdaptor(MinMaxScaler())), ("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 = ExponentTransformer() yt = t1.fit_transform(yt) t2 = TabularToSeriesAdaptor(MinMaxScaler()) 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 learn(series_data): model = TransformedTargetForecaster([ ("deseasonalise", Deseasonalizer(model="multiplicative", sp=7)), ("detrend", Detrender(forecaster=PolynomialTrendForecaster(degree=4))), ("forecast", PolynomialTrendForecaster(degree=4)) ]) model.fit(series_data[:-2]) return model
def test_skip_inverse_transform(): """Test transformers with skip-inverse-transform tag in pipeline.""" y = load_airline() # add nan and outlier y.iloc[3] = np.nan y.iloc[4] = y.iloc[4] * 20 y_train, y_test = temporal_train_test_split(y) forecaster = TransformedTargetForecaster([ ("t1", HampelFilter(window_length=12)), ("t2", Imputer(method="mean")), ("forecaster", NaiveForecaster()), ]) fh = np.arange(len(y_test)) + 1 forecaster.fit(y_train, fh=fh) y_pred = forecaster.predict() assert isinstance(y_pred, pd.Series)
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"])
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) @pytest.mark.parametrize("cv", CVs)
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( "cv",
model = PolynomialTrendForecaster(degree=1) transformer = Detrender(model) yt = transformer.fit_transform(train) trendline = model.fit(train).predict(fh=-np.arange(len(train))) plot_ys(train, trendline, yt, labels=['series', 'trend', 'detrended']) # ### Pipelining # In[130]: forecaster = TransformedTargetForecaster([ ("deseasonalise", Deseasonalizer(model="multiplicative", sp=12)), ("detrend", Detrender(forecaster=PolynomialTrendForecaster(degree=1))), ("forecast", ReducedRegressionForecaster(regressor=regressor, window_length=12, strategy="recursive")) ]) forecaster.fit(train) y_pred = forecaster.predict(fh) plot_ys(train, test, y_pred, labels=["y_train", "y_test", "y_pred"]) smape_loss(test, y_pred) # ## WORK IN PROGRESS # ## Pipeline Tuning # In[246]: from sklearn.base import clone
def make_pipeline(*estimators): """Helper function to make pipeline""" steps = [(estimator.__class__.__name__, estimator) for estimator in estimators] return TransformedTargetForecaster(steps)
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", ReducedRegressionForecaster(LinearRegression())) ]), { "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( "cv", [ *[SingleWindowSplitter(fh=fh) for fh in TEST_OOS_FHS], # single split with multi-step fh