def test_ReducedForecastingRegressor_with_TransformedTargetRegressor( window_length, dynamic, fh): # define setting # forecasting horizon len_fh = len(fh) # load data and split into train/test series y = load_shampoo_sales() train = pd.Series([y.iloc[0].iloc[:-len_fh]]) test = pd.Series([y.iloc[0].iloc[-len_fh:]]) forecaster = ReducedRegressionForecaster(tsr, window_length=window_length, dynamic=dynamic) transformer = Pipeline([('deseasonalise', Deseasonaliser(sp=12)), ('detrend', Detrender(order=1))]) m = TransformedTargetForecaster(forecaster, transformer) # check if error is raised when dynamic is set to true but fh is not specified if not dynamic: with pytest.raises(ValueError): m.fit(train) m.fit(train, fh=fh) pred = m.predict(fh=fh) assert len(pred) == len(test.iloc[0])
def test_TransformedTargetForecaster_fit_predict(trend_order, arima_order): # forecasting horizon fh = np.arange(3) + 1 len_fh = len(fh) # load data and split into train/test series y = load_shampoo_sales() train = pd.Series([y.iloc[0].iloc[:-len_fh]]) test = pd.Series([y.iloc[0].iloc[-len_fh:]]) transformer = Detrender(order=trend_order) forecaster = ARIMAForecaster(order=arima_order) # using meta-estimator forecaster = TransformedTargetForecaster(forecaster, transformer) forecaster.fit(train) actual = forecaster.predict(fh=fh) check_consistent_time_indices(actual, test.iloc[0]) # checking against manual transform-inverse-transform train = pd.DataFrame(train) traint = transformer.fit_transform(train) traint = traint.iloc[:, 0] forecaster.fit(traint) pred = forecaster.predict(fh=fh) pred = pd.DataFrame(pd.Series([pred])) pred = transformer.inverse_transform(pred) expected = pred.iloc[0, 0] check_consistent_time_indices(expected, test.iloc[0]) np.testing.assert_allclose(actual, expected)
def test_multiplex_with_grid_search(): """Test MultiplexForecaster perfromas as expected with ForecastingGridSearchCV. Because the typical use case of MultiplexForecaster is to use it with the ForecastingGridSearchCV forecaster - here we simply test that the best "selected_forecaster" for MultiplexForecaster found using ForecastingGridSearchCV is the same forecaster we would find if we evaluated all the forecasters in MultiplexForecaster independently. """ y = load_shampoo_sales() forecasters = [ ("ets", AutoETS()), ("naive", NaiveForecaster()), ] multiplex_forecaster = MultiplexForecaster(forecasters=forecasters) forecaster_names = [name for name, _ in forecasters] cv = ExpandingWindowSplitter(start_with_window=True, step_length=12) gscv = ForecastingGridSearchCV( cv=cv, param_grid={"selected_forecaster": forecaster_names}, forecaster=multiplex_forecaster, ) gscv.fit(y) gscv_best_name = gscv.best_forecaster_.selected_forecaster best_name = _score_forecasters(forecasters, cv, y) assert gscv_best_name == best_name
def test_multiplex_forecaster_alone(): """Test results of MultiplexForecaster. Because MultiplexForecaster is in many ways a wrapper for an underlying forecaster - we can confirm that if the selected_forecaster is set that the MultiplexForecaster performs as expected. """ from numpy.testing import assert_array_equal y = load_shampoo_sales() # Note - we select two forecasters which are deterministic. forecaster_tuples = [ ("naive", NaiveForecaster()), ("theta", ThetaForecaster()), ] forecaster_names = [name for name, _ in forecaster_tuples] forecasters = [forecaster for _, forecaster in forecaster_tuples] multiplex_forecaster = MultiplexForecaster(forecasters=forecaster_tuples) fh_test = [1, 2, 3] # for each of the forecasters - check that the wrapped forecaster predictions # agree with the unwrapped forecaster predictions! for ind, name in enumerate(forecaster_names): # make a copy to ensure we don't reference the same objectL test_forecaster = clone(forecasters[ind]) test_forecaster.fit(y) multiplex_forecaster.selected_forecaster = name # Note- MultiplexForecaster will make a copy of the forecaster before fitting. multiplex_forecaster.fit(y) y_pred_indiv = test_forecaster.predict(fh=fh_test) y_pred_multi = multiplex_forecaster.predict(fh=fh_test) assert_array_equal(y_pred_indiv, y_pred_multi)
def test_ARIMAForecaster_univariate(fh): y = load_shampoo_sales() max_fh = np.max(fh) m = len(y.iloc[0]) cutoff = m - max_fh y_train = pd.Series([y.iloc[0].iloc[:cutoff]]) y_test = pd.Series([y.iloc[0].iloc[cutoff:]]) m = ARIMAForecaster() m.fit(y_train) y_pred = m.predict(fh=fh) assert y_pred.shape[0] == len(fh) assert m.score(y_test, fh=fh) > 0
def test_Forecasting2TSRReductionStrategy_univariate(): shampoo = load_shampoo_sales(return_y_as_dataframe=True) train = pd.DataFrame(pd.Series([shampoo.iloc[0, 0].iloc[:30]]), columns=shampoo.columns) test = pd.DataFrame(pd.Series([shampoo.iloc[0, 0].iloc[30:]]), columns=shampoo.columns) target = "ShampooSales" fh = np.arange(len(test[target].iloc[0])) + 1 task = ForecastingTask(target=target, fh=fh, metadata=train) s = Forecasting2TSRReductionStrategy(estimator=regressor) s.fit(task, train) y_pred = s.predict() assert y_pred.shape == test[task.target].iloc[0].shape
def test_univariate(dynamic, fh): fh = validate_fh(fh) len_fh = len(fh) y = load_shampoo_sales(return_y_as_dataframe=True) index = np.arange(y.iloc[0, 0].shape[0]) train_times = index[:-len_fh] test_times = index[-len_fh:] y_train = select_times(y, train_times) y_test = select_times(y, test_times) task = ForecastingTask(target="ShampooSales", fh=fh, metadata=y_train) s = Forecasting2TSRReductionStrategy(estimator=regressor, dynamic=dynamic) s.fit(task, y_train) y_pred = s.predict() assert y_pred.shape == y_test[task.target].iloc[0].shape
def test_ReducedForecastingRegressor(window_length, dynamic, fh): # define setting # forecasting horizon len_fh = len(fh) # load data and split into train/test series y = load_shampoo_sales() train = pd.Series([y.iloc[0].iloc[:-len_fh]]) test = pd.Series([y.iloc[0].iloc[-len_fh:]]) forecaster = ReducedRegressionForecaster(tsr, window_length=window_length, dynamic=dynamic) # check if error is raised when dynamic is set to true but fh is not specified if not dynamic: with pytest.raises(ValueError): forecaster.fit(train) forecaster.fit(train, fh=fh) pred = forecaster.predict(fh=fh) assert len(pred) == len(test.iloc[0])
import pytest from pytest import raises from sktime.datasets import load_gunpoint from sktime.datasets import load_shampoo_sales from sktime.highlevel.tasks import BaseTask from sktime.highlevel.tasks import TSCTask from sktime.highlevel.tasks import TSRTask from sktime.highlevel.tasks import ForecastingTask __author__ = "Markus Löning" TASKS = (TSCTask, TSRTask, ForecastingTask) gunpoint = load_gunpoint(return_X_y=False) shampoo_sales = load_shampoo_sales(return_y_as_dataframe=True) BASE_READONLY_ATTRS = ("target", "features", "metadata") # Test read-only attributes of base task @pytest.mark.parametrize("attr", BASE_READONLY_ATTRS) def test_readonly_attributes(attr): task = BaseTask(target='class_val', metadata=gunpoint) with raises(AttributeError): task.__setattr__(attr, "val") # Test read-only forecasting horizon attribute of forecasting task @pytest.mark.parametrize("fh", [None, [1], [1, 2, 3]]) def test_readonly_fh(fh):
from sktime.forecasters import DummyForecaster from sktime.forecasters import ExpSmoothingForecaster from sktime.forecasters import ARIMAForecaster from sktime.forecasters import EnsembleForecaster from sktime.datasets import load_shampoo_sales __author__ = "Markus Löning" # forecasters FORECASTERS = (DummyForecaster, ExpSmoothingForecaster, ARIMAForecaster) # forecast horizons FHS = (None, [1], [1, 3], np.array([1]), np.array([1, 3]), np.arange(5)) # load test data y = load_shampoo_sales() # test default forecasters output for different forecasting horizons @pytest.mark.parametrize("fh", FHS) def test_EnsembleForecaster_fhs(fh): estimators = [('ses', ExpSmoothingForecaster()), ('last', DummyForecaster(strategy='last'))] m = EnsembleForecaster(estimators=estimators) m.fit(y) y_pred = m.predict(fh=fh) # adjust for default value if fh is None: fh = np.array([1]) if isinstance(fh, list):