def test_window_slice_windows(): windows = SlidingWindow(size=4, stride=2) X = signal_embedded_search X_windows = windows.fit_transform(X) slice_idx = windows.slice_windows(X) assert_almost_equal( np.stack([X[begin:end] for begin, end in slice_idx]), X_windows )
def test_window_resample(): windows = SlidingWindow(width=3, stride=2) windows.fit(y) y_resampled = windows.resample(y) assert_almost_equal(y_resampled, y[np.arange(3, 20, 2)])
def test_window_transform(): windows = SlidingWindow(width=3, stride=2) x_windows = windows.fit_transform(signal_embedded_search) assert (x_windows.shape == (8, 4, 2))
def test_window_params(): windows = SlidingWindow(width=0) with pytest.raises(ValueError): windows.fit(signal)
n_timestamps = 10 X, y = np.arange(n_timestamps), np.arange(n_timestamps) - n_timestamps X, y # We can instantiate our sliding window transformer-resampler and run it on the pair ``(X, y)``: # 我们可以实例化滑动窗口转换器-重采样器,然后在(X,y)对上运行它 # In[2]: from gtda.time_series import SlidingWindow window_size = 3 stride = 2 SW = SlidingWindow(size=window_size, stride=stride) X_sw, yr = SW.fit_transform_resample(X, y) X_sw, yr # We note a couple of things: # - ``fit_transform_resample`` returns a pair: the window-transformed ``X`` and the resampled and aligned ``y``. # - ``SlidingWindow`` has made a choice for us on how to resample ``y`` and line it up with the windows from ``X``: a window on ``X`` corresponds to the *last* value in a corresponding window over ``y``. This is common in time series forecasting where, for example, ``y`` could be a shift of ``X`` by one timestamp. # - Some of the initial values of ``X`` may not be found in ``X_sw``. This is because ``SlidingWindow`` only ensures the *last* value is represented in the last window, regardless of the stride. # # 注意以下几点: # fit_transform_resample返回一对:经过窗口转换的X和经过重新采样并对齐的y。 # SlidingWindow已为我们选择了如何对y重新采样并将其与X中的窗口对齐:X上的窗口与y上相应窗口中的最后一个值相对应。这在时间序列预测中很常见,例如,y可能是X偏移一个时间戳。 # X的某些初始值可能在X_sw中找不到。这是因为SlidingWindow仅确保最后一个值在最后一个窗口中表示,而不管步幅如何。 # ## Multivariate time series example: Sliding window + topology ``Pipeline``
def test_window_plot(): windows = SlidingWindow(width=3, stride=2) X_windows = windows.fit_transform(signal_embedded_search) windows.plot(X_windows, sample=0)
def test_window_params(size): windows = SlidingWindow(size=size) with pytest.raises(ValueError): windows.fit(signal)
def test_window_transform(): windows = SlidingWindow(size=4, stride=2) X_windows = windows.fit_transform(signal_embedded_search) assert (X_windows.shape == (8, 4, 2))