Skip to content

utils

save_forecaster(forecaster, file_name, verbose=True)

Save forecaster model using joblib.

Parameters:

Name Type Description Default
forecaster

Forecaster created with skforecast library.

required
file_name str

File name given to the object.

required
verbose bool

Print summary about the forecaster saved.

True

Returns:

Type Description
None
Source code in skforecast\utils\utils.py
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
def save_forecaster(
    forecaster, 
    file_name: str, 
    verbose: bool=True
) -> None:
    """
    Save forecaster model using joblib.

    Parameters
    ----------
    forecaster: forecaster
        Forecaster created with skforecast library.
    file_name: str
        File name given to the object.
    verbose: bool, default `True`
        Print summary about the forecaster saved.

    Returns
    -------
    None

    """

    joblib.dump(forecaster, filename=file_name)

    if verbose:
        forecaster.summary()

load_forecaster(file_name, verbose=True)

Load forecaster model using joblib.

Parameters:

Name Type Description Default
file_name str

Object file name.

required
verbose bool

Print summary about the forecaster loaded.

True

Returns:

Name Type Description
forecaster forecaster

Forecaster created with skforecast library.

Source code in skforecast\utils\utils.py
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
def load_forecaster(
    file_name: str,
    verbose: bool=True
) -> object:
    """
    Load forecaster model using joblib.

    Parameters
    ----------
    file_name: str
        Object file name.
    verbose: bool, default `True`
        Print summary about the forecaster loaded.

    Returns
    -------
    forecaster: forecaster
        Forecaster created with skforecast library.

    """

    forecaster = joblib.load(filename=file_name)

    if verbose:
        forecaster.summary()

    return forecaster

initialize_lags(forecaster_name, lags)

Check lags argument input and generate the corresponding numpy ndarray.

Parameters:

Name Type Description Default
forecaster_name str

Forecaster name. ForecasterAutoreg, ForecasterAutoregCustom, ForecasterAutoregDirect, ForecasterAutoregMultiSeries, ForecasterAutoregMultiSeriesCustom, ForecasterAutoregMultiVariate.

required
lags Any

Lags used as predictors.

required

Returns:

Name Type Description
lags numpy ndarray

Lags used as predictors.

Source code in skforecast\utils\utils.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def initialize_lags(
    forecaster_name: str,
    lags: Any
) -> np.ndarray:
    """
    Check lags argument input and generate the corresponding numpy ndarray.

    Parameters
    ----------
    forecaster_name : str
        Forecaster name. ForecasterAutoreg, ForecasterAutoregCustom, 
        ForecasterAutoregDirect, ForecasterAutoregMultiSeries, 
        ForecasterAutoregMultiSeriesCustom, ForecasterAutoregMultiVariate.
    lags : Any
        Lags used as predictors.

    Returns
    -------
    lags : numpy ndarray
        Lags used as predictors.

    """

    if isinstance(lags, int) and lags < 1:
        raise ValueError("Minimum value of lags allowed is 1.")

    if isinstance(lags, (list, np.ndarray)):
        for lag in lags:
            if not isinstance(lag, (int, np.int64, np.int32)):
                raise TypeError("All values in `lags` must be int.")

    if isinstance(lags, (list, range, np.ndarray)) and min(lags) < 1:
        raise ValueError("Minimum value of lags allowed is 1.")

    if isinstance(lags, int):
        lags = np.arange(lags) + 1
    elif isinstance(lags, (list, range)):
        lags = np.array(lags)
    elif isinstance(lags, np.ndarray):
        lags = lags
    else:
        if not forecaster_name == 'ForecasterAutoregMultiVariate':
            raise TypeError(
                ("`lags` argument must be an int, 1d numpy ndarray, range or list. "
                 f"Got {type(lags)}.")
            )
        else:
            raise TypeError(
                ("`lags` argument must be a dict, int, 1d numpy ndarray, range or list. "
                 f"Got {type(lags)}.")
            )

    return lags

initialize_weights(forecaster_name, regressor, weight_func, series_weights)

Check weights arguments, weight_func and series_weights for the different forecasters. Create source_code_weight_func, source code of the custom function(s) used to create weights.

Parameters:

Name Type Description Default
forecaster_name str

Forecaster name. ForecasterAutoreg, ForecasterAutoregCustom, ForecasterAutoregDirect, ForecasterAutoregMultiSeries, ForecasterAutoregMultiSeriesCustom, ForecasterAutoregMultiVariate.

required
regressor regressor or pipeline compatible with the scikit-learn API

Regressor of the forecaster.

required
weight_func Callable, dict

Argument weight_func of the forecaster.

required
series_weights dict

Argument series_weights of the forecaster.

required

Returns:

Name Type Description
weight_func Callable, dict

Argument weight_func of the forecaster.

source_code_weight_func str, dict

Argument source_code_weight_func of the forecaster.

series_weights dict

Argument series_weights of the forecaster.

Source code in skforecast\utils\utils.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
def initialize_weights(
    forecaster_name: str,
    regressor: object,
    weight_func: Union[Callable, dict],
    series_weights: dict
) -> Tuple[Union[Callable, dict], Union[str, dict], dict]:
    """
    Check weights arguments, `weight_func` and `series_weights` for the different 
    forecasters. Create `source_code_weight_func`, source code of the custom 
    function(s) used to create weights.

    Parameters
    ----------
    forecaster_name : str
        Forecaster name. ForecasterAutoreg, ForecasterAutoregCustom, 
        ForecasterAutoregDirect, ForecasterAutoregMultiSeries, 
        ForecasterAutoregMultiSeriesCustom, ForecasterAutoregMultiVariate.
    regressor : regressor or pipeline compatible with the scikit-learn API
        Regressor of the forecaster.
    weight_func : Callable, dict
        Argument `weight_func` of the forecaster.
    series_weights : dict
        Argument `series_weights` of the forecaster.

    Returns
    -------
    weight_func : Callable, dict
        Argument `weight_func` of the forecaster.
    source_code_weight_func : str, dict
        Argument `source_code_weight_func` of the forecaster.
    series_weights : dict
        Argument `series_weights` of the forecaster.

    """

    source_code_weight_func = None

    if weight_func is not None:

        if forecaster_name in ['ForecasterAutoregMultiSeries', 'ForecasterAutoregMultiSeriesCustom']:
            if not isinstance(weight_func, (Callable, dict)):
                raise TypeError(
                    (f"Argument `weight_func` must be a Callable or a dict of "
                     f"Callables. Got {type(weight_func)}.")
                )
        elif not isinstance(weight_func, Callable):
            raise TypeError(
                f"Argument `weight_func` must be a Callable. Got {type(weight_func)}."
            )

        if isinstance(weight_func, dict):
            source_code_weight_func = {}
            for key in weight_func:
                source_code_weight_func[key] = inspect.getsource(weight_func[key])
        else:
            source_code_weight_func = inspect.getsource(weight_func)

        if 'sample_weight' not in inspect.signature(regressor.fit).parameters:
            warnings.warn(
                (f"Argument `weight_func` is ignored since regressor {regressor} "
                 f"does not accept `sample_weight` in its `fit` method."),
                 IgnoredArgumentWarning
            )
            weight_func = None
            source_code_weight_func = None

    if series_weights is not None:
        if not isinstance(series_weights, dict):
            raise TypeError(
                (f"Argument `series_weights` must be a dict of floats or ints."
                 f"Got {type(series_weights)}.")
            )
        if 'sample_weight' not in inspect.signature(regressor.fit).parameters:
            warnings.warn(
                (f"Argument `series_weights` is ignored since regressor {regressor} "
                 f"does not accept `sample_weight` in its `fit` method."),
                 IgnoredArgumentWarning
            )
            series_weights = None

    return weight_func, source_code_weight_func, series_weights

check_select_fit_kwargs(regressor, fit_kwargs=None)

Check if fit_kwargs is a dict and select only the keys that are used by the fit method of the regressor.

Parameters:

Name Type Description Default
regressor object

Regressor object.

required
fit_kwargs dict

Dictionary with the arguments to pass to the `fit' method of the forecaster.

`None`

Returns:

Name Type Description
fit_kwargs dict

Dictionary with the arguments to be passed to the fit method of the regressor after removing the unused keys.

Source code in skforecast\utils\utils.py
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
def check_select_fit_kwargs(
    regressor: object,
    fit_kwargs: Optional[dict]=None
) -> dict:
    """
    Check if `fit_kwargs` is a dict and select only the keys that are used by
    the `fit` method of the regressor.

    Parameters
    ----------
    regressor : object
        Regressor object.
    fit_kwargs : dict, default `None`
        Dictionary with the arguments to pass to the `fit' method of the forecaster.

    Returns
    -------
    fit_kwargs : dict
        Dictionary with the arguments to be passed to the `fit` method of the 
        regressor after removing the unused keys.

    """

    if fit_kwargs is None:
        fit_kwargs = {}
    else:
        if not isinstance(fit_kwargs, dict):
            raise TypeError(
                f"Argument `fit_kwargs` must be a dict. Got {type(fit_kwargs)}."
            )

        # Non used keys
        non_used_keys = [k for k in fit_kwargs.keys()
                         if k not in inspect.signature(regressor.fit).parameters]
        if non_used_keys:
            warnings.warn(
                (f"Argument/s {non_used_keys} ignored since they are not used by the "
                 f"regressor's `fit` method."),
                 IgnoredArgumentWarning
            )

        if 'sample_weight' in fit_kwargs.keys():
            warnings.warn(
                ("The `sample_weight` argument is ignored. Use `weight_func` to pass "
                 "a function that defines the individual weights for each sample "
                 "based on its index."),
                 IgnoredArgumentWarning
            )
            del fit_kwargs['sample_weight']

        # Select only the keyword arguments allowed by the regressor's `fit` method.
        fit_kwargs = {k:v for k, v in fit_kwargs.items()
                      if k in inspect.signature(regressor.fit).parameters}

    return fit_kwargs

check_y(y)

Raise Exception if y is not pandas Series or if it has missing values.

Parameters:

Name Type Description Default
y Any

Time series values.

required

Returns:

Type Description
None
Source code in skforecast\utils\utils.py
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
def check_y(
    y: Any
) -> None:
    """
    Raise Exception if `y` is not pandas Series or if it has missing values.

    Parameters
    ----------
    y : Any
        Time series values.

    Returns
    -------
    None

    """

    if not isinstance(y, pd.Series):
        raise TypeError("`y` must be a pandas Series.")

    if y.isnull().any():
        raise ValueError("`y` has missing values.")

    return

check_exog(exog, allow_nan=True)

Raise Exception if exog is not pandas Series or pandas DataFrame. If allow_nan = True, issue a warning if exog contains NaN values.

Parameters:

Name Type Description Default
exog Any

Exogenous variable/s included as predictor/s.

required
allow_nan bool

If True, allows the presence of NaN values in exog. If False (default), issue a warning if exog contains NaN values.

`True`

Returns:

Type Description
None
Source code in skforecast\utils\utils.py
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
def check_exog(
    exog: Any,
    allow_nan: bool=True
) -> None:
    """
    Raise Exception if `exog` is not pandas Series or pandas DataFrame.
    If `allow_nan = True`, issue a warning if `exog` contains NaN values.

    Parameters
    ----------
    exog : Any
        Exogenous variable/s included as predictor/s.
    allow_nan : bool, default `True`
        If True, allows the presence of NaN values in `exog`. If False (default),
        issue a warning if `exog` contains NaN values.

    Returns
    -------
    None

    """

    if not isinstance(exog, (pd.Series, pd.DataFrame)):
        raise TypeError("`exog` must be a pandas Series or DataFrame.")

    if not allow_nan:
        if exog.isnull().any().any():
            warnings.warn(
                ("`exog` has missing values. Most machine learning models do not allow "
                 "missing values. Fitting the forecaster may fail."), 
                 MissingValuesExogWarning
            )

    return

get_exog_dtypes(exog)

Store dtypes of exog.

Parameters:

Name Type Description Default
exog pandas DataFrame, pandas Series

Exogenous variable/s included as predictor/s.

required

Returns:

Name Type Description
exog_dtypes dict

Dictionary with the dtypes in exog.

Source code in skforecast\utils\utils.py
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
def get_exog_dtypes(
    exog: Union[pd.DataFrame, pd.Series]
) -> dict:
    """
    Store dtypes of `exog`.

    Parameters
    ----------
    exog : pandas DataFrame, pandas Series
        Exogenous variable/s included as predictor/s.

    Returns
    -------
    exog_dtypes : dict
        Dictionary with the dtypes in `exog`.

    """

    if isinstance(exog, pd.Series):
        exog_dtypes = {exog.name: exog.dtypes}
    else:
        exog_dtypes = exog.dtypes.to_dict()

    return exog_dtypes

check_exog_dtypes(exog)

Raise Exception if exog has categorical columns with non integer values. This is needed when using machine learning regressors that allow categorical features. Issue a Warning if exog has columns that are not init, float, or category.

Parameters:

Name Type Description Default
exog pandas DataFrame, pandas Series

Exogenous variable/s included as predictor/s.

required

Returns:

Type Description
None
Source code in skforecast\utils\utils.py
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
def check_exog_dtypes(
    exog: Union[pd.DataFrame, pd.Series]
) -> None:
    """
    Raise Exception if `exog` has categorical columns with non integer values.
    This is needed when using machine learning regressors that allow categorical
    features.
    Issue a Warning if `exog` has columns that are not `init`, `float`, or `category`.

    Parameters
    ----------
    exog : pandas DataFrame, pandas Series
        Exogenous variable/s included as predictor/s.

    Returns
    -------
    None

    """

    check_exog(exog=exog, allow_nan=False)

    if isinstance(exog, pd.DataFrame):
        if not exog.select_dtypes(exclude=[np.number, 'category']).columns.empty:
            warnings.warn(
                ("`exog` may contain only `int`, `float` or `category` dtypes. Most "
                 "machine learning models do not allow other types of values. "
                 "Fitting the forecaster may fail."), DataTypeWarning
            )
        for col in exog.select_dtypes(include='category'):
            if exog[col].cat.categories.dtype not in [int, np.int32, np.int64]:
                raise TypeError(
                    ("Categorical columns in exog must contain only integer values. "
                     "See skforecast docs for more info about how to include "
                     "categorical features https://skforecast.org/"
                     "latest/user_guides/categorical-features.html")
                )
    else:
        if exog.dtype.name not in ['int', 'int8', 'int16', 'int32', 'int64', 'float', 
        'float16', 'float32', 'float64', 'uint8', 'uint16', 'uint32', 'uint64', 'category']:
            warnings.warn(
                ("`exog` may contain only `int`, `float` or `category` dtypes. Most "
                 "machine learning models do not allow other types of values. "
                 "Fitting the forecaster may fail."), DataTypeWarning
            )
        if exog.dtype.name == 'category' and exog.cat.categories.dtype not in [int,
        np.int32, np.int64]:
            raise TypeError(
                ("If exog is of type category, it must contain only integer values. "
                 "See skforecast docs for more info about how to include "
                 "categorical features https://skforecast.org/"
                 "latest/user_guides/categorical-features.html")
            )

    return

check_interval(interval=None, alpha=None)

Check provided confidence interval sequence is valid.

Parameters:

Name Type Description Default
interval list

Confidence of the prediction interval estimated. Sequence of percentiles to compute, which must be between 0 and 100 inclusive. For example, interval of 95% should be as interval = [2.5, 97.5].

`None`
alpha float

The confidence intervals used in ForecasterSarimax are (1 - alpha) %.

`None`

Returns:

Type Description
None
Source code in skforecast\utils\utils.py
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
def check_interval(
    interval: list=None,
    alpha: float=None
) -> None:
    """
    Check provided confidence interval sequence is valid.

    Parameters
    ----------
    interval : list, default `None`
        Confidence of the prediction interval estimated. Sequence of percentiles
        to compute, which must be between 0 and 100 inclusive. For example, 
        interval of 95% should be as `interval = [2.5, 97.5]`.
    alpha : float, default `None`
        The confidence intervals used in ForecasterSarimax are (1 - alpha) %.

    Returns
    -------
    None

    """

    if interval is not None:
        if not isinstance(interval, list):
            raise TypeError(
                ("`interval` must be a `list`. For example, interval of 95% "
                 "should be as `interval = [2.5, 97.5]`.")
            )

        if len(interval) != 2:
            raise ValueError(
                ("`interval` must contain exactly 2 values, respectively the "
                 "lower and upper interval bounds. For example, interval of 95% "
                 "should be as `interval = [2.5, 97.5]`.")
            )

        if (interval[0] < 0.) or (interval[0] >= 100.):
            raise ValueError(
                f"Lower interval bound ({interval[0]}) must be >= 0 and < 100."
            )

        if (interval[1] <= 0.) or (interval[1] > 100.):
            raise ValueError(
                f"Upper interval bound ({interval[1]}) must be > 0 and <= 100."
            )

        if interval[0] >= interval[1]:
            raise ValueError(
                f"Lower interval bound ({interval[0]}) must be less than the "
                f"upper interval bound ({interval[1]})."
            )

    if alpha is not None:
        if not isinstance(alpha, float):
            raise TypeError(
                ("`alpha` must be a `float`. For example, interval of 95% "
                 "should be as `alpha = 0.05`.")
            )

        if (alpha <= 0.) or (alpha >= 1):
            raise ValueError(
                f"`alpha` must have a value between 0 and 1. Got {alpha}."
            )

    return

check_predict_input(forecaster_name, steps, fitted, included_exog, index_type, index_freq, window_size, last_window=None, last_window_exog=None, exog=None, exog_type=None, exog_col_names=None, interval=None, alpha=None, max_steps=None, levels=None, series_col_names=None)

Check all inputs of predict method. This is a helper function to validate that inputs used in predict method match attributes of a forecaster already trained.

Parameters:

Name Type Description Default
forecaster_name str

Forecaster name. ForecasterAutoreg, ForecasterAutoregCustom, ForecasterAutoregDirect, ForecasterAutoregMultiSeries, ForecasterAutoregMultiSeriesCustom, ForecasterAutoregMultiVariate.

required
steps int, list

Number of future steps predicted.

required
fitted bool

Tag to identify if the regressor has been fitted (trained).

required
included_exog bool

If the forecaster has been trained using exogenous variable/s.

required
index_type type

Type of index of the input used in training.

required
index_freq str

Frequency of Index of the input used in training.

required
window_size int

Size of the window needed to create the predictors. It is equal to max_lag.

required
last_window pandas Series, pandas DataFrame

Values of the series used to create the predictors (lags) need in the first iteration of prediction (t + 1).

`None`
last_window_exog pandas Series, pandas DataFrame

Values of the exogenous variables aligned with last_window in ForecasterSarimax predictions.

`None`
exog pandas Series, pandas DataFrame

Exogenous variable/s included as predictor/s.

`None`
exog_type type

Type of exogenous variable/s used in training.

`None`
exog_col_names list

Names of columns of exog if exog used in training was a pandas DataFrame.

`None`
interval list

Confidence of the prediction interval estimated. Sequence of percentiles to compute, which must be between 0 and 100 inclusive. For example, interval of 95% should be as interval = [2.5, 97.5].

`None`
alpha float

The confidence intervals used in ForecasterSarimax are (1 - alpha) %.

`None`
max_steps Optional[int]

Maximum number of steps allowed (ForecasterAutoregDirect and ForecasterAutoregMultiVariate).

None
levels str, list

Time series to be predicted (ForecasterAutoregMultiSeries and ForecasterAutoregMultiSeriesCustom).

`None`
series_col_names list

Names of the columns used during fit (ForecasterAutoregMultiSeries, ForecasterAutoregMultiSeriesCustom and ForecasterAutoregMultiVariate).

`None`

Returns:

Type Description
None
Source code in skforecast\utils\utils.py
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
def check_predict_input(
    forecaster_name: str,
    steps: Union[int, list],
    fitted: bool,
    included_exog: bool,
    index_type: type,
    index_freq: str,
    window_size: int,
    last_window: Optional[Union[pd.Series, pd.DataFrame]]=None,
    last_window_exog: Optional[Union[pd.Series, pd.DataFrame]]=None,
    exog: Optional[Union[pd.Series, pd.DataFrame]]=None,
    exog_type: Optional[Union[type, None]]=None,
    exog_col_names: Optional[Union[list, None]]=None,
    interval: Optional[list]=None,
    alpha: Optional[float]=None,
    max_steps: Optional[int]=None,
    levels: Optional[Union[str, list]]=None,
    series_col_names: Optional[list]=None
) -> None:
    """
    Check all inputs of predict method. This is a helper function to validate
    that inputs used in predict method match attributes of a forecaster already
    trained.

    Parameters
    ----------
    forecaster_name : str
        Forecaster name. ForecasterAutoreg, ForecasterAutoregCustom, 
        ForecasterAutoregDirect, ForecasterAutoregMultiSeries, 
        ForecasterAutoregMultiSeriesCustom, ForecasterAutoregMultiVariate.
    steps : int, list
        Number of future steps predicted.
    fitted: bool
        Tag to identify if the regressor has been fitted (trained).
    included_exog : bool
        If the forecaster has been trained using exogenous variable/s.
    index_type : type
        Type of index of the input used in training.
    index_freq : str
        Frequency of Index of the input used in training.
    window_size: int
        Size of the window needed to create the predictors. It is equal to 
        `max_lag`.
    last_window : pandas Series, pandas DataFrame, default `None`
        Values of the series used to create the predictors (lags) need in the 
        first iteration of prediction (t + 1).
    last_window_exog : pandas Series, pandas DataFrame, default `None`
        Values of the exogenous variables aligned with `last_window` in 
        ForecasterSarimax predictions.
    exog : pandas Series, pandas DataFrame, default `None`
        Exogenous variable/s included as predictor/s.
    exog_type : type, default `None`
        Type of exogenous variable/s used in training.
    exog_col_names : list, default `None`
        Names of columns of `exog` if `exog` used in training was a pandas
        DataFrame.
    interval : list, default `None`
        Confidence of the prediction interval estimated. Sequence of percentiles
        to compute, which must be between 0 and 100 inclusive. For example, 
        interval of 95% should be as `interval = [2.5, 97.5]`.
    alpha : float, default `None`
        The confidence intervals used in ForecasterSarimax are (1 - alpha) %.
    max_steps: int, default `None`
        Maximum number of steps allowed (`ForecasterAutoregDirect` and 
        `ForecasterAutoregMultiVariate`).
    levels : str, list, default `None`
        Time series to be predicted (`ForecasterAutoregMultiSeries` and
        `ForecasterAutoregMultiSeriesCustom`).
    series_col_names : list, default `None`
        Names of the columns used during fit (`ForecasterAutoregMultiSeries`, 
        `ForecasterAutoregMultiSeriesCustom` and `ForecasterAutoregMultiVariate`).

    Returns
    -------
    None

    """

    if not fitted:
        raise sklearn.exceptions.NotFittedError(
            ("This Forecaster instance is not fitted yet. Call `fit` with "
             "appropriate arguments before using predict.")
        )

    if isinstance(steps, (int, np.integer)) and steps < 1:
        raise ValueError(
            f"`steps` must be an integer greater than or equal to 1. Got {steps}."
        )

    if isinstance(steps, list) and min(steps) < 1:
        raise ValueError(
           (f"The minimum value of `steps` must be equal to or greater than 1. "
            f"Got {min(steps)}.")
        )

    if max_steps is not None:
        if max(steps) > max_steps:
            raise ValueError(
                (f"The maximum value of `steps` must be less than or equal to "
                 f"the value of steps defined when initializing the forecaster. "
                 f"Got {max(steps)}, but the maximum is {max_steps}.")
            )

    if interval is not None or alpha is not None:
        check_interval(interval=interval, alpha=alpha)

    if forecaster_name in ['ForecasterAutoregMultiSeries', 
                           'ForecasterAutoregMultiSeriesCustom']:
        if levels is not None and not isinstance(levels, (str, list)):
            raise TypeError(
                ("`levels` must be a `list` of column names, a `str` of a "
                 "column name or `None`.")
            )
        if len(set(levels) - set(series_col_names)) != 0:
            raise ValueError(
                f"`levels` must be in `series_col_names` : {series_col_names}."
            )

    if exog is None and included_exog:
        raise ValueError(
            ("Forecaster trained with exogenous variable/s. "
             "Same variable/s must be provided when predicting.")
        )

    if exog is not None and not included_exog:
        raise ValueError(
            ("Forecaster trained without exogenous variable/s. "
             "`exog` must be `None` when predicting.")
        )

    # Checks last_window
    # Check last_window type (pd.Series or pd.DataFrame according to forecaster)
    if forecaster_name in ['ForecasterAutoregMultiSeries', 
                           'ForecasterAutoregMultiSeriesCustom',
                           'ForecasterAutoregMultiVariate']:
        if not isinstance(last_window, pd.DataFrame):
            raise TypeError(
                f"`last_window` must be a pandas DataFrame. Got {type(last_window)}."
            )

        if forecaster_name in ['ForecasterAutoregMultiSeries', 
                               'ForecasterAutoregMultiSeriesCustom'] and \
            len(set(levels) - set(last_window.columns)) != 0:
            raise ValueError(
                (f"`last_window` must contain a column(s) named as the level(s) "
                 f"to be predicted.\n"
                 f"    `levels` : {levels}.\n"
                 f"    `last_window` columns : {list(last_window.columns)}.")
            )

        if forecaster_name == 'ForecasterAutoregMultiVariate' and \
            (series_col_names != list(last_window.columns)):
            raise ValueError(
                (f"`last_window` columns must be the same as `series` column names.\n"
                 f"    `last_window` columns : {list(last_window.columns)}.\n"
                 f"    `series` columns      : {series_col_names}.")
            )    
    else:    
        if not isinstance(last_window, pd.Series):
            raise TypeError(
                f"`last_window` must be a pandas Series. Got {type(last_window)}."
            )

    # Check last_window len, nulls and index (type and freq)
    if len(last_window) < window_size:
        raise ValueError(
            (f"`last_window` must have as many values as needed to "
             f"generate the predictors. For this forecaster it is {window_size}.")
        )
    if last_window.isnull().any().all():
        raise ValueError(
            ("`last_window` has missing values.")
        )
    _, last_window_index = preprocess_last_window(
                               last_window  = last_window.iloc[:0],
                               return_values = False
                           ) 
    if not isinstance(last_window_index, index_type):
        raise TypeError(
            (f"Expected index of type {index_type} for `last_window`. "
             f"Got {type(last_window_index)}.")
        )
    if isinstance(last_window_index, pd.DatetimeIndex):
        if not last_window_index.freqstr == index_freq:
            raise TypeError(
                (f"Expected frequency of type {index_freq} for `last_window`. "
                 f"Got {last_window_index.freqstr}.")
            )

    # Checks exog
    if exog is not None:
        # Check type, nulls and expected type
        if not isinstance(exog, (pd.Series, pd.DataFrame)):
            raise TypeError("`exog` must be a pandas Series or DataFrame.")
        if exog.isnull().any().any():
            warnings.warn(
                ("`exog` has missing values. Most of machine learning models do "
                 "not allow missing values. `predict` method may fail."), 
                 MissingValuesExogWarning
            )
        if not isinstance(exog, exog_type):
            raise TypeError(
                f"Expected type for `exog`: {exog_type}. Got {type(exog)}."    
            )

        # Check exog has many values as distance to max step predicted
        last_step = max(steps) if isinstance(steps, list) else steps
        if len(exog) < last_step:
            raise ValueError(
                (f"`exog` must have at least as many values as the distance to "
                 f"the maximum step predicted, {last_step}.")
            )

        # Check all columns are in the pandas DataFrame
        if isinstance(exog, pd.DataFrame):
            col_missing = set(exog_col_names).difference(set(exog.columns))
            if col_missing:
                raise ValueError(
                    (f"Missing columns in `exog`. Expected {exog_col_names}. "
                     f"Got {exog.columns.to_list()}.") 
                )

        # Check index dtype and freq
        _, exog_index = preprocess_exog(
                            exog          = exog.iloc[:0, ],
                            return_values = False
                        )
        if not isinstance(exog_index, index_type):
            raise TypeError(
                (f"Expected index of type {index_type} for `exog`. "
                 f"Got {type(exog_index)}.")
            )   
        if isinstance(exog_index, pd.DatetimeIndex):
            if not exog_index.freqstr == index_freq:
                raise TypeError(
                    (f"Expected frequency of type {index_freq} for `exog`. "
                     f"Got {exog_index.freqstr}.")
                )

        # Check exog starts one step ahead of last_window end.
        expected_index = expand_index(last_window.index, 1)[0]
        if expected_index != exog.index[0]:
            raise ValueError(
                (f"To make predictions `exog` must start one step ahead of `last_window`.\n"
                 f"    `last_window` ends at : {last_window.index[-1]}.\n"
                 f"    `exog` starts at      : {exog.index[0]}.\n"
                 f"     Expected index       : {expected_index}.")
            )

    # Checks ForecasterSarimax
    if forecaster_name == 'ForecasterSarimax':
        # Check last_window_exog type, len, nulls and index (type and freq)
        if last_window_exog is not None:
            if not included_exog:
                raise ValueError(
                    ("Forecaster trained without exogenous variable/s. "
                     "`last_window_exog` must be `None` when predicting.")
                )

            if not isinstance(last_window_exog, (pd.Series, pd.DataFrame)):
                raise TypeError(
                    (f"`last_window_exog` must be a pandas Series or a "
                     f"pandas DataFrame. Got {type(last_window_exog)}.")
                )
            if len(last_window_exog) < window_size:
                raise ValueError(
                    (f"`last_window_exog` must have as many values as needed to "
                     f"generate the predictors. For this forecaster it is {window_size}.")
                )
            if last_window_exog.isnull().any().all():
                warnings.warn(
                ("`last_window_exog` has missing values. Most of machine learning "
                 "models do not allow missing values. `predict` method may fail."),
                MissingValuesExogWarning
            )
            _, last_window_exog_index = preprocess_last_window(
                                            last_window   = last_window_exog.iloc[:0],
                                            return_values = False
                                        ) 
            if not isinstance(last_window_exog_index, index_type):
                raise TypeError(
                    (f"Expected index of type {index_type} for `last_window_exog`. "
                     f"Got {type(last_window_exog_index)}.")
                )
            if isinstance(last_window_exog_index, pd.DatetimeIndex):
                if not last_window_exog_index.freqstr == index_freq:
                    raise TypeError(
                        (f"Expected frequency of type {index_freq} for "
                         f"`last_window_exog`. Got {last_window_exog_index.freqstr}.")
                    )

            # Check all columns are in the pd.DataFrame, last_window_exog
            if isinstance(last_window_exog, pd.DataFrame):
                col_missing = set(exog_col_names).difference(set(last_window_exog.columns))
                if col_missing:
                    raise ValueError(
                        (f"Missing columns in `exog`. Expected {exog_col_names}. "
                         f"Got {last_window_exog.columns.to_list()}.") 
                    )

    return

preprocess_y(y, return_values=True)

Return values and index of series separately. Index is overwritten according to the next rules:

  • If index is of type DatetimeIndex and has frequency, nothing is changed.
  • If index is of type RangeIndex, nothing is changed.
  • If index is of type DatetimeIndex but has no frequency, a RangeIndex is created.
  • If index is not of type DatetimeIndex, a RangeIndex is created.

Parameters:

Name Type Description Default
y pandas Series, pandas DataFrame

Time series.

required
return_values bool

If True return the values of y as numpy ndarray. This option is intended to avoid copying data when it is not necessary.

`True`

Returns:

Name Type Description
y_values None, numpy ndarray

Numpy array with values of y.

y_index pandas Index

Index of y modified according to the rules.

Source code in skforecast\utils\utils.py
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
def preprocess_y(
    y: Union[pd.Series, pd.DataFrame],
    return_values: bool=True
) -> Tuple[Union[None, np.ndarray], pd.Index]:
    """
    Return values and index of series separately. Index is overwritten 
    according to the next rules:

    - If index is of type `DatetimeIndex` and has frequency, nothing is 
    changed.
    - If index is of type `RangeIndex`, nothing is changed.
    - If index is of type `DatetimeIndex` but has no frequency, a 
    `RangeIndex` is created.
    - If index is not of type `DatetimeIndex`, a `RangeIndex` is created.

    Parameters
    ----------
    y : pandas Series, pandas DataFrame
        Time series.
    return_values : bool, default `True`
        If `True` return the values of `y` as numpy ndarray. This option is 
        intended to avoid copying data when it is not necessary.

    Returns
    -------
    y_values : None, numpy ndarray
        Numpy array with values of `y`.
    y_index : pandas Index
        Index of `y` modified according to the rules.

    """

    if isinstance(y.index, pd.DatetimeIndex) and y.index.freq is not None:
        y_index = y.index
    elif isinstance(y.index, pd.RangeIndex):
        y_index = y.index
    elif isinstance(y.index, pd.DatetimeIndex) and y.index.freq is None:
        warnings.warn(
            ("`y` has DatetimeIndex index but no frequency. "
             "Index is overwritten with a RangeIndex of step 1.")
        )
        y_index = pd.RangeIndex(
                      start = 0,
                      stop  = len(y),
                      step  = 1
                  )
    else:
        warnings.warn(
            ("`y` has no DatetimeIndex nor RangeIndex index. "
             "Index is overwritten with a RangeIndex.")
        )
        y_index = pd.RangeIndex(
                      start = 0,
                      stop  = len(y),
                      step  = 1
                  )

    y_values = y.to_numpy() if return_values else None

    return y_values, y_index

preprocess_last_window(last_window, return_values=True)

Return values and index of series separately. Index is overwritten according to the next rules:

  • If index is of type DatetimeIndex and has frequency, nothing is changed.
  • If index is of type RangeIndex, nothing is changed.
  • If index is of type DatetimeIndex but has no frequency, a RangeIndex is created.
  • If index is not of type DatetimeIndex, a RangeIndex is created.

Parameters:

Name Type Description Default
last_window pandas Series, pandas DataFrame

Time series values.

required
return_values bool

If True return the values of last_window as numpy ndarray. This option is intended to avoid copying data when it is not necessary.

`True`

Returns:

Name Type Description
last_window_values numpy ndarray

Numpy array with values of last_window.

last_window_index pandas Index

Index of last_window modified according to the rules.

Source code in skforecast\utils\utils.py
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
def preprocess_last_window(
    last_window: Union[pd.Series, pd.DataFrame],
    return_values: bool=True
 ) -> Tuple[np.ndarray, pd.Index]:
    """
    Return values and index of series separately. Index is overwritten 
    according to the next rules:

    - If index is of type `DatetimeIndex` and has frequency, nothing is 
    changed.
    - If index is of type `RangeIndex`, nothing is changed.
    - If index is of type `DatetimeIndex` but has no frequency, a 
    `RangeIndex` is created.
    - If index is not of type `DatetimeIndex`, a `RangeIndex` is created.

    Parameters
    ----------
    last_window : pandas Series, pandas DataFrame
        Time series values.
    return_values : bool, default `True`
        If `True` return the values of `last_window` as numpy ndarray. This option 
        is intended to avoid copying data when it is not necessary.

    Returns
    -------
    last_window_values : numpy ndarray
        Numpy array with values of `last_window`.
    last_window_index : pandas Index
        Index of `last_window` modified according to the rules.

    """

    if isinstance(last_window.index, pd.DatetimeIndex) and last_window.index.freq is not None:
        last_window_index = last_window.index
    elif isinstance(last_window.index, pd.RangeIndex):
        last_window_index = last_window.index
    elif isinstance(last_window.index, pd.DatetimeIndex) and last_window.index.freq is None:
        warnings.warn(
            ("`last_window` has DatetimeIndex index but no frequency. "
             "Index is overwritten with a RangeIndex of step 1.")
        )
        last_window_index = pd.RangeIndex(
                                start = 0,
                                stop  = len(last_window),
                                step  = 1
                            )
    else:
        warnings.warn(
            ("`last_window` has no DatetimeIndex nor RangeIndex index. "
             "Index is overwritten with a RangeIndex.")
        )
        last_window_index = pd.RangeIndex(
                                start = 0,
                                stop  = len(last_window),
                                step  = 1
                            )

    last_window_values = last_window.to_numpy() if return_values else None

    return last_window_values, last_window_index

preprocess_exog(exog, return_values=True)

Return values and index of series or data frame separately. Index is overwritten according to the next rules:

  • If index is of type DatetimeIndex and has frequency, nothing is changed.
  • If index is of type RangeIndex, nothing is changed.
  • If index is of type DatetimeIndex but has no frequency, a RangeIndex is created.
  • If index is not of type DatetimeIndex, a RangeIndex is created.

Parameters:

Name Type Description Default
exog pandas Series, pandas DataFrame

Exogenous variables.

required
return_values bool

If True return the values of exog as numpy ndarray. This option is intended to avoid copying data when it is not necessary.

`True`

Returns:

Name Type Description
exog_values None, numpy ndarray

Numpy array with values of exog.

exog_index pandas Index

Index of exog modified according to the rules.

Source code in skforecast\utils\utils.py
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
def preprocess_exog(
    exog: Union[pd.Series, pd.DataFrame],
    return_values: bool=True
) -> Tuple[Union[None, np.ndarray], pd.Index]:
    """
    Return values and index of series or data frame separately. Index is
    overwritten  according to the next rules:

    - If index is of type `DatetimeIndex` and has frequency, nothing is 
    changed.
    - If index is of type `RangeIndex`, nothing is changed.
    - If index is of type `DatetimeIndex` but has no frequency, a 
    `RangeIndex` is created.
    - If index is not of type `DatetimeIndex`, a `RangeIndex` is created.

    Parameters
    ----------
    exog : pandas Series, pandas DataFrame
        Exogenous variables.
    return_values : bool, default `True`
        If `True` return the values of `exog` as numpy ndarray. This option is 
        intended to avoid copying data when it is not necessary.

    Returns
    -------
    exog_values : None, numpy ndarray
        Numpy array with values of `exog`.
    exog_index : pandas Index
        Index of `exog` modified according to the rules.

    """

    if isinstance(exog.index, pd.DatetimeIndex) and exog.index.freq is not None:
        exog_index = exog.index
    elif isinstance(exog.index, pd.RangeIndex):
        exog_index = exog.index
    elif isinstance(exog.index, pd.DatetimeIndex) and exog.index.freq is None:
        warnings.warn(
            ("`exog` has DatetimeIndex index but no frequency. "
             "Index is overwritten with a RangeIndex of step 1.")
        )
        exog_index = pd.RangeIndex(
                         start = 0,
                         stop  = len(exog),
                         step  = 1
                     )

    else:
        warnings.warn(
            ("`exog` has no DatetimeIndex nor RangeIndex index. "
             "Index is overwritten with a RangeIndex.")
        )
        exog_index = pd.RangeIndex(
                         start = 0,
                         stop  = len(exog),
                         step  = 1
                     )

    exog_values = exog.to_numpy() if return_values else None

    return exog_values, exog_index

cast_exog_dtypes(exog, exog_dtypes)

Cast exog to a specified types. This is done because, for a forecaster to accept a categorical exog, it must contain only integer values. Due to the internal modifications of numpy, the values may be casted to float, so they have to be re-converted to int.

  • If exog is a pandas Series, exog_dtypes must be a dict with a single value.
  • If exog_dtypes is category but the current type of exog is float, then the type is cast to int and then to category.

Parameters:

Name Type Description Default
exog pandas Series, pandas DataFrame

Exogenous variables.

required
exog_dtypes dict

Dictionary with name and type of the series or data frame columns.

required

Returns:

Name Type Description
exog pandas Series, pandas DataFrame

Exogenous variables casted to the indicated dtypes.

Source code in skforecast\utils\utils.py
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
def cast_exog_dtypes(
    exog: Union[pd.Series, pd.DataFrame],
    exog_dtypes: dict,
) -> Union[pd.Series, pd.DataFrame]: # pragma: no cover
    """
    Cast `exog` to a specified types. This is done because, for a forecaster to 
    accept a categorical exog, it must contain only integer values. Due to the 
    internal modifications of numpy, the values may be casted to `float`, so 
    they have to be re-converted to `int`.

    - If `exog` is a pandas Series, `exog_dtypes` must be a dict with a 
    single value.
    - If `exog_dtypes` is `category` but the current type of `exog` is `float`, 
    then the type is cast to `int` and then to `category`. 

    Parameters
    ----------
    exog : pandas Series, pandas DataFrame
        Exogenous variables.
    exog_dtypes: dict
        Dictionary with name and type of the series or data frame columns.

    Returns
    -------
    exog : pandas Series, pandas DataFrame
        Exogenous variables casted to the indicated dtypes.

    """

    # Remove keys from exog_dtypes not in exog.columns
    exog_dtypes = {k:v for k, v in exog_dtypes.items() if k in exog.columns}

    if isinstance(exog, pd.Series) and exog.dtypes != list(exog_dtypes.values())[0]:
        exog = exog.astype(list(exog_dtypes.values())[0])
    elif isinstance(exog, pd.DataFrame):
        for col, initial_dtype in exog_dtypes.items():
            if exog[col].dtypes != initial_dtype:
                if initial_dtype == "category" and exog[col].dtypes==float:
                    exog[col] = exog[col].astype(int).astype("category")
                else:
                    exog[col] = exog[col].astype(initial_dtype)

    return exog

exog_to_direct(exog, steps)

Transforms exog to a pandas DataFrame with the shape needed for Direct forecasting.

Parameters:

Name Type Description Default
exog pandas Series, pandas DataFrame

Exogenous variables.

required
steps int.

Number of steps that will be predicted using exog.

required

Returns:

Name Type Description
exog_transformed pandas DataFrame

Exogenous variables transformed.

Source code in skforecast\utils\utils.py
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
def exog_to_direct(
    exog: Union[pd.Series, pd.DataFrame],
    steps: int
)-> pd.DataFrame:
    """
    Transforms `exog` to a pandas DataFrame with the shape needed for Direct
    forecasting.

    Parameters
    ----------
    exog : pandas Series, pandas DataFrame
        Exogenous variables.
    steps : int.
        Number of steps that will be predicted using exog.

    Returns
    -------
    exog_transformed : pandas DataFrame
        Exogenous variables transformed.

    """

    if not isinstance(exog, (pd.Series, pd.DataFrame)):
        raise TypeError(f"`exog` must be a pandas Series or DataFrame. Got {type(exog)}.")

    if isinstance(exog, pd.Series):
        exog = exog.to_frame()

    n_rows = len(exog)
    exog_idx = exog.index
    exog_transformed = []

    for i in range(steps):
        exog_column_transformed = exog.iloc[i : n_rows - (steps - 1 - i), ]
        exog_column_transformed.index = pd.RangeIndex(len(exog_column_transformed))
        exog_column_transformed.columns = [f"{col}_step_{i+1}" 
                                           for col in exog_column_transformed.columns]
        exog_transformed.append(exog_column_transformed)

    if len(exog_transformed) > 1:
        exog_transformed = pd.concat(exog_transformed, axis=1, copy=False)
    else:
        exog_transformed = exog_column_transformed

    exog_transformed.index = exog_idx[-len(exog_transformed):]

    return exog_transformed

exog_to_direct_numpy(exog, steps)

Transforms exog to numpy ndarray with the shape needed for Direct forecasting.

Parameters:

Name Type Description Default
exog numpy ndarray, shape(samples,)

Exogenous variables.

required
steps int.

Number of steps that will be predicted using exog.

required

Returns:

Name Type Description
exog_transformed numpy ndarray

Exogenous variables transformed.

Source code in skforecast\utils\utils.py
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
def exog_to_direct_numpy(
    exog: np.ndarray,
    steps: int
)-> np.ndarray:
    """
    Transforms `exog` to numpy ndarray with the shape needed for Direct
    forecasting.

    Parameters
    ----------
    exog : numpy ndarray, shape(samples,)
        Exogenous variables.
    steps : int.
        Number of steps that will be predicted using exog.

    Returns
    -------
    exog_transformed : numpy ndarray
        Exogenous variables transformed.

    """

    if not isinstance(exog, np.ndarray):
        raise TypeError(f"`exog` must be a numpy ndarray. Got {type(exog)}.")

    if exog.ndim == 1:
        exog = np.expand_dims(exog, axis=1)

    n_rows = len(exog)
    exog_transformed = []

    for i in range(steps):
        exog_column_transformed = exog[i : n_rows - (steps - 1 - i)]
        exog_transformed.append(exog_column_transformed)

    if len(exog_transformed) > 1:
        exog_transformed = np.concatenate(exog_transformed, axis=1)
    else:
        exog_transformed = exog_column_transformed

    return exog_transformed

expand_index(index, steps)

Create a new index of length steps starting at the end of the index.

Parameters:

Name Type Description Default
index pandas Index, None

Original index.

required
steps int

Number of steps to expand.

required

Returns:

Name Type Description
new_index pandas Index

New index.

Source code in skforecast\utils\utils.py
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
def expand_index(
    index: Union[pd.Index, None], 
    steps: int
) -> pd.Index:
    """
    Create a new index of length `steps` starting at the end of the index.

    Parameters
    ----------
    index : pandas Index, None
        Original index.
    steps : int
        Number of steps to expand.

    Returns
    -------
    new_index : pandas Index
        New index.

    """

    if isinstance(index, pd.Index):

        if isinstance(index, pd.DatetimeIndex):
            new_index = pd.date_range(
                            start   = index[-1] + index.freq,
                            periods = steps,
                            freq    = index.freq
                        )
        elif isinstance(index, pd.RangeIndex):
            new_index = pd.RangeIndex(
                            start = index[-1] + 1,
                            stop  = index[-1] + 1 + steps
                        )
    else: 
        new_index = pd.RangeIndex(
                        start = 0,
                        stop  = steps
                    )

    return new_index

transform_series(series, transformer, fit=False, inverse_transform=False)

Transform raw values of pandas Series with a scikit-learn alike transformer (preprocessor). The transformer used must have the following methods: fit, transform, fit_transform and inverse_transform. ColumnTransformers are not allowed since they do not have inverse_transform method.

Parameters:

Name Type Description Default
series pandas Series

Series to be transformed.

required
transformer scikit-learn alike transformer (preprocessor).

scikit-learn alike transformer (preprocessor) with methods: fit, transform, fit_transform and inverse_transform. ColumnTransformers are not allowed since they do not have inverse_transform method.

required
fit bool

Train the transformer before applying it.

`False`
inverse_transform bool

Transform back the data to the original representation.

`False`

Returns:

Name Type Description
series_transformed pandas Series, pandas DataFrame

Transformed Series. Depending on the transformer used, the output may be a Series or a DataFrame.

Source code in skforecast\utils\utils.py
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
def transform_series(
    series: pd.Series,
    transformer,
    fit: bool=False,
    inverse_transform: bool=False
) -> Union[pd.Series, pd.DataFrame]:
    """      
    Transform raw values of pandas Series with a scikit-learn alike transformer
    (preprocessor). The transformer used must have the following methods: fit, 
    transform, fit_transform and inverse_transform. ColumnTransformers are not 
    allowed since they do not have inverse_transform method.

    Parameters
    ----------
    series : pandas Series
        Series to be transformed.
    transformer : scikit-learn alike transformer (preprocessor).
        scikit-learn alike transformer (preprocessor) with methods: fit, transform,
        fit_transform and inverse_transform. ColumnTransformers are not allowed 
        since they do not have inverse_transform method.
    fit : bool, default `False`
        Train the transformer before applying it.
    inverse_transform : bool, default `False`
        Transform back the data to the original representation.

    Returns
    -------
    series_transformed : pandas Series, pandas DataFrame
        Transformed Series. Depending on the transformer used, the output may 
        be a Series or a DataFrame.

    """

    if not isinstance(series, pd.Series):
        raise TypeError(
            (f"`series` argument must be a pandas Series. Got {type(series)}.")
        )

    if transformer is None:
        return series

    if series.name is None:
        series.name = 'no_name'

    data = series.to_frame()

    if fit and hasattr(transformer, 'fit'):
        transformer.fit(data)

    # If argument feature_names_in_ exits, is overwritten to allow using the 
    # transformer on other series than those that were passed during fit.
    if hasattr(transformer, 'feature_names_in_') and transformer.feature_names_in_[0] != data.columns[0]:
        transformer = deepcopy(transformer)
        transformer.feature_names_in_ = np.array([data.columns[0]], dtype=object)

    if inverse_transform:
        values_transformed = transformer.inverse_transform(data)
    else:
        values_transformed = transformer.transform(data)   

    if hasattr(values_transformed, 'toarray'):
        # If the returned values are in sparse matrix format, it is converted to dense array.
        values_transformed = values_transformed.toarray()

    if isinstance(values_transformed, np.ndarray) and values_transformed.shape[1] == 1:
        series_transformed = pd.Series(
                                 data  = values_transformed.flatten(),
                                 index = data.index,
                                 name  = data.columns[0]
                             )
    elif isinstance(values_transformed, pd.DataFrame) and values_transformed.shape[1] == 1:
        series_transformed = values_transformed.squeeze()
    else:
        series_transformed = pd.DataFrame(
                                 data    = values_transformed,
                                 index   = data.index,
                                 columns = transformer.get_feature_names_out()
                             )

    return series_transformed

transform_dataframe(df, transformer, fit=False, inverse_transform=False)

Transform raw values of pandas DataFrame with a scikit-learn alike transformer, preprocessor or ColumnTransformer. inverse_transform is not available when using ColumnTransformers.

Parameters:

Name Type Description Default
df pandas DataFrame

DataFrame to be transformed.

required
transformer scikit-learn alike transformer, preprocessor or ColumnTransformer.

scikit-learn alike transformer, preprocessor or ColumnTransformer.

required
fit bool

Train the transformer before applying it.

`False`
inverse_transform bool

Transform back the data to the original representation. This is not available when using transformers of class scikit-learn ColumnTransformers.

`False`

Returns:

Name Type Description
df_transformed pandas DataFrame

Transformed DataFrame.

Source code in skforecast\utils\utils.py
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
def transform_dataframe(
    df: pd.DataFrame,
    transformer,
    fit: bool=False,
    inverse_transform: bool=False
) -> pd.DataFrame:
    """      
    Transform raw values of pandas DataFrame with a scikit-learn alike
    transformer, preprocessor or ColumnTransformer. `inverse_transform` is not 
    available when using ColumnTransformers.

    Parameters
    ----------
    df : pandas DataFrame
        DataFrame to be transformed.
    transformer : scikit-learn alike transformer, preprocessor or ColumnTransformer.
        scikit-learn alike transformer, preprocessor or ColumnTransformer.
    fit : bool, default `False`
        Train the transformer before applying it.
    inverse_transform : bool, default `False`
        Transform back the data to the original representation. This is not available
        when using transformers of class scikit-learn ColumnTransformers.

    Returns
    -------
    df_transformed : pandas DataFrame
        Transformed DataFrame.

    """

    if not isinstance(df, pd.DataFrame):
        raise TypeError(
            f"`df` argument must be a pandas DataFrame. Got {type(df)}"
        )

    if transformer is None:
        return df

    if inverse_transform and isinstance(transformer, ColumnTransformer):
        raise Exception(
            "`inverse_transform` is not available when using ColumnTransformers."
        )

    if not inverse_transform:
        if fit:
            values_transformed = transformer.fit_transform(df)
        else:
            values_transformed = transformer.transform(df)
    else:
        values_transformed = transformer.inverse_transform(df)

    if hasattr(values_transformed, 'toarray'):
        # If the returned values are in sparse matrix format, it is converted to dense
        values_transformed = values_transformed.toarray()

    if hasattr(transformer, 'get_feature_names_out'):
        feature_names_out = transformer.get_feature_names_out()
    elif hasattr(transformer, 'categories_'):   
        feature_names_out = transformer.categories_
    else:
        feature_names_out = df.columns

    df_transformed = pd.DataFrame(
                         data    = values_transformed,
                         index   = df.index,
                         columns = feature_names_out
                     )

    return df_transformed

check_optional_dependency(package_name)

Check if an optional dependency is installed, if not raise an ImportError
with installation instructions.

Parameters:

Name Type Description Default
package_name str

Name of the package to check.

required

Returns:

Type Description
None
Source code in skforecast\utils\utils.py
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
def check_optional_dependency(
    package_name: str
) -> None:
    """
    Check if an optional dependency is installed, if not raise an ImportError  
    with installation instructions.

    Parameters
    ----------
    package_name : str
        Name of the package to check.

    Returns
    -------
    None

    """

    if importlib.util.find_spec(package_name) is None:
        try:
            extra, package_version = _find_optional_dependency(package_name=package_name)
            msg = (
                f"\n'{package_name}' is an optional dependency not included in the default "
                f"skforecast installation. Please run: `pip install \"{package_version}\"` to install it."
                f"\n\nAlternately, you can install it by running `pip install skforecast[{extra}]`"
            )
        except:
            msg = f"\n'{package_name}' is needed but not installed. Please install it."

        raise ImportError(msg)

multivariate_time_series_corr(time_series, other, lags, method='pearson')

Compute correlation between a time_series and the lagged values of other time series.

Parameters:

Name Type Description Default
time_series pandas Series

Target time series.

required
other pandas DataFrame

Time series whose lagged values are correlated to time_series.

required
lags int, list, numpy ndarray

Lags to be included in the correlation analysis.

required
method str
  • 'pearson': standard correlation coefficient.
  • 'kendall': Kendall Tau correlation coefficient.
  • 'spearman': Spearman rank correlation.
'pearson'

Returns:

Name Type Description
corr pandas DataFrame

Correlation values.

Source code in skforecast\utils\utils.py
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
def multivariate_time_series_corr(
    time_series: pd.Series,
    other: pd.DataFrame,
    lags: Union[int, list, np.array],
    method: str='pearson'
)-> pd.DataFrame:
    """
    Compute correlation between a time_series and the lagged values of other 
    time series. 

    Parameters
    ----------
    time_series : pandas Series
        Target time series.
    other : pandas DataFrame
        Time series whose lagged values are correlated to `time_series`.
    lags : int, list, numpy ndarray
        Lags to be included in the correlation analysis.
    method : str, default 'pearson'
        - 'pearson': standard correlation coefficient.
        - 'kendall': Kendall Tau correlation coefficient.
        - 'spearman': Spearman rank correlation.

    Returns
    -------
    corr : pandas DataFrame
        Correlation values.

    """

    if not len(time_series) == len(other):
        raise ValueError("`time_series` and `other` must have the same length.")

    if not (time_series.index == other.index).all():
        raise ValueError("`time_series` and `other` must have the same index.")

    if isinstance(lags, int):
        lags = range(lags)

    corr = {}
    for col in other.columns:
        lag_values = {}
        for lag in lags:
            lag_values[lag] = other[col].shift(lag)

        lag_values = pd.DataFrame(lag_values)
        lag_values.insert(0, None, time_series)
        corr[col] = lag_values.corr(method=method).iloc[1:, 0]

    corr = pd.DataFrame(corr)
    corr.index = corr.index.astype('int64')
    corr.index.name = "lag"

    return corr

check_backtesting_input(forecaster, steps, metric, y=None, series=None, initial_train_size=None, fixed_train_size=True, gap=0, allow_incomplete_fold=True, refit=False, interval=None, alpha=None, n_boot=500, random_state=123, in_sample_residuals=True, n_jobs='auto', verbose=False, show_progress=True)

This is a helper function to check most inputs of backtesting functions in modules model_selection, model_selection_multiseries and model_selection_sarimax.

Parameters:

Name Type Description Default
forecaster object

Forecaster model.

required
steps int, list

Number of future steps predicted.

required
metric str, Callable, list

Metric used to quantify the goodness of fit of the model.

required
y pandas Series

Training time series for uni-series forecasters.

None
series pandas DataFrame

Training time series for multi-series forecasters.

None
initial_train_size int

Number of samples in the initial train split. If None and forecaster is already trained, no initial train is done and all data is used to evaluate the model.

`None`
fixed_train_size bool

If True, train size doesn't increase but moves by steps in each iteration.

`True`
gap int

Number of samples to be excluded after the end of each training set and before the test set.

`0`
allow_incomplete_fold bool

Last fold is allowed to have a smaller number of samples than the test_size. If False, the last fold is excluded.

`True`
refit bool, int

Whether to re-fit the forecaster in each iteration. If refit is an integer, the Forecaster will be trained every that number of iterations.

`False`
interval list

Confidence of the prediction interval estimated. Sequence of percentiles to compute, which must be between 0 and 100 inclusive.

`None`
alpha float

The confidence intervals used in ForecasterSarimax are (1 - alpha) %.

`None`
n_boot int

Number of bootstrapping iterations used to estimate prediction intervals.

`500`
random_state int

Sets a seed to the random generator, so that boot intervals are always deterministic.

`123`
in_sample_residuals bool

If True, residuals from the training data are used as proxy of prediction error to create prediction intervals. If False, out_sample_residuals are used if they are already stored inside the forecaster.

`True`
n_jobs int, auto

The number of jobs to run in parallel. If -1, then the number of jobs is set to the number of cores. If 'auto', n_jobs is set using the fuction skforecast.utils.select_n_jobs_fit_forecaster. New in version 0.9.0

`'auto'`
verbose bool

Print number of folds and index of training and validation sets used for backtesting.

`False`
show_progress bool

Whether to show a progress bar.

True

Returns:

Type Description
None
Source code in skforecast\utils\utils.py
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
def check_backtesting_input(
    forecaster: object,
    steps: int,
    metric: Union[str, Callable, list],
    y: Optional[pd.Series]=None,
    series: Optional[pd.DataFrame]=None,
    initial_train_size: Optional[int]=None,
    fixed_train_size: bool=True,
    gap: int=0,
    allow_incomplete_fold: bool=True,
    refit: Optional[Union[bool, int]]=False,
    interval: Optional[list]=None,
    alpha: Optional[float]=None,
    n_boot: int=500,
    random_state: int=123,
    in_sample_residuals: bool=True,
    n_jobs: Optional[Union[int, str]]='auto',
    verbose: bool=False,
    show_progress: bool=True
) -> None:
    """
    This is a helper function to check most inputs of backtesting functions in 
    modules `model_selection`, `model_selection_multiseries` and 
    `model_selection_sarimax`.

    Parameters
    ----------
    forecaster : object
        Forecaster model.
    steps : int, list
        Number of future steps predicted.
    metric : str, Callable, list
        Metric used to quantify the goodness of fit of the model.
    y : pandas Series
        Training time series for uni-series forecasters.
    series : pandas DataFrame
        Training time series for multi-series forecasters.
    initial_train_size : int, default `None`
        Number of samples in the initial train split. If `None` and `forecaster` 
        is already trained, no initial train is done and all data is used to 
        evaluate the model.
    fixed_train_size : bool, default `True`
        If True, train size doesn't increase but moves by `steps` in each iteration.
    gap : int, default `0`
        Number of samples to be excluded after the end of each training set and 
        before the test set.
    allow_incomplete_fold : bool, default `True`
        Last fold is allowed to have a smaller number of samples than the 
        `test_size`. If `False`, the last fold is excluded.
    refit : bool, int, default `False`
        Whether to re-fit the forecaster in each iteration. If `refit` is an integer, 
        the Forecaster will be trained every that number of iterations.
    interval : list, default `None`
        Confidence of the prediction interval estimated. Sequence of percentiles
        to compute, which must be between 0 and 100 inclusive.
    alpha : float, default `None`
        The confidence intervals used in ForecasterSarimax are (1 - alpha) %. 
    n_boot : int, default `500`
        Number of bootstrapping iterations used to estimate prediction
        intervals.
    random_state : int, default `123`
        Sets a seed to the random generator, so that boot intervals are always 
        deterministic.
    in_sample_residuals : bool, default `True`
        If `True`, residuals from the training data are used as proxy of prediction 
        error to create prediction intervals.  If `False`, out_sample_residuals 
        are used if they are already stored inside the forecaster.
    n_jobs : int, 'auto', default `'auto'`
            The number of jobs to run in parallel. If `-1`, then the number of jobs is 
            set to the number of cores. If 'auto', `n_jobs` is set using the fuction
            skforecast.utils.select_n_jobs_fit_forecaster.
            **New in version 0.9.0**
    verbose : bool, default `False`
        Print number of folds and index of training and validation sets used 
        for backtesting.
    show_progress: bool, default `True`
        Whether to show a progress bar.

    Returns
    -------
    None

    """

    forecasters_uni = ['ForecasterAutoreg', 'ForecasterAutoregCustom', 
                       'ForecasterAutoregDirect', 'ForecasterSarimax']
    forecasters_multi = ['ForecasterAutoregMultiSeries', 
                         'ForecasterAutoregMultiSeriesCustom', 
                         'ForecasterAutoregMultiVariate']

    if type(forecaster).__name__ in forecasters_uni:
        if not isinstance(y, pd.Series):
            raise TypeError("`y` must be a pandas Series.")
        data_name = 'y'
        data_length = len(y)

    if type(forecaster).__name__ in forecasters_multi:
        if not isinstance(series, pd.DataFrame):
            raise TypeError("`series` must be a pandas DataFrame.")
        data_name = 'series'
        data_length = len(series)

    if not isinstance(steps, (int, np.integer)) or steps < 1:
        raise TypeError(
            f"`steps` must be an integer greater than or equal to 1. Got {steps}."
        )
    if not isinstance(gap, (int, np.integer)) or gap < 0:
        raise TypeError(
            f"`gap` must be an integer greater than or equal to 0. Got {gap}."
        )
    if not isinstance(metric, (str, Callable, list)):
        raise TypeError(
            (f"`metric` must be a string, a callable function, or a list containing "
             f"multiple strings and/or callables. Got {type(metric)}.")
        )

    if initial_train_size is not None:
        if not isinstance(initial_train_size, (int, np.integer)):
            raise TypeError(
                (f"If used, `initial_train_size` must be an integer greater than the "
                 f"window_size of the forecaster. Got type {type(initial_train_size)}.")
            )
        if initial_train_size >= data_length:
            raise ValueError(
                (f"If used, `initial_train_size` must be an integer smaller "
                 f"than the length of `{data_name}` ({data_length}).")
            )    
        if initial_train_size < forecaster.window_size:
            raise ValueError(
                (f"If used, `initial_train_size` must be an integer greater than "
                 f"the window_size of the forecaster ({forecaster.window_size}).")
            )
        if initial_train_size + gap >= data_length:
            raise ValueError(
                (f"The combination of initial_train_size {initial_train_size} and "
                 f"gap {gap} cannot be greater than the length of `{data_name}` "
                 f"({data_length}).")
            )
        if data_name == 'series':
            for serie in series:
                if np.isnan(series[serie].to_numpy()[:initial_train_size]).all():
                    raise ValueError(
                        (f"All values of series '{serie}' are NaN. When working "
                         f"with series of different lengths, make sure that "
                         f"`initial_train_size` has an appropriate value so that "
                         f"all series reach the first non-null value.")
                    )
    else:
        if type(forecaster).__name__ == 'ForecasterSarimax':
            raise ValueError(
                (f"`initial_train_size` must be an integer smaller than the "
                 f"length of `{data_name}` ({data_length}).")
            )    
        else:
            if not forecaster.fitted:
                raise NotFittedError(
                    ("`forecaster` must be already trained if no `initial_train_size` "
                     "is provided.")
                )
            if refit:
                raise ValueError(
                    "`refit` is only allowed when `initial_train_size` is not `None`."
                )

    if not isinstance(fixed_train_size, bool):
        raise TypeError("`fixed_train_size` must be a boolean: `True`, `False`.")
    if not isinstance(allow_incomplete_fold, bool):
        raise TypeError("`allow_incomplete_fold` must be a boolean: `True`, `False`.")
    if not isinstance(refit, (bool, int, np.integer)) or refit < 0:
        raise TypeError(f"`refit` must be a boolean or an integer greater than 0. Got {refit}.")
    if not isinstance(n_boot, (int, np.integer)) or n_boot < 0:
        raise TypeError(f"`n_boot` must be an integer greater than 0. Got {n_boot}.")
    if not isinstance(random_state, (int, np.integer)) or random_state < 0:
        raise TypeError(f"`random_state` must be an integer greater than 0. Got {random_state}.")
    if not isinstance(in_sample_residuals, bool):
        raise TypeError("`in_sample_residuals` must be a boolean: `True`, `False`.")
    if not isinstance(n_jobs, int) and n_jobs != 'auto':
        raise TypeError(f"`n_jobs` must be an integer or `'auto'`. Got {n_jobs}.")
    if not isinstance(verbose, bool):
        raise TypeError("`verbose` must be a boolean: `True`, `False`.")
    if not isinstance(show_progress, bool):
        raise TypeError("`show_progress` must be a boolean: `True`, `False`.")

    if interval is not None or alpha is not None:
        check_interval(interval=interval, alpha=alpha)

    if not allow_incomplete_fold and data_length - (initial_train_size + gap) < steps:
        raise ValueError(
            (f"There is not enough data to evaluate {steps} steps in a single "
             f"fold. Set `allow_incomplete_fold` to `True` to allow incomplete folds.\n"
             f"    Data available for test : {data_length - (initial_train_size + gap)}\n"
             f"    Steps                   : {steps}")
        )

    return

select_n_jobs_backtesting(forecaster_name, regressor_name, refit)

Select the optimal number of jobs to use in the backtesting process. This selection is based on heuristics and is not guaranteed to be optimal.

The number of jobs is chosen as follows:

  • If refit is an integer, then n_jobs=1. This is because parallelization doesn't work with intermittent refit.
  • If forecaster_name is 'ForecasterAutoreg' or 'ForecasterAutoregCustom' and regressor_name is a linear regressor, then n_jobs=1.
  • If forecaster_name is 'ForecasterAutoreg' or 'ForecasterAutoregCustom', regressor_name is not a linear regressor and refit=True, then n_jobs=cpu_count().
  • If forecaster_name is 'ForecasterAutoreg' or 'ForecasterAutoregCustom', regressor_name is not a linear regressor and refit=False, then n_jobs=1.
  • If forecaster_name is 'ForecasterAutoregDirect' or 'ForecasterAutoregMultiVariate' and refit=True, then n_jobs=cpu_count().
  • If forecaster_name is 'ForecasterAutoregDirect' or 'ForecasterAutoregMultiVariate' and refit=False, then n_jobs=1.
  • If forecaster_name is 'ForecasterAutoregMultiseries', then n_jobs=cpu_count().

Parameters:

Name Type Description Default
forecaster_name str

The type of Forecaster.

required
regressor_name str

The type of regressor.

required
refit bool, int

If the forecaster is refitted during the backtesting process.

required

Returns:

Name Type Description
n_jobs int

The number of jobs to run in parallel.

Source code in skforecast\utils\utils.py
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
def select_n_jobs_backtesting(
    forecaster_name: str,
    regressor_name: str,
    refit: Union[bool, int]
) -> int:
    """
    Select the optimal number of jobs to use in the backtesting process. This
    selection is based on heuristics and is not guaranteed to be optimal.

    The number of jobs is chosen as follows:

    - If `refit` is an integer, then n_jobs=1. This is because parallelization doesn't 
    work with intermittent refit.
    - If forecaster_name is 'ForecasterAutoreg' or 'ForecasterAutoregCustom' and
    regressor_name is a linear regressor, then n_jobs=1.
    - If forecaster_name is 'ForecasterAutoreg' or 'ForecasterAutoregCustom',
    regressor_name is not a linear regressor and refit=`True`, then
    n_jobs=cpu_count().
    - If forecaster_name is 'ForecasterAutoreg' or 'ForecasterAutoregCustom',
    regressor_name is not a linear regressor and refit=`False`, then
    n_jobs=1.
    - If forecaster_name is 'ForecasterAutoregDirect' or 'ForecasterAutoregMultiVariate'
    and refit=`True`, then n_jobs=cpu_count().
    - If forecaster_name is 'ForecasterAutoregDirect' or 'ForecasterAutoregMultiVariate'
    and refit=`False`, then n_jobs=1.
    - If forecaster_name is 'ForecasterAutoregMultiseries', then n_jobs=cpu_count().

    Parameters
    ----------
    forecaster_name : str
        The type of Forecaster.
    regressor_name : str
        The type of regressor.
    refit : bool, int
        If the forecaster is refitted during the backtesting process.

    Returns
    -------
    n_jobs : int
        The number of jobs to run in parallel.

    """

    linear_regressors = [
        regressor_name
        for regressor_name in dir(sklearn.linear_model)
        if not regressor_name.startswith('_')
    ]

    refit = False if refit == 0 else refit
    if not isinstance(refit, bool) and refit != 1:
        n_jobs = 1
    else:
        if forecaster_name in ['ForecasterAutoreg', 'ForecasterAutoregCustom']:
            if regressor_name in linear_regressors:
                n_jobs = 1
            else:
                n_jobs = joblib.cpu_count() if refit else 1
        elif forecaster_name in ['ForecasterAutoregDirect', 'ForecasterAutoregMultiVariate']:
            n_jobs = 1
        elif forecaster_name in ['ForecasterAutoregMultiseries', 'ForecasterAutoregMultiSeriesCustom']:
            n_jobs = joblib.cpu_count()
        elif forecaster_name in ['ForecasterSarimax']:
            n_jobs = 1
        else:
            n_jobs = 1

    return n_jobs

select_n_jobs_fit_forecaster(forecaster_name, regressor_name)

Select the optimal number of jobs to use in the fitting process. This selection is based on heuristics and is not guaranteed to be optimal.

The number of jobs is chosen as follows:

  • If forecaster_name is 'ForecasterAutoregDirect' or 'ForecasterAutoregMultiVariate' and regressor_name is a linear regressor, then n_jobs=1, otherwise n_jobs=cpu_count().

Parameters:

Name Type Description Default
forecaster_name str

The type of Forecaster.

required
regressor_name str

The type of regressor.

required

Returns:

Name Type Description
n_jobs int

The number of jobs to run in parallel.

Source code in skforecast\utils\utils.py
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
def select_n_jobs_fit_forecaster(
    forecaster_name: str,
    regressor_name: str,
) -> int:
    """
    Select the optimal number of jobs to use in the fitting process. This
    selection is based on heuristics and is not guaranteed to be optimal. 

    The number of jobs is chosen as follows:

    - If forecaster_name is 'ForecasterAutoregDirect' or 'ForecasterAutoregMultiVariate'
    and regressor_name is a linear regressor, then n_jobs=1, otherwise n_jobs=cpu_count().

    Parameters
    ----------
    forecaster_name : str
        The type of Forecaster.
    regressor_name : str
        The type of regressor.

    Returns
    -------
    n_jobs : int
        The number of jobs to run in parallel.

    """

    linear_regressors = [
        regressor_name
        for regressor_name in dir(sklearn.linear_model)
        if not regressor_name.startswith('_')
    ]

    if forecaster_name in ['ForecasterAutoregDirect', 'ForecasterAutoregMultiVariate']:
        if regressor_name in linear_regressors:
            n_jobs = 1
        else:
            n_jobs = joblib.cpu_count()
    else:
        n_jobs = 1

    return n_jobs