model_selection
¶
backtesting_forecaster(forecaster, y, steps, metric, initial_train_size=None, fixed_train_size=True, gap=0, allow_incomplete_fold=True, exog=None, refit=False, interval=None, n_boot=250, random_state=123, in_sample_residuals=True, binned_residuals=False, n_jobs='auto', verbose=False, show_progress=True)
¶
Backtesting of forecaster model.
- If
refit
isFalse
, the model will be trained only once using theinitial_train_size
first observations. - If
refit
isTrue
, the model is trained on each iteration, increasing the training set. - If
refit
is aninteger
, the model will be trained every that number of iterations. - If
forecaster
is already trained andinitial_train_size
isNone
, no initial train will be done and all data will be used to evaluate the model. However, the firstlen(forecaster.last_window)
observations are needed to create the initial predictors, so no predictions are calculated for them.
A copy of the original forecaster is created so that it is not modified during the process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
forecaster |
(ForecasterAutoreg, ForecasterAutoregCustom, ForecasterAutoregDirect)
|
Forecaster model. |
required |
y |
pandas Series
|
Training time series. |
required |
steps |
int
|
Number of steps to predict. |
required |
metric |
(str, Callable, list)
|
Metric used to quantify the goodness of fit of the model.
|
required |
initial_train_size |
int
|
Number of samples in the initial train split. If |
`None`
|
fixed_train_size |
bool
|
If True, train size doesn't increase but moves by |
`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
|
`True`
|
exog |
pandas Series, pandas DataFrame
|
Exogenous variable/s included as predictor/s. Must have the same
number of observations as |
`None`
|
refit |
(bool, int)
|
Whether to re-fit the forecaster in each iteration. If |
`False`
|
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 |
`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`
|
binned_residuals |
bool
|
|
`False`
|
n_jobs |
(int, auto)
|
The number of jobs to run in parallel. If |
`'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:
Name | Type | Description |
---|---|---|
metrics_value |
(float, list)
|
Value(s) of the metric(s). |
backtest_predictions |
pandas DataFrame
|
Value of predictions and their estimated interval if
|
Source code in skforecast\model_selection\model_selection.py
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 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 |
|
grid_search_forecaster(forecaster, y, param_grid, steps, metric, initial_train_size, fixed_train_size=True, gap=0, allow_incomplete_fold=True, exog=None, lags_grid=None, refit=False, return_best=True, n_jobs='auto', verbose=True, show_progress=True, output_file=None)
¶
Exhaustive search over specified parameter values for a Forecaster object. Validation is done using time series backtesting.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
forecaster |
(ForecasterAutoreg, ForecasterAutoregCustom, ForecasterAutoregDirect)
|
Forecaster model. |
required |
y |
pandas Series
|
Training time series. |
required |
param_grid |
dict
|
Dictionary with parameters names ( |
required |
steps |
int
|
Number of steps to predict. |
required |
metric |
(str, Callable, list)
|
Metric used to quantify the goodness of fit of the model.
|
required |
initial_train_size |
int
|
Number of samples in the initial train split. |
required |
fixed_train_size |
bool
|
If True, train size doesn't increase but moves by |
`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
|
`True`
|
exog |
pandas Series, pandas DataFrame
|
Exogenous variable/s included as predictor/s. Must have the same
number of observations as |
`None`
|
lags_grid |
(list, dict)
|
Lists of lags to try, containing int, lists, numpy ndarray, or range
objects. If |
`None`
|
refit |
(bool, int)
|
Whether to re-fit the forecaster in each iteration. If |
`False`
|
return_best |
bool
|
Refit the |
`True`
|
n_jobs |
(int, auto)
|
The number of jobs to run in parallel. If |
`'auto'`
|
verbose |
bool
|
Print number of folds used for cv or backtesting. |
`True`
|
show_progress |
bool
|
Whether to show a progress bar. |
`True`
|
output_file |
str
|
Specifies the filename or full path where the results should be saved.
The results will be saved in a tab-separated values (TSV) format. If
|
`None`
|
Returns:
Name | Type | Description |
---|---|---|
results |
pandas DataFrame
|
Results for each combination of parameters.
|
Source code in skforecast\model_selection\model_selection.py
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 804 805 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 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 |
|
random_search_forecaster(forecaster, y, param_distributions, steps, metric, initial_train_size, fixed_train_size=True, gap=0, allow_incomplete_fold=True, exog=None, lags_grid=None, refit=False, n_iter=10, random_state=123, return_best=True, n_jobs='auto', verbose=True, show_progress=True, output_file=None)
¶
Random search over specified parameter values or distributions for a Forecaster object. Validation is done using time series backtesting.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
forecaster |
(ForecasterAutoreg, ForecasterAutoregCustom, ForecasterAutoregDirect)
|
Forecaster model. |
required |
y |
pandas Series
|
Training time series. |
required |
param_distributions |
dict
|
Dictionary with parameters names ( |
required |
steps |
int
|
Number of steps to predict. |
required |
metric |
(str, Callable, list)
|
Metric used to quantify the goodness of fit of the model.
|
required |
initial_train_size |
int
|
Number of samples in the initial train split. |
required |
fixed_train_size |
bool
|
If True, train size doesn't increase but moves by |
`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
|
`True`
|
exog |
pandas Series, pandas DataFrame
|
Exogenous variable/s included as predictor/s. Must have the same
number of observations as |
`None`
|
lags_grid |
(list, dict)
|
Lists of lags to try, containing int, lists, numpy ndarray, or range
objects. If |
`None`
|
refit |
(bool, int)
|
Whether to re-fit the forecaster in each iteration. If |
`False`
|
n_iter |
int
|
Number of parameter settings that are sampled per lags configuration. n_iter trades off runtime vs quality of the solution. |
`10`
|
random_state |
int
|
Sets a seed to the random sampling for reproducible output. |
`123`
|
return_best |
bool
|
Refit the |
`True`
|
n_jobs |
(int, auto)
|
The number of jobs to run in parallel. If |
`'auto'`
|
verbose |
bool
|
Print number of folds used for cv or backtesting. |
`True`
|
show_progress |
bool
|
Whether to show a progress bar. |
`True`
|
output_file |
str
|
Specifies the filename or full path where the results should be saved.
The results will be saved in a tab-separated values (TSV) format. If
|
`None`
|
Returns:
Name | Type | Description |
---|---|---|
results |
pandas DataFrame
|
Results for each combination of parameters.
|
Source code in skforecast\model_selection\model_selection.py
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 929 930 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 974 975 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 |
|
bayesian_search_forecaster(forecaster, y, search_space, steps, metric, initial_train_size, fixed_train_size=True, gap=0, allow_incomplete_fold=True, exog=None, lags_grid='deprecated', refit=False, n_trials=10, random_state=123, return_best=True, n_jobs='auto', verbose=True, show_progress=True, output_file=None, engine='optuna', kwargs_create_study={}, kwargs_study_optimize={})
¶
Bayesian optimization for a Forecaster object using time series backtesting and optuna library.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
forecaster |
(ForecasterAutoreg, ForecasterAutoregCustom, ForecasterAutoregDirect)
|
Forecaster model. |
required |
y |
pandas Series
|
Training time series. |
required |
search_space |
Callable(optuna)
|
Function with argument |
required |
steps |
int
|
Number of steps to predict. |
required |
metric |
(str, Callable, list)
|
Metric used to quantify the goodness of fit of the model.
|
required |
initial_train_size |
int
|
Number of samples in the initial train split. |
required |
fixed_train_size |
bool
|
If True, train size doesn't increase but moves by |
`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
|
`True`
|
exog |
pandas Series, pandas DataFrame
|
Exogenous variable/s included as predictor/s. Must have the same
number of observations as |
`None`
|
lags_grid |
deprecated
|
Deprecated since version 0.12.0 and will be removed in 0.13.0. Use
|
'deprecated'
|
refit |
(bool, int)
|
Whether to re-fit the forecaster in each iteration. If |
`False`
|
n_trials |
int
|
Number of parameter settings that are sampled in each lag configuration. |
`10`
|
random_state |
int
|
Sets a seed to the sampling for reproducible output. When a new sampler
is passed in |
`123`
|
return_best |
bool
|
Refit the |
`True`
|
n_jobs |
(int, auto)
|
The number of jobs to run in parallel. If |
`'auto'`
|
verbose |
bool
|
Print number of folds used for cv or backtesting. |
`True`
|
show_progress |
bool
|
Whether to show a progress bar. |
`True`
|
output_file |
str
|
Specifies the filename or full path where the results should be saved.
The results will be saved in a tab-separated values (TSV) format. If
|
`None`
|
engine |
str
|
Bayesian optimization runs through the optuna library. |
`'optuna'`
|
kwargs_create_study |
dict
|
Keyword arguments (key, value mappings) to pass to optuna.create_study(). If default, the direction is set to 'minimize' and a TPESampler(seed=123) sampler is used during optimization. |
`{}`
|
kwargs_study_optimize |
dict
|
Only applies to engine='optuna'. Other keyword arguments (key, value mappings) to pass to study.optimize(). |
`{}`
|
Returns:
Name | Type | Description |
---|---|---|
results |
pandas DataFrame
|
Results for each combination of parameters.
|
best_trial |
optuna object
|
The best optimization result returned as a FrozenTrial optuna object. |
Source code in skforecast\model_selection\model_selection.py
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 1262 1263 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 1291 1292 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 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 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 1382 |
|
select_features(forecaster, selector, y, exog=None, select_only=None, force_inclusion=None, subsample=0.5, random_state=123, verbose=True)
¶
Feature selection using any of the sklearn.feature_selection module selectors
(such as RFECV
, SelectFromModel
, etc.). Two groups of features are
evaluated: autoregressive features and exogenous features. By default, the
selection process is performed on both sets of features at the same time,
so that the most relevant autoregressive and exogenous features are selected.
However, using the select_only
argument, the selection process can focus
only on the autoregressive or exogenous features without taking into account
the other features. Therefore, all other features will remain in the model.
It is also possible to force the inclusion of certain features in the final
list of selected features using the force_inclusion
parameter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
forecaster |
(ForecasterAutoreg, ForecasterAutoregCustom)
|
Forecaster model. |
required |
selector |
object
|
A feature selector from sklearn.feature_selection. |
required |
y |
pandas Series, pandas DataFrame
|
Target time series to which the feature selection will be applied. |
required |
exog |
pandas Series, pandas DataFrame
|
Exogenous variable/s included as predictor/s. Must have the same
number of observations as |
`None`
|
select_only |
str
|
Decide what type of features to include in the selection process.
|
`None`
|
force_inclusion |
(list, str)
|
Features to force include in the final list of selected features.
|
`None`
|
subsample |
(int, float)
|
Proportion of records to use for feature selection. |
`0.5`
|
random_state |
int
|
Sets a seed for the random subsample so that the subsampling process is always deterministic. |
`123`
|
verbose |
bool
|
Print information about feature selection process. |
`True`
|
Returns:
Name | Type | Description |
---|---|---|
selected_autoreg |
list
|
List of selected autoregressive features. |
selected_exog |
list
|
List of selected exogenous features. |
Source code in skforecast\model_selection\model_selection.py
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 1705 1706 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 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 |
|