class SlidingWindowDataset(Dataset):
def __init__(self, train_X, train_y, input_window, output_window, stride=1):
self.train_X = train_X
self.train_y = train_y
self.input_window = input_window
self.output_window = output_window
self.stride = stride
self.variables = train_X.columns # train_X에도 target이 있어야 함.
self.target = self.train_y
self.num_samples = max((len(self.target) - input_window - output_window) // stride + 1, 0)
def create_features(self, df, idx):
return np.stack([df[var].values[idx * self.stride:idx * self.stride + self.input_window] for var in self.variables], axis=-1)
def create_target(self, target, idx):
return target[idx * self.stride + self.input_window : idx * self.stride + self.input_window + self.output_window]
def __getitem__(self, idx):
X = self.create_features(self.train_X, idx)
Y = self.create_target(self.target, idx)
X_tensor = torch.tensor(X, dtype=torch.float32)
Y_tensor = torch.tensor(Y, dtype=torch.float32)
return X_tensor, Y_tensor
def __len__(self):
return self.num_samples