def test_sliding_window_splitter_initial_window_start_with_empty_window_raises_error(): y = _make_series() cv = SlidingWindowSplitter( fh=1, initial_window=15, start_with_window=False, ) message = "`start_with_window` must be True if `initial_window` is given" with pytest.raises(ValueError, match=message): next(cv.split(y))
def test_sliding_window_splitter_initial_window_smaller_than_window_raise_error(): y = _make_series() cv = SlidingWindowSplitter( fh=1, window_length=10, initial_window=5, ) message = "`initial_window` must greater than `window_length`" with pytest.raises(ValueError, match=message): next(cv.split(y))
def test_evaluate_initial_window(): initial_window = 20 y = make_forecasting_problem(n_timepoints=30, index_type="int") forecaster = NaiveForecaster() fh = 1 cv = SlidingWindowSplitter(fh=fh, initial_window=initial_window) scoring = sMAPE() out = evaluate( forecaster=forecaster, y=y, cv=cv, strategy="update", scoring=scoring ) _check_evaluate_output(out, cv, y, scoring) assert out.loc[0, "len_train_window"] == initial_window # check scoring actual = out.loc[0, f"test_{scoring.name}"] train, test = next(cv.split(y)) f = clone(forecaster) f.fit(y.iloc[train], fh=fh) expected = scoring(y.iloc[test], f.predict()) np.testing.assert_equal(actual, expected)
strategy="recursive") forecaster.fit(y_train) y_pred = forecaster.predict(fh) plot_ys(y_train, y_test, y_pred, labels=["y_train", "y_test", "y_pred"]) st.pyplot() st.write("smape_loss(y_test, y_pred):", smape_loss(y_test, y_pred)) st.write(''' 为了更好地理解先前的数据转换,我们可以看看如何将训练数据划分为多个窗口。 本质上,sktime使用时间时间序列分割器,类似于scikit-learn中的交叉验证分割器。 在这里,我们展示了这对于训练数据的前20个观察结果是如何工作的: ''') with st.echo(): from sktime.forecasting.model_selection import SlidingWindowSplitter cv = SlidingWindowSplitter(window_length=10, start_with_window=True) for input_window, output_window in cv.split(y_train.iloc[:20]): print(input_window, output_window) st.write(''' [0 1 2 3 4 5 6 7 8 9] [10] [ 1 2 3 4 5 6 7 8 9 10] [11] [ 2 3 4 5 6 7 8 9 10 11] [12] [ 3 4 5 6 7 8 9 10 11 12] [13] [ 4 5 6 7 8 9 10 11 12 13] [14] [ 5 6 7 8 9 10 11 12 13 14] [15] [ 6 7 8 9 10 11 12 13 14 15] [16]