Skip to content

Commit

Permalink
Fix sparse-lagged transformation for multi-ts
Browse files Browse the repository at this point in the history
  • Loading branch information
YamLyubov committed Nov 25, 2022
1 parent b1fad53 commit 65b5568
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
4 changes: 1 addition & 3 deletions cases/multi_ts_level_forecasting.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ def run_multi_ts_forecast(forecast_length, is_multi_ts):
max_arity=4,
cv_folds=None,
validation_blocks=None,
initial_assumption=init_pipeline,
available_operations=['lagged', 'smoothing', 'diff_filter', 'gaussian_filter',
'ridge', 'lasso', 'linear', 'cut']
initial_assumption=init_pipeline
)
# fit model
pipeline = model.fit(train_data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,7 @@ def _current_target_for_each_ts(self, current_ts_id, target):
def _apply_transformation_for_predict(self, input_data: InputData):
"""Apply lagged transformation for every column (time series) in the dataset
"""

if self.sparse_transform:
self.log.debug(f'Sparse lagged transformation applied. If new data were used. Call fit method')
transformed_cols = self._update_features_for_sparse(input_data)
# Take last row in the lagged table and reshape into array with 1 row and n columns
self.features_columns = transformed_cols[-1].reshape(1, -1)
return self.features_columns
old_idx = copy(input_data.idx)

if len(input_data.features.shape) > 1:
# Multivariate time series
Expand All @@ -294,6 +288,12 @@ def _apply_transformation_for_predict(self, input_data: InputData):
else:
current_ts = np.ravel(input_data.features[:, current_ts_id])

if self.sparse_transform:
self.log.debug('Sparse lagged transformation applied. If new data were used. Call fit method')
transformed_cols = self._update_features_for_sparse(current_ts, old_idx)
# Take last row in the lagged table and reshape into array with 1 row and n columns
current_ts = transformed_cols[-1].reshape(1, -1)

# Take last window_size elements for current ts
last_part_of_ts = current_ts[-self.window_size:].reshape(1, -1)
if current_ts_id == 0:
Expand All @@ -320,13 +320,13 @@ def stack_by_type_predict(self, input_data, all_features, part_to_add):
all_features = np.hstack((all_features, part_to_add))
return all_features

def _update_features_for_sparse(self, input_data: InputData):
def _update_features_for_sparse(self, time_series: np.array, idx: np.array):
"""Make sparse matrix which will be used during forecasting
"""

# Prepare features for training
new_idx, transformed_cols = ts_to_table(idx=input_data.idx,
time_series=input_data.features,
new_idx, transformed_cols = ts_to_table(idx=idx,
time_series=time_series,
window_size=self.window_size,
is_lag=True)
# Sparsing
Expand Down

0 comments on commit 65b5568

Please # to comment.