preprocessing¶
skforecast.preprocessing.preprocessing.RollingFeatures ¶
RollingFeatures(
stats,
window_sizes,
min_periods=None,
features_names=None,
fillna=None,
kwargs_stats={"ewm": {"alpha": 0.3}},
)
This class computes rolling features. To avoid data leakage, the last point in the window is excluded from calculations, ('closed': 'left' and 'center': False).
Currently, the following statistics are supported: 'mean', 'std', 'min', 'max', 'sum', 'median', 'ratio_min_max', 'coef_variation', 'ewm'. For 'ewm', the alpha parameter can be set in the kwargs_stats dictionary, default is {'ewm': {'alpha': 0.3}}.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stats
|
(str, list)
|
Statistics to compute over the rolling window. Can be a |
required |
window_sizes
|
(int, list)
|
Size of the rolling window for each statistic. If an |
required |
min_periods
|
(int, list)
|
Minimum number of observations in window required to have a value.
Same as the |
None
|
features_names
|
list
|
Names of the output features. If |
None
|
fillna
|
(str, float)
|
Fill missing values in |
None
|
kwargs_stats
|
dict
|
Dictionary with additional arguments for the statistics. The keys are the statistic names and the values are dictionaries with the arguments for the corresponding statistic. For example, {'ewm': {'alpha': 0.3}}. |
{'ewm': {'alpha': 0.3}}
|
Attributes:
| Name | Type | Description |
|---|---|---|
stats |
list
|
Statistics to compute over the rolling window. |
n_stats |
int
|
Number of statistics to compute. |
window_sizes |
list
|
Size of the rolling window for each statistic. |
max_window_size |
int
|
Maximum window size. |
min_periods |
list
|
Minimum number of observations in window required to have a value. |
features_names |
list
|
Names of the output features. |
fillna |
(str, float)
|
Method to fill missing values in |
unique_rolling_windows |
dict
|
Dictionary containing unique rolling window parameters and the corresponding statistics. |
kwargs_stats |
dict
|
Dictionary with additional arguments for the statistics. |
Methods:
| Name | Description |
|---|---|
transform_batch |
Transform an entire pandas Series using rolling windows and compute the |
transform |
Transform a numpy array using rolling windows and compute the |
Source code in skforecast\preprocessing\preprocessing.py
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 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 | |
kwargs_stats
instance-attribute
¶
kwargs_stats = (
kwargs_stats if kwargs_stats is not None else {}
)
_repr_html_ ¶
_repr_html_()
HTML representation of the object. The "General Information" section is expanded by default.
Source code in skforecast\preprocessing\preprocessing.py
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 | |
_validate_params ¶
_validate_params(
stats,
window_sizes,
min_periods=None,
features_names=None,
fillna=None,
kwargs_stats=None,
)
Validate the parameters of the RollingFeatures class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stats
|
(str, list)
|
Statistics to compute over the rolling window. Can be a |
required |
window_sizes
|
(int, list)
|
Size of the rolling window for each statistic. If an |
required |
min_periods
|
(int, list)
|
Minimum number of observations in window required to have a value.
Same as the |
None
|
features_names
|
list
|
Names of the output features. If |
None
|
fillna
|
(str, float)
|
Fill missing values in |
None
|
kwargs_stats
|
dict
|
Dictionary with additional arguments for the statistics. The keys are the statistic names and the values are dictionaries with the arguments for the corresponding statistic. For example, {'ewm': {'alpha': 0.3}}. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in skforecast\preprocessing\preprocessing.py
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 1383 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 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 | |
_apply_stat_pandas ¶
_apply_stat_pandas(rolling_obj, stat)
Apply the specified statistic to a pandas rolling object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rolling_obj
|
pandas Rolling
|
Rolling object to apply the statistic. |
required |
stat
|
str
|
Statistic to compute. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
stat_series |
pandas Series
|
Series with the computed statistic. |
Source code in skforecast\preprocessing\preprocessing.py
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 | |
transform_batch ¶
transform_batch(X)
Transform an entire pandas Series using rolling windows and compute the specified statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
pandas Series
|
The input data series to transform. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
rolling_features |
pandas DataFrame
|
A DataFrame containing the rolling features. |
Source code in skforecast\preprocessing\preprocessing.py
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 | |
_apply_stat_numpy_jit ¶
_apply_stat_numpy_jit(X_window, stat)
Apply the specified statistic to a numpy array using Numba JIT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X_window
|
numpy array
|
Array with the rolling window. |
required |
stat
|
str
|
Statistic to compute. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
stat_value |
float
|
Value of the computed statistic. |
Source code in skforecast\preprocessing\preprocessing.py
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 | |
transform ¶
transform(X)
Transform a numpy array using rolling windows and compute the specified statistics. The returned array will have the shape (X.shape[1] if exists, n_stats). For example, if X is a flat array, the output will have shape (n_stats,). If X is a 2D array, the output will have shape (X.shape[1], n_stats).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
numpy ndarray
|
The input data array to transform. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
rolling_features |
numpy ndarray
|
An array containing the computed statistics. |
Source code in skforecast\preprocessing\preprocessing.py
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 1635 1636 1637 | |
skforecast.preprocessing.preprocessing.RollingFeaturesClassification ¶
RollingFeaturesClassification(
stats,
window_sizes,
min_periods=None,
features_names=None,
fillna=None,
)
This class computes rolling features for classification problems. To avoid data leakage, the last point in the window is excluded from calculations, ('closed': 'left' and 'center': False).
Currently, the following statistics are supported: 'proportion', 'mode', 'entropy', 'n_changes', 'n_unique'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stats
|
(str, list)
|
Statistics to compute over the rolling window. Can be a |
required |
window_sizes
|
(int, list)
|
Size of the rolling window for each statistic. If an |
required |
min_periods
|
(int, list)
|
Minimum number of observations in window required to have a value.
Same as the |
None
|
features_names
|
list
|
Names of the output features. If |
None
|
fillna
|
(str, float)
|
Fill missing values in |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
stats |
list
|
Statistics to compute over the rolling window. |
n_stats |
int
|
Number of statistics to compute. |
window_sizes |
list
|
Size of the rolling window for each statistic. |
max_window_size |
int
|
Maximum window size. |
min_periods |
list
|
Minimum number of observations in window required to have a value. |
classes |
list
|
Unique classes found in the data. Inferred during |
features_names |
list
|
Names of the output features. |
fillna |
(str, float)
|
Method to fill missing values in |
unique_rolling_windows |
dict
|
Dictionary containing unique rolling window parameters and the corresponding statistics. |
Methods:
| Name | Description |
|---|---|
transform_batch |
Transform an entire pandas Series using rolling windows and compute the |
transform |
Transform a numpy array using rolling windows and compute the |
Source code in skforecast\preprocessing\preprocessing.py
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 | |
_repr_html_ ¶
_repr_html_()
HTML representation of the object. The "General Information" section is expanded by default.
Source code in skforecast\preprocessing\preprocessing.py
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 | |
_validate_params ¶
_validate_params(
stats,
window_sizes,
min_periods=None,
features_names=None,
fillna=None,
)
Validate the parameters of the RollingFeaturesClassification class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stats
|
(str, list)
|
Statistics to compute over the rolling window. Can be a |
required |
window_sizes
|
(int, list)
|
Size of the rolling window for each statistic. If an |
required |
min_periods
|
(int, list)
|
Minimum number of observations in window required to have a value.
Same as the |
None
|
features_names
|
list
|
Names of the output features. If |
None
|
fillna
|
(str, float)
|
Fill missing values in |
None
|
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in skforecast\preprocessing\preprocessing.py
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 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 | |
_apply_stat_pandas ¶
_apply_stat_pandas(X, rolling_obj, stat)
Apply the specified statistic to a pandas rolling object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rolling_obj
|
pandas Rolling
|
Rolling object to apply the statistic. |
required |
stat
|
str
|
Statistic to compute. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
stat_series |
pandas Series
|
Series with the computed statistic. |
Source code in skforecast\preprocessing\preprocessing.py
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 | |
transform_batch ¶
transform_batch(X)
Transform an entire pandas Series using rolling windows and compute the specified statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
pandas Series
|
The input data series to transform. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
rolling_features |
pandas DataFrame
|
A DataFrame containing the rolling features. |
Source code in skforecast\preprocessing\preprocessing.py
1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 | |
_apply_stat_numpy_jit ¶
_apply_stat_numpy_jit(X_window, stat)
Apply the specified statistic to a numpy array using Numba JIT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X_window
|
numpy array
|
Array with the rolling window. |
required |
stat
|
str
|
Statistic to compute. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
stat_value |
float
|
Value of the computed statistic. |
Source code in skforecast\preprocessing\preprocessing.py
2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 | |
transform ¶
transform(X)
Transform a numpy array using rolling windows and compute the specified statistics. The returned array will have the shape (X.shape[1] if exists, n_stats). For example, if X is a flat array, the output will have shape (n_stats,). If X is a 2D array, the output will have shape (X.shape[1], n_stats).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
numpy ndarray
|
The input data array to transform. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
rolling_features |
numpy ndarray
|
An array containing the computed statistics. |
Source code in skforecast\preprocessing\preprocessing.py
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 | |
skforecast.preprocessing.preprocessing.reshape_series_wide_to_long ¶
reshape_series_wide_to_long(data, return_multi_index=True)
Convert a pandas DataFrame where each column represents a different time series
into a long format DataFrame with a MultiIndex. The index of the input DataFrame
must be a pandas DatetimeIndex with a defined frequency. The function reshapes the
DataFrame from wide format to long format, where each row corresponds to a
specific time point and series ID. The resulting DataFrame will have a MultiIndex
with the series IDs as the first level and a pandas DatetimeIndex as the second
level. If return_multi_index is set to False, the returned DataFrame have three
columns: 'series_id', 'datetime' and 'value', with a regular index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Wide format series. The index must be a pandas DatetimeIndex with a defined frequency and each column must represent a different time series. |
required |
return_multi_index
|
bool
|
If True, the returned DataFrame will have a MultiIndex with the series IDs as the first level and a pandas DatetimeIndex as the second level. If False, the returned DataFrame will have a regular index. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
data |
pandas DataFrame
|
Long format series with a MultiIndex. The first level contains the series IDs, and the second level contains a pandas DatetimeIndex with the same frequency for each series. |
Source code in skforecast\preprocessing\preprocessing.py
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 | |
skforecast.preprocessing.preprocessing.reshape_series_long_to_dict ¶
reshape_series_long_to_dict(
data,
freq,
series_id=None,
index=None,
values=None,
suppress_warnings=False,
)
Convert a long-format DataFrame into a dictionary of pandas Series with the specified frequency. Supports two input formats:
- A pandas DataFrame with explicit columns for the series identifier, time index, and values.
- A pandas DataFrame with a MultiIndex, where the first level contains the series IDs, and the second level contains a pandas DatetimeIndex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Long-format series. |
required |
freq
|
str
|
Frequency of the series. |
required |
series_id
|
str | None
|
Column name with the series identifier. Not needed if the input data is a pandas DataFrame with MultiIndex. |
None
|
index
|
str | None
|
Column name with the time index. Not needed if the input data is a pandas DataFrame with MultiIndex. |
None
|
values
|
str | None
|
Column name with the values. Not needed if the input data is a pandas DataFrame with MultiIndex. |
None
|
suppress_warnings
|
bool
|
If True, suppress warnings when a series is incomplete after setting the frequency. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
series_dict |
dict
|
Dictionary with the series. |
Source code in skforecast\preprocessing\preprocessing.py
438 439 440 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 | |
skforecast.preprocessing.preprocessing.reshape_exog_long_to_dict ¶
reshape_exog_long_to_dict(
data,
freq,
series_id=None,
index=None,
drop_all_nan_cols=False,
consolidate_dtypes=True,
suppress_warnings=False,
)
Convert a long-format DataFrame of exogenous variables into a dictionary of pandas DataFrames with the specified frequency. Supports two input formats:
- A pandas DataFrame with explicit columns for the series identifier, time index, and exogenous variables.
- A pandas DataFrame with a MultiIndex, where the first level contains the series IDs, and the second level contains a pandas DatetimeIndex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Long format exogenous variables. |
required |
freq
|
str
|
Frequency of the series. |
required |
series_id
|
str | None
|
Column name with the series identifier. Not needed if the input data is a pandas DataFrame with MultiIndex. |
None
|
index
|
str | None
|
Column name with the time index. Not needed if the input data is a pandas DataFrame with MultiIndex. |
None
|
drop_all_nan_cols
|
bool
|
If True, drop columns with all values as NaN. This is useful when there are series without some exogenous variables. |
False
|
consolidate_dtypes
|
bool
|
Consolidate the data types of the exogenous variables if, after setting the frequency, NaNs have been introduced and the data types have changed to float. |
True
|
suppress_warnings
|
bool
|
If True, suppress warnings when exog is incomplete after setting the frequency. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
exog_dict |
dict
|
Dictionary with the exogenous variables. |
Source code in skforecast\preprocessing\preprocessing.py
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 | |
skforecast.preprocessing.preprocessing.reshape_series_exog_dict_to_long ¶
reshape_series_exog_dict_to_long(
series,
exog,
series_col_name="series_value",
index_names=["series_id", "datetime"],
merge_how="left",
)
Convert dictionaries of series and exogenous variables to a long-format pandas DataFrame with MultiIndex. The first level of the MultiIndex contains the series identifiers, and the second level contains the temporal index. If both series and exog are provided, they are merged into a single DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series
|
dict[str, Series] | None
|
Dictionary with multiple time series (expected: dict[str, pd.Series]). |
required |
exog
|
dict[str, Series | DataFrame] | None
|
Dictionary with exogenous variables (expected: dict[str, pd.Series or pd.DataFrame]). |
required |
series_col_name
|
str
|
Column name for the series values in the resulting DataFrame. |
'series_value'
|
index_names
|
list[str]
|
Names for the levels of the MultiIndex in the resulting DataFrame. The first name corresponds to the series identifier, and the second name corresponds to the temporal index. |
['series_id', 'datetime']
|
merge_how
|
str
|
Type of merge to perform when combining
|
'left'
|
Returns:
| Name | Type | Description |
|---|---|---|
long_df |
DataFrame
|
Long-format DataFrame with a MultiIndex of two levels:
- First level: series identifier (named by |
Source code in skforecast\preprocessing\preprocessing.py
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 | |
skforecast.preprocessing.preprocessing.TimeSeriesDifferentiator ¶
TimeSeriesDifferentiator(order=1, window_size=None)
Bases: BaseEstimator, TransformerMixin
Transforms a time series into a differentiated time series of a specified order and provides functionality to revert the differentiation.
When using a direct module Forecaster, the model in step 1 must be
used if you want to reverse the differentiation of the training time
series with the inverse_transform_training method.
Parameters:
Attributes:
| Name | Type | Description |
|---|---|---|
order |
int
|
The order of differentiation. |
initial_values |
list
|
List with the first value of the time series before each differentiation.
If |
pre_train_values |
list
|
List with the first training value of the time series before each differentiation.
For |
last_values |
list
|
List with the last value of the time series before each differentiation,
used to revert differentiation on subsequent data windows. If |
Methods:
| Name | Description |
|---|---|
fit |
Fits the transformer. Stores the values needed to revert the |
transform |
Transforms a time series into a differentiated time series of order n. |
inverse_transform |
Reverts the differentiation. To do so, the input array is assumed to be |
inverse_transform_training |
Reverts the differentiation. To do so, the input array is assumed to be |
inverse_transform_next_window |
Reverts the differentiation. The input array |
set_params |
Set the parameters of the TimeSeriesDifferentiator. |
Source code in skforecast\preprocessing\preprocessing.py
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 | |
fit ¶
fit(X, y=None)
Fits the transformer. Stores the values needed to revert the differentiation of different window of the time series, original time series, training time series, and a time series that follows immediately after the series used to fit the transformer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
numpy ndarray
|
Time series to be differentiated. |
required |
y
|
Ignored
|
Not used, present here for API consistency by convention. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
TimeSeriesDifferentiator
|
|
Source code in skforecast\preprocessing\preprocessing.py
161 162 163 164 165 166 167 168 169 170 171 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 | |
transform ¶
transform(X, y=None)
Transforms a time series into a differentiated time series of order n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
numpy ndarray
|
Time series to be differentiated. |
required |
y
|
Ignored
|
Not used, present here for API consistency by convention. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
X_diff |
numpy ndarray
|
Differentiated time series. The length of the array is the same as
the original time series but the first n |
Source code in skforecast\preprocessing\preprocessing.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
inverse_transform ¶
inverse_transform(X, y=None)
Reverts the differentiation. To do so, the input array is assumed to be the same time series used to fit the transformer but differentiated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
numpy ndarray
|
Differentiated time series. |
required |
y
|
Ignored
|
Not used, present here for API consistency by convention. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
X_diff |
numpy ndarray
|
Reverted differentiated time series. |
Source code in skforecast\preprocessing\preprocessing.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
inverse_transform_training ¶
inverse_transform_training(X, y=None)
Reverts the differentiation. To do so, the input array is assumed to be the differentiated training time series generated with the original time series used to fit the transformer.
When using a direct module Forecaster, the model in step 1 must be
used if you want to reverse the differentiation of the training time
series with the inverse_transform_training method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
numpy ndarray
|
Differentiated time series. |
required |
y
|
Ignored
|
Not used, present here for API consistency by convention. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
X_diff |
numpy ndarray
|
Reverted differentiated time series. |
Source code in skforecast\preprocessing\preprocessing.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | |
inverse_transform_next_window ¶
inverse_transform_next_window(X, y=None)
Reverts the differentiation. The input array X is assumed to be a
differentiated time series of order n that starts right after the
the time series used to fit the transformer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
numpy ndarray
|
Differentiated time series. It is assumed o start right after the time series used to fit the transformer. |
required |
y
|
Ignored
|
Not used, present here for API consistency by convention. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
X_undiff |
numpy ndarray
|
Reverted differentiated time series. |
Source code in skforecast\preprocessing\preprocessing.py
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 | |
set_params ¶
set_params(**params)
Set the parameters of the TimeSeriesDifferentiator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
dict
|
A dictionary of the parameters to set. |
{}
|
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in skforecast\preprocessing\preprocessing.py
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
skforecast.preprocessing.preprocessing.QuantileBinner ¶
QuantileBinner(
n_bins,
method="linear",
subsample=200000,
dtype=np.float64,
random_state=789654,
)
QuantileBinner class to bin data into quantile-based bins using numpy.percentile.
This class is similar to KBinsDiscretizer but faster for binning data into
quantile-based bins. Bin intervals are defined following the convention:
bins[i-1] <= x < bins[i]. See more information in numpy.percentile and
numpy.digitize.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_bins
|
int
|
The number of quantile-based bins to create. |
required |
method
|
str
|
The method used to compute the quantiles. This parameter is passed to
|
'linear'
|
subsample
|
int
|
The number of samples to use for computing quantiles. If the dataset
has more samples than |
200000
|
dtype
|
data type
|
The data type to use for the bin indices. Default is |
numpy.float64
|
random_state
|
int
|
The random seed to use for generating a random subset of the data. |
789654
|
Attributes:
| Name | Type | Description |
|---|---|---|
n_bins |
int
|
The number of quantile-based bins to create. |
method |
str
|
The method used to compute the quantiles. This parameter is passed to
|
subsample |
int
|
The number of samples to use for computing quantiles. If the dataset
has more samples than |
dtype |
data type
|
The data type to use for the bin indices. Default is |
random_state |
int
|
The random seed to use for generating a random subset of the data. |
n_bins_ |
int
|
The number of bins learned during fitting. |
bin_edges_ |
numpy ndarray
|
The edges of the bins learned during fitting. |
intervals_ |
dict
|
A dictionary with the bin indices as keys and the corresponding bin intervals as values. |
Methods:
| Name | Description |
|---|---|
fit |
Learn the bin edges based on quantiles from the training data. |
transform |
Assign new data to the learned bins. |
fit_transform |
Fit the model to the data and return the bin indices for the same data. |
get_params |
Get the parameters of the quantile binner. |
set_params |
Set the parameters of the QuantileBinner. |
Source code in skforecast\preprocessing\preprocessing.py
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 | |
_validate_params ¶
_validate_params(
n_bins, method, subsample, dtype, random_state
)
Validate the parameters passed to the class initializer.
Source code in skforecast\preprocessing\preprocessing.py
2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 | |
fit ¶
fit(X)
Learn the bin edges based on quantiles from the training data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
numpy ndarray
|
The training data used to compute the quantiles. |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in skforecast\preprocessing\preprocessing.py
2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 | |
transform ¶
transform(X)
Assign new data to the learned bins.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
numpy ndarray
|
The data to assign to the bins. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bin_indices |
numpy ndarray
|
The indices of the bins each value belongs to. Values less than the smallest bin edge are assigned to the first bin, and values greater than the largest bin edge are assigned to the last bin. |
Source code in skforecast\preprocessing\preprocessing.py
2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 | |
fit_transform ¶
fit_transform(X)
Fit the model to the data and return the bin indices for the same data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
The data to fit and transform. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bin_indices |
ndarray
|
The indices of the bins each value belongs to. Values less than the smallest bin edge are assigned to the first bin, and values greater than the largest bin edge are assigned to the last bin. |
Source code in skforecast\preprocessing\preprocessing.py
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 | |
get_params ¶
get_params()
Get the parameters of the quantile binner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
self
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
params |
dict
|
A dictionary of the parameters of the quantile binner. |
Source code in skforecast\preprocessing\preprocessing.py
2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 | |
set_params ¶
set_params(**params)
Set the parameters of the QuantileBinner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
dict
|
A dictionary of the parameters to set. |
{}
|
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in skforecast\preprocessing\preprocessing.py
2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 | |
skforecast.preprocessing.preprocessing.ConformalIntervalCalibrator ¶
ConformalIntervalCalibrator(
nominal_coverage=0.8, symmetric_calibration=True
)
Transformer that calibrates the prediction interval to achieve the desired coverage based on conformity scores. It uses the conformal split method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nominal_coverage
|
float
|
Desired coverage. This is the desired probability that the true value falls within the calibrated interval. |
0.8
|
symmetric_calibration
|
bool
|
If True, the calibration factor is the same for the lower and upper bounds. If False, the calibration factor is different for the lower and upper bounds. |
True
|
Attributes:
| Name | Type | Description |
|---|---|---|
nominal_coverage |
float
|
Desired coverage. This is the desired probability that the true value falls within the calibrated interval. |
symmetric_calibration |
bool, default True
|
If True, the calibration factor is the same for the lower and upper bounds. If False, the calibration factor is different for the lower and upper bounds. |
correction_factor_ |
dict
|
Correction factor to achieve the desired coverage. This is the correction
factor used when |
correction_factor_lower_ |
dict
|
Correction factor for the lower bound to achieve the desired coverage. It is
used when |
correction_factor_upper_ |
dict
|
Correction factor for the upper bound to achieve the desired coverage. It is
used when |
fit_coverage_ |
dict
|
Coverage observed in the data used to fit the transformer. This is the empirical coverage from which the correction factor is learned. |
fit_input_type_ |
str
|
Type of input data used to fit the transformer. Can be 'single' or 'multi'. |
fit_series_names_ |
list
|
Names of the series used to fit the transformer. |
Methods:
Source code in skforecast\preprocessing\preprocessing.py
2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 | |
_repr_html_ ¶
_repr_html_()
HTML representation of the object. The "General Information" section is expanded by default.
Source code in skforecast\preprocessing\preprocessing.py
2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 | |
fit ¶
fit(y_true, y_pred_interval)
Learn the correction factor needed to achieve the desired coverage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
pandas Series, pandas DataFrame, dict
|
True values of the time series.
|
required |
y_pred_interval
|
pandas DataFrame
|
Prediction interval estimated for the time series.
|
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in skforecast\preprocessing\preprocessing.py
2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 | |
transform ¶
transform(y_pred_interval)
Apply the correction factor to the prediction interval to achieve the desired coverage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_pred_interval
|
pandas DataFrame
|
Prediction interval to be calibrated using conformal method.
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
y_pred_interval_conformal |
pandas DataFrame
|
Prediction interval with the correction factor applied. |
Source code in skforecast\preprocessing\preprocessing.py
2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 | |