code: def fitted_values(
      source_input: InputData,
      train_predicted: OutputData,
      horizon_step: int = None,
  ) -> OutputData:
      """The method converts a multidimensional lagged array into an
      one-dimensional array - time series based on predicted values for training sample
  
      :param source_input: InputData which were used to train pipeline
      :param train_predicted: OutputData with trained values
      :param horizon_step: index of elements for forecast. If None - perform
      averaging for all forecasting steps
      """
      copied_data = copy(train_predicted)
      if horizon_step is not None:
          # Take particular forecast step
          copied_data.predict = copied_data.predict[:, horizon_step]
          if isinstance(copied_data.idx, list):
              # if indices can not be incremented, replace it
              copied_data.idx = generate_ids(
                  source_input, copied_data, expand=False
              )
          copied_data.idx = copied_data.idx + horizon_step
          return copied_data
      else:
          # Perform collapse with averaging
          forecast_length = copied_data.task.task_params.forecast_length
  
          # Extend source index range
          if isinstance(copied_data.idx, list):
              indices_range = generate_ids(
                  source_input, copied_data, expand=True
              )
          else:
              # if indices can be incremented
              indices_range = np.arange(
                  copied_data.idx[0],
                  copied_data.idx[-1] + forecast_length,
              )
  
          # Lagged matrix with indices in cells
          _, idx_matrix = ts_to_table(
              idx=indices_range,
              time_series=indices_range,
              window_size=forecast_length,
          )
          predicted_matrix = copied_data.predict
  
          # For every index calculate mean predictions (by all forecast steps)
          final_predictions = []
          for index in indices_range:
              vals = predicted_matrix[idx_matrix == index]
              mean_value = np.mean(vals)
              final_predictions.append(mean_value)
          copied_data.predict = np.array(final_predictions)
          copied_data.idx = indices_range
          return copied_data
  
file_path: FEDOT/fedot/core/pipelines/ts_wrappers.py
imports: import math
  from copy import copy
  from typing import Union, Optional
  import numpy
  from fedot.core.data.data import InputData, OutputData
  from fedot.core.data.multi_modal import MultiModalData
  from fedot.core.operations.evaluation.operation_implementations.data_operations.ts_transformations import ts_to_table
  from fedot.core.pipelines.pipeline import Pipeline
  from fedot.core.repository.dataset_types import DataTypesEnum
  from fedot.core.repository.tasks import TaskTypesEnum
label_desc: Use image node_id 3 for calling a global function with example usage: fitted_values(source_input, train_predicted, horizon_step) and returns: copied_data, copied_data