Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kasyanovse committed Sep 6, 2023
1 parent b947965 commit 3ee9629
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
3 changes: 3 additions & 0 deletions fedot/core/data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,9 @@ def slice(self, start: Optional[int] = None, stop: Optional[int] = None,

index = np.arange(len(self))[slice(start, stop, step)]

if len(index) == 0:
raise ValueError(f"Uncorrect slicing [{start}:{stop}:{step}] for data with length {len(self)}")

return self.slice_by_index(index, step, in_sample=in_sample)

def slice_by_index(self, indexes: Iterable, step: int = 1, in_sample: bool = True):
Expand Down
19 changes: 11 additions & 8 deletions fedot/core/pipelines/ts_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,19 @@ def in_sample_ts_forecast(pipeline, input_data: Union[InputData, MultiModalData]
number_of_iterations = math.ceil(horizon / forecast_length)

final_forecast = np.zeros((number_of_iterations, forecast_length))
data = InputData(idx=input_data.idx,
features=input_data.features[:-forecast_length],
target=input_data.target,
data_type=input_data.data_type,
task=input_data.task)
for i in range(number_of_iterations):
data = input_data.slice(-(i + 1) * forecast_length,
-i * forecast_length if i != 0 else None,
in_sample=False)
if data.features.shape[0] == 0:
raise ValueError(('Cannot make predict without features.'
'Please, check that horizon is lower than data.features'))
iter_predict = pipeline.predict(input_data=data)
iter_predict = np.ravel(iter_predict.predict)
final_forecast[-(i + 1), :] = iter_predict
final_forecast[-(i + 1), :] = np.ravel(iter_predict.predict)
data = InputData(idx=data.idx[:-forecast_length],
features=data.features[:-forecast_length],
target=data.target[:-forecast_length],
data_type=data.data_type,
task=data.task)

final_forecast = np.ravel(final_forecast)[:horizon]
return final_forecast
Expand Down
12 changes: 11 additions & 1 deletion test/unit/tasks/test_forecasting.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,19 +271,29 @@ def test_multistep_out_of_sample_forecasting():

def test_multistep_in_sample_forecasting():
horizon = 12
train_data, test_data = get_ts_data(n_steps=200, forecast_length=5)
train_data, test_data = get_ts_data(n_steps=200, forecast_length=5, validation_blocks=3)

pipeline = get_multiscale_pipeline()

# Fit pipeline to make forecasts 5 elements above
pipeline.fit(input_data=train_data)

# manual predict
a = test_data.slice(-15, -10, in_sample=False)
manual_predict = []
manual_predict.append(pipeline.predict(input_data=test_data.slice(-15, -10, in_sample=False)))
manual_predict.append(pipeline.predict(input_data=test_data.slice(-10, -5, in_sample=False)))
manual_predict.append(pipeline.predict(input_data=test_data.slice(-5, in_sample=False)))
manual_predict = [np.ravel(x.predict) for x in manual_predict]
manual_predict = np.concatenate(manual_predict, axis=0)[:horizon]

# Make prediction for 12 elements
predicted = in_sample_ts_forecast(pipeline=pipeline,
input_data=test_data,
horizon=horizon)

assert len(predicted) == horizon
assert np.array_equal(manual_predict, predicted)


def test_ts_forecasting_with_multiple_series_in_lagged():
Expand Down

0 comments on commit 3ee9629

Please sign in to comment.