docstr: Regression Model
  Can be used to fit any scikit-learn-like regressor class to predict the target time series from lagged values.
  
  Parameters
  ----------
  lags
      Lagged target `series` values used to predict the next time step/s.
      If an integer, must be > 0. Uses the last `n=lags` past lags; e.g. `(-1, -2, ..., -lags)`, where `0`
      corresponds the first predicted time step of each sample.
      If a list of integers, each value must be < 0. Uses only the specified values as lags.
      If a dictionary, the keys correspond to the `series` component names (of the first series when
      using multiple series) and the values correspond to the component lags (integer or list of integers). The
      key 'default_lags' can be used to provide default lags for un-specified components. Raises and error if some
      components are missing and the 'default_lags' key is not provided.
  lags_past_covariates
      Lagged `past_covariates` values used to predict the next time step/s.
      If an integer, must be > 0. Uses the last `n=lags_past_covariates` past lags; e.g. `(-1, -2, ..., -lags)`,
      where `0` corresponds to the first predicted time step of each sample.
      If a list of integers, each value must be < 0. Uses only the specified values as lags.
      If a dictionary, the keys correspond to the `past_covariates` component names (of the first series when
      using multiple series) and the values correspond to the component lags (integer or list of integers). The
      key 'default_lags' can be used to provide default lags for un-specified components. Raises and error if some
      components are missing and the 'default_lags' key is not provided.
  lags_future_covariates
      Lagged `future_covariates` values used to predict the next time step/s.
      If a tuple of `(past, future)`, both values must be > 0. Uses the last `n=past` past lags and `n=future`
      future lags; e.g. `(-past, -(past - 1), ..., -1, 0, 1, .... future - 1)`, where `0`
      corresponds the first predicted time step of each sample.
      If a list of integers, uses only the specified values as lags.
      If a dictionary, the keys correspond to the `future_covariates` component names (of the first series when
      using multiple series) and the values correspond to the component lags (tuple or list of integers). The key
      'default_lags' can be used to provide default lags for un-specified components. Raises and error if some
      components are missing and the 'default_lags' key is not provided.
  output_chunk_length
      Number of time steps predicted at once (per chunk) by the internal model. It is not the same as forecast
      horizon `n` used in `predict()`, which is the desired number of prediction points generated using a
      one-shot- or auto-regressive forecast. Setting `n <= output_chunk_length` prevents auto-regression. This is
      useful when the covariates don't extend far enough into the future, or to prohibit the model from using
      future values of past and / or future covariates for prediction (depending on the model's covariate
      support).
  add_encoders
      A large number of past and future covariates can be automatically generated with `add_encoders`.
      This can be done by adding multiple pre-defined index encoders and/or custom user-made functions that
      will be used as index encoders. Additionally, a transformer such as Darts' :class:`Scaler` can be added to
      transform the generated covariates. This happens all under one hood and only needs to be specified at
      model creation.
      Read :meth:`SequentialEncoder <darts.dataprocessing.encoders.SequentialEncoder>` to find out more about
      ``add_encoders``. Default: ``None``. An example showing some of ``add_encoders`` features:
  
      .. highlight:: python
      .. code-block:: python
  
          def encode_year(idx):
              return (idx.year - 1950) / 50
  
          add_encoders={
              'cyclic': {'future': ['month']},
              'datetime_attribute': {'future': ['hour', 'dayofweek']},
              'position': {'past': ['relative'], 'future': ['relative']},
              'custom': {'past': [encode_year]},
              'transformer': Scaler(),
              'tz': 'CET'
          }
      ..
  model
      Scikit-learn-like model with ``fit()`` and ``predict()`` methods. Also possible to use model that doesn't
      support multi-output regression for multivariate timeseries, in which case one regressor
      will be used per component in the multivariate series.
      If None, defaults to: ``sklearn.linear_model.LinearRegression(n_jobs=-1)``.
  multi_models
      If True, a separate model will be trained for each future lag to predict. If False, a single model is
      trained to predict at step 'output_chunk_length' in the future. Default: True.
  use_static_covariates
      Whether the model should use static covariate information in case the input `series` passed to ``fit()``
      contain static covariates. If ``True``, and static covariates are available at fitting time, will enforce
      that all target `series` have the same static covariate dimensionality in ``fit()`` and ``predict()``.
  
  Examples
  --------
  >>> from darts.datasets import WeatherDataset
  >>> from darts.models import RegressionModel
  >>> from sklearn.linear_model import Ridge
  >>> series = WeatherDataset().load()
  >>> # predicting atmospheric pressure
  >>> target = series['p (mbar)'][:100]
  >>> # optionally, use past observed rainfall (pretending to be unknown beyond index 100)
  >>> past_cov = series['rain (mm)'][:100]
  >>> # optionally, use future temperatures (pretending this component is a forecast)
  >>> future_cov = series['T (degC)'][:106]
  >>> # wrap around the sklearn Ridge model
  >>> model = RegressionModel(
  >>>     model=Ridge(),
  >>>     lags=12,
  >>>     lags_past_covariates=4,
  >>>     lags_future_covariates=(0,6),
  >>>     output_chunk_length=6
  >>> )
  >>> model.fit(target, past_covariates=past_cov, future_covariates=future_cov)
  >>> pred = model.predict(6)
  >>> pred.values()
  array([[1005.73340676],
         [1005.71159051],
         [1005.7322616 ],
         [1005.76314504],
         [1005.82204348],
         [1005.89100967]])
docstr_tok: ["Regression","Model","Can","be","used","to","fit","any","scikit-learn-like","regressor","class","to","predict","the","target","time","series","from","lagged","values",".","Parameters","--","--","--","--","--","lags","Lagged","target","`","series","`","values","used","to","predict","the","next","time","step\/s",".","If","an","integer",",","must","be",">","0",".","Uses","the","last","`","n=lags","`","past","lags",";","e.g",".","`","(","-1",",","-2",",","...",",","-lags",")","`",",","where","`","0","`","corresponds","the","first","predicted","time","step","of","each","sample",".","If","a","list","of","integers",",","each","value","must","be","<","0",".","Uses","only","the","specified","values","as","lags",".","If","a","dictionary",",","the","keys","correspond","to","the","`","series","`","component","names","(","of","the","first","series","when","using","multiple","series",")","and","the","values","correspond","to","the","component","lags","(","integer","or","list","of","integers",")",".","The","key","'default_lags","'","can","be","used","to","provide","default","lags","for","un-specified","components",".","Raises","and","error","if","some","components","are","missing","and","the","'default_lags","'","key","is","not","provided",".","lags_past_covariates","Lagged","`","past_covariates","`","values","used","to","predict","the","next","time","step\/s",".","If","an","integer",",","must","be",">","0",".","Uses","the","last","`","n=lags_past_covariates","`","past","lags",";","e.g",".","`","(","-1",",","-2",",","...",",","-lags",")","`",",","where","`","0","`","corresponds","to","the","first","predicted","time","step","of","each","sample",".","If","a","list","of","integers",",","each","value","must","be","<","0",".","Uses","only","the","specified","values","as","lags",".","If","a","dictionary",",","the","keys","correspond","to","the","`","past_covariates","`","component","names","(","of","the","first","series","when","using","multiple","series",")","and","the","values","correspond","to","the","component","lags","(","integer","or","list","of","integers",")",".","The","key","'default_lags","'","can","be","used","to","provide","default","lags","for","un-specified","components",".","Raises","and","error","if","some","components","are","missing","and","the","'default_lags","'","key","is","not","provided",".","lags_future_covariates","Lagged","`","future_covariates","`","values","used","to","predict","the","next","time","step\/s",".","If","a","tuple","of","`","(","past",",","future",")","`",",","both","values","must","be",">","0",".","Uses","the","last","`","n=past","`","past","lags","and","`","n=future","`","future","lags",";","e.g",".","`","(","-past",",","-","(","past","-","1",")",",","...",",","-1",",","0",",","1",",","....","future","-","1",")","`",",","where","`","0","`","corresponds","the","first","predicted","time","step","of","each","sample",".","If","a","list","of","integers",",","uses","only","the","specified","values","as","lags",".","If","a","dictionary",",","the","keys","correspond","to","the","`","future_covariates","`","component","names","(","of","the","first","series","when","using","multiple","series",")","and","the","values","correspond","to","the","component","lags","(","tuple","or","list","of","integers",")",".","The","key","'default_lags","'","can","be","used","to","provide","default","lags","for","un-specified","components",".","Raises","and","error","if","some","components","are","missing","and","the","'default_lags","'","key","is","not","provided",".","output_chunk_length","Number","of","time","steps","predicted","at","once","(","per","chunk",")","by","the","internal","model",".","It","is","not","the","same","as","forecast","horizon","`","n","`","used","in","`","predict","(",")","`",",","which","is","the","desired","number","of","prediction","points","generated","using","a","one-shot-","or","auto-regressive","forecast",".","Setting","`","n","<","=","output_chunk_length","`","prevents","auto-regression",".","This","is","useful","when","the","covariates","do","n't","extend","far","enough","into","the","future",",","or","to","prohibit","the","model","from","using","future","values","of","past","and","\/","or","future","covariates","for","prediction","(","depending","on","the","model","'s","covariate","support",")",".","add_encoders","A","large","number","of","past","and","future","covariates","can","be","automatically","generated","with","`","add_encoders","`",".","This","can","be","done","by","adding","multiple","pre-defined","index","encoders","and\/or","custom","user-made","functions","that","will","be","used","as","index","encoders",".","Additionally",",","a","transformer","such","as","Darts","'",":","class",":","`","Scaler","`","can","be","added","to","transform","the","generated","covariates",".","This","happens","all","under","one","hood","and","only","needs","to","be","specified","at","model","creation",".","Read",":","meth",":","`","SequentialEncoder","<","darts.dataprocessing.encoders.SequentialEncoder",">","`","to","find","out","more","about","``","add_encoders","``",".","Default",":","``","None","``",".","An","example","showing","some","of","``","add_encoders","``","features",":","..","highlight",":",":","python","..","code-block",":",":","python","def","encode_year","(","idx",")",":","return","(","idx.year","-","1950",")","\/","50","add_encoders=","{","'cyclic","'",":","{","'future","'",":","[","'month","'","]","}",",","'datetime_attribute","'",":","{","'future","'",":","[","'hour","'",",","'dayofweek","'","]","}",",","'position","'",":","{","'past","'",":","[","'relative","'","]",",","'future","'",":","[","'relative","'","]","}",",","'custom","'",":","{","'past","'",":","[","encode_year","]","}",",","'transformer","'",":","Scaler","(",")",",","'tz","'",":","'CET'","}","..","model","Scikit-learn-like","model","with","``","fit","(",")","``","and","``","predict","(",")","``","methods",".","Also","possible","to","use","model","that","doesn't","support","multi-output","regression","for","multivariate","timeseries",",","in","which","case","one","regressor","will","be","used","per","component","in","the","multivariate","series",".","If","None",",","defaults","to",":","``","sklearn.linear_model.LinearRegression","(","n_jobs=-1",")","``",".","multi_models","If","True",",","a","separate","model","will","be","trained","for","each","future","lag","to","predict",".","If","False",",","a","single","model","is","trained","to","predict","at","step","'output_chunk_length","'","in","the","future",".","Default",":","True",".","use_static_covariates","Whether","the","model","should","use","static","covariate","information","in","case","the","input","`","series","`","passed","to","``","fit","(",")","``","contain","static","covariates",".","If","``","True","``",",","and","static","covariates","are","available","at","fitting","time",",","will","enforce","that","all","target","`","series","`","have","the","same","static","covariate","dimensionality","in","``","fit","(",")","``","and","``","predict","(",")","``",".","Examples","--","--","--","--",">",">",">","from","darts.datasets","import","WeatherDataset",">",">",">","from","darts.models","import","RegressionModel",">",">",">","from","sklearn.linear_model","import","Ridge",">",">",">","series","=","WeatherDataset","(",")",".load","(",")",">",">",">","#","predicting","atmospheric","pressure",">",">",">","target","=","series","[","'","p","(","mbar",")","'","]","[",":100","]",">",">",">","#","optionally",",","use","past","observed","rainfall","(","pretending","to","be","unknown","beyond","index","100",")",">",">",">","past_cov","=","series","[","'rain","(","mm",")","'","]","[",":100","]",">",">",">","#","optionally",",","use","future","temperatures","(","pretending","this","component","is","a","forecast",")",">",">",">","future_cov","=","series","[","'T","(","degC",")","'","]","[",":106","]",">",">",">","#","wrap","around","the","sklearn","Ridge","model",">",">",">","model","=","RegressionModel","(",">",">",">","model=Ridge","(",")",",",">",">",">","lags=12",",",">",">",">","lags_past_covariates=4",",",">",">",">","lags_future_covariates=","(","0,6",")",",",">",">",">","output_chunk_length=6",">",">",">",")",">",">",">","model.fit","(","target",",","past_covariates=past_cov",",","future_covariates=future_cov",")",">",">",">","pred","=","model.predict","(","6",")",">",">",">","pred.values","(",")","array","(","[","[","1005.73340676","]",",","[","1005.71159051","]",",","[","1005.7322616","]",",","[","1005.76314504","]",",","[","1005.82204348","]",",","[","1005.89100967","]","]",")"]
code: def __init__(
      self,
      lags: Optional[LAGS_TYPE] = None,
      lags_past_covariates: Optional[LAGS_TYPE] = None,
      lags_future_covariates: Optional[FUTURE_LAGS_TYPE] = None,
      output_chunk_length: int = 1,
      add_encoders: Optional[dict] = None,
      model=None,
      multi_models: Optional[bool] = True,
      use_static_covariates: bool = True,
  ):
      """Regression Model
      Can be used to fit any scikit-learn-like regressor class to predict the target time series from lagged values.
  
      Parameters
      ----------
      lags
          Lagged target `series` values used to predict the next time step/s.
          If an integer, must be > 0. Uses the last `n=lags` past lags; e.g. `(-1, -2, ..., -lags)`, where `0`
          corresponds the first predicted time step of each sample.
          If a list of integers, each value must be < 0. Uses only the specified values as lags.
          If a dictionary, the keys correspond to the `series` component names (of the first series when
          using multiple series) and the values correspond to the component lags (integer or list of integers). The
          key 'default_lags' can be used to provide default lags for un-specified components. Raises and error if some
          components are missing and the 'default_lags' key is not provided.
      lags_past_covariates
          Lagged `past_covariates` values used to predict the next time step/s.
          If an integer, must be > 0. Uses the last `n=lags_past_covariates` past lags; e.g. `(-1, -2, ..., -lags)`,
          where `0` corresponds to the first predicted time step of each sample.
          If a list of integers, each value must be < 0. Uses only the specified values as lags.
          If a dictionary, the keys correspond to the `past_covariates` component names (of the first series when
          using multiple series) and the values correspond to the component lags (integer or list of integers). The
          key 'default_lags' can be used to provide default lags for un-specified components. Raises and error if some
          components are missing and the 'default_lags' key is not provided.
      lags_future_covariates
          Lagged `future_covariates` values used to predict the next time step/s.
          If a tuple of `(past, future)`, both values must be > 0. Uses the last `n=past` past lags and `n=future`
          future lags; e.g. `(-past, -(past - 1), ..., -1, 0, 1, .... future - 1)`, where `0`
          corresponds the first predicted time step of each sample.
          If a list of integers, uses only the specified values as lags.
          If a dictionary, the keys correspond to the `future_covariates` component names (of the first series when
          using multiple series) and the values correspond to the component lags (tuple or list of integers). The key
          'default_lags' can be used to provide default lags for un-specified components. Raises and error if some
          components are missing and the 'default_lags' key is not provided.
      output_chunk_length
          Number of time steps predicted at once (per chunk) by the internal model. It is not the same as forecast
          horizon `n` used in `predict()`, which is the desired number of prediction points generated using a
          one-shot- or auto-regressive forecast. Setting `n <= output_chunk_length` prevents auto-regression. This is
          useful when the covariates don't extend far enough into the future, or to prohibit the model from using
          future values of past and / or future covariates for prediction (depending on the model's covariate
          support).
      add_encoders
          A large number of past and future covariates can be automatically generated with `add_encoders`.
          This can be done by adding multiple pre-defined index encoders and/or custom user-made functions that
          will be used as index encoders. Additionally, a transformer such as Darts' :class:`Scaler` can be added to
          transform the generated covariates. This happens all under one hood and only needs to be specified at
          model creation.
          Read :meth:`SequentialEncoder <darts.dataprocessing.encoders.SequentialEncoder>` to find out more about
          ``add_encoders``. Default: ``None``. An example showing some of ``add_encoders`` features:
  
          .. highlight:: python
          .. code-block:: python
  
              def encode_year(idx):
                  return (idx.year - 1950) / 50
  
              add_encoders={
                  'cyclic': {'future': ['month']},
                  'datetime_attribute': {'future': ['hour', 'dayofweek']},
                  'position': {'past': ['relative'], 'future': ['relative']},
                  'custom': {'past': [encode_year]},
                  'transformer': Scaler(),
                  'tz': 'CET'
              }
          ..
      model
          Scikit-learn-like model with ``fit()`` and ``predict()`` methods. Also possible to use model that doesn't
          support multi-output regression for multivariate timeseries, in which case one regressor
          will be used per component in the multivariate series.
          If None, defaults to: ``sklearn.linear_model.LinearRegression(n_jobs=-1)``.
      multi_models
          If True, a separate model will be trained for each future lag to predict. If False, a single model is
          trained to predict at step 'output_chunk_length' in the future. Default: True.
      use_static_covariates
          Whether the model should use static covariate information in case the input `series` passed to ``fit()``
          contain static covariates. If ``True``, and static covariates are available at fitting time, will enforce
          that all target `series` have the same static covariate dimensionality in ``fit()`` and ``predict()``.
  
      Examples
      --------
      >>> from darts.datasets import WeatherDataset
      >>> from darts.models import RegressionModel
      >>> from sklearn.linear_model import Ridge
      >>> series = WeatherDataset().load()
      >>> # predicting atmospheric pressure
      >>> target = series['p (mbar)'][:100]
      >>> # optionally, use past observed rainfall (pretending to be unknown beyond index 100)
      >>> past_cov = series['rain (mm)'][:100]
      >>> # optionally, use future temperatures (pretending this component is a forecast)
      >>> future_cov = series['T (degC)'][:106]
      >>> # wrap around the sklearn Ridge model
      >>> model = RegressionModel(
      >>>     model=Ridge(),
      >>>     lags=12,
      >>>     lags_past_covariates=4,
      >>>     lags_future_covariates=(0,6),
      >>>     output_chunk_length=6
      >>> )
      >>> model.fit(target, past_covariates=past_cov, future_covariates=future_cov)
      >>> pred = model.predict(6)
      >>> pred.values()
      array([[1005.73340676],
             [1005.71159051],
             [1005.7322616 ],
             [1005.76314504],
             [1005.82204348],
             [1005.89100967]])
      """
  
      super().__init__(add_encoders=add_encoders)
  
      self.model = model
      self.lags: Dict[str, List[int]] = {}
      self.component_lags: Dict[str, Dict[str, List[int]]] = {}
      self.input_dim = None
      self.multi_models = multi_models
      self._considers_static_covariates = use_static_covariates
      self._static_covariates_shape: Optional[Tuple[int, int]] = None
      self._lagged_feature_names: Optional[List[str]] = None
  
      # check and set output_chunk_length
      raise_if_not(
          isinstance(output_chunk_length, int)
          and output_chunk_length > 0,
          f"output_chunk_length must be an integer greater than 0. Given: {output_chunk_length}",
          logger=logger,
      )
      self._output_chunk_length = output_chunk_length
  
      # model checks
      if self.model is None:
          self.model = LinearRegression(n_jobs=-1)
  
      if not callable(getattr(self.model, "fit", None)):
          raise_log(
              Exception(
                  "Provided model object must have a fit() method",
                  logger,
              )
          )
      if not callable(getattr(self.model, "predict", None)):
          raise_log(
              Exception(
                  "Provided model object must have a predict() method",
                  logger,
              )
          )
  
      # check lags
      raise_if(
          (lags is None)
          and (lags_future_covariates is None)
          and (lags_past_covariates is None),
          "At least one of `lags`, `lags_future_covariates` or `lags_past_covariates` must be not None.",
      )
  
      # convert lags arguments to list of int
      # lags attribute should always be accessed with self._get_lags(), not self.lags.get()
      self.lags, self.component_lags = self._generate_lags(
          lags=lags,
          lags_past_covariates=lags_past_covariates,
          lags_future_covariates=lags_future_covariates,
      )
  
      self.pred_dim = (
          self.output_chunk_length if self.multi_models else 1
      )
  
imports: from collections import OrderedDict
  from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union
  import numpy
  import pandas
  from sklearn.linear_model import LinearRegression
  from darts.logging import get_logger, raise_if, raise_if_not, raise_log
  from darts.models.forecasting.forecasting_model import GlobalForecastingModel
  from darts.timeseries import TimeSeries
  from darts.utils.data.tabularization import add_static_covariates_to_lagged_data, create_lagged_component_names, create_lagged_training_data
  from darts.utils.historical_forecasts import _check_optimizable_historical_forecasts_global_models, _optimized_historical_forecasts_all_points, _optimized_historical_forecasts_last_points_only, _process_historical_forecast_input
  from darts.utils.multioutput import MultiOutputRegressor
  from darts.utils.utils import _check_quantiles, get_single_series, seq2series, series2seq
label_desc: Use image node_id 1 to create a new RegressionModel object from inherited base classes: GlobalForecastingModel with example: obj = RegressionModel(lags, lags_past_covariates, lags_future_covariates, output_chunk_length, add_encoders, model, multi_models, use_static_covariates)