Time Series cross-validator. Provides train/test indices to split time-ordered data, where other cross-validation methods are inappropriate, as they would lead to training on future data and evaluating on past data. To ensure comparable metrics across folds, samples must be equally
| 1114 | |
| 1115 | |
| 1116 | class TimeSeriesSplit(_BaseKFold): |
| 1117 | """Time Series cross-validator. |
| 1118 | |
| 1119 | Provides train/test indices to split time-ordered data, where other |
| 1120 | cross-validation methods are inappropriate, as they would lead to training |
| 1121 | on future data and evaluating on past data. |
| 1122 | To ensure comparable metrics across folds, samples must be equally spaced. |
| 1123 | Once this condition is met, each test set covers the same time duration, |
| 1124 | while the train set size accumulates data from previous splits. |
| 1125 | |
| 1126 | This cross-validation object is a variation of :class:`KFold`. |
| 1127 | In the k-th split, it returns the first k folds as the train set and the |
| 1128 | (k+1)-th fold as the test set. |
| 1129 | |
| 1130 | Note that, unlike standard cross-validation methods, successive |
| 1131 | training sets are supersets of those that come before them. |
| 1132 | |
| 1133 | Read more in the :ref:`User Guide <time_series_split>`. |
| 1134 | |
| 1135 | For visualisation of cross-validation behaviour and |
| 1136 | comparison between common scikit-learn split methods |
| 1137 | refer to :ref:`sphx_glr_auto_examples_model_selection_plot_cv_indices.py` |
| 1138 | |
| 1139 | .. versionadded:: 0.18 |
| 1140 | |
| 1141 | Parameters |
| 1142 | ---------- |
| 1143 | n_splits : int, default=5 |
| 1144 | Number of splits. Must be at least 2. |
| 1145 | |
| 1146 | .. versionchanged:: 0.22 |
| 1147 | ``n_splits`` default value changed from 3 to 5. |
| 1148 | |
| 1149 | max_train_size : int, default=None |
| 1150 | Maximum size for a single training set. |
| 1151 | |
| 1152 | test_size : int, default=None |
| 1153 | Used to limit the size of the test set. Defaults to |
| 1154 | ``n_samples // (n_splits + 1)``, which is the maximum allowed value |
| 1155 | with ``gap=0``. |
| 1156 | |
| 1157 | .. versionadded:: 0.24 |
| 1158 | |
| 1159 | gap : int, default=0 |
| 1160 | Number of samples to exclude from the end of each train set before |
| 1161 | the test set. |
| 1162 | |
| 1163 | .. versionadded:: 0.24 |
| 1164 | |
| 1165 | Examples |
| 1166 | -------- |
| 1167 | >>> import numpy as np |
| 1168 | >>> from sklearn.model_selection import TimeSeriesSplit |
| 1169 | >>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4], [1, 2], [3, 4]]) |
| 1170 | >>> y = np.array([1, 2, 3, 4, 5, 6]) |
| 1171 | >>> tscv = TimeSeriesSplit() |
| 1172 | >>> print(tscv) |
| 1173 | TimeSeriesSplit(gap=0, max_train_size=None, n_splits=5, test_size=None) |
no outgoing calls
searching dependent graphs…