ExpandingWindowSplitter¶
yohou.model_selection.split.ExpandingWindowSplitter
¶
Bases: BaseSplitter
Expanding window time series cross-validation splitter.
Provides train/test indices to split time series data samples that are observed at fixed time intervals, in train/test sets. In each split, test indices must be higher than before, and thus shuffling in cross validator is inappropriate.
The training set grows with each split (expanding window), meaning successive training sets are supersets of those that come before them. This is useful when more data generally leads to better models and when you want to simulate accumulating historical data over time.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
n_splits
|
int
|
Number of splits. Must be at least 2. |
3
|
max_train_size
|
int
|
Maximum size for a single training set. If None, all available training data is used. |
None
|
test_size
|
int
|
Used to limit the size of the test set. Defaults to
|
None
|
Examples¶
>>> import polars as pl
>>> from datetime import datetime, timedelta
>>> from yohou.model_selection import ExpandingWindowSplitter
>>>
>>> # Create time series
>>> time = [datetime(2020, 1, 1) + timedelta(days=i) for i in range(100)]
>>> y = pl.DataFrame({"time": time, "value": range(100)})
>>>
>>> # 3 splits with 10-day test windows
>>> splitter = ExpandingWindowSplitter(n_splits=3, test_size=10)
>>> splits = list(splitter.split(y))
>>> len(splits)
3
>>>
>>> # First split: train on [0:70], test on [70:80]
>>> train, test = splits[0]
>>> len(train), len(test)
(70, 10)
>>>
>>> # Second split: train on [0:80], test on [80:90] (training set grows)
>>> train, test = splits[1]
>>> len(train), len(test)
(80, 10)
>>>
Notes¶
- Training sets grow with each split (expanding window)
- Test sets do not overlap
- All data is used in temporal order
- For panel data, splits all groups together using row indices
See Also¶
SlidingWindowSplitter: Fixed-size rolling window splitter
Source Code¶
Show/Hide source
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 227 228 229 230 231 232 233 234 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 270 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 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 | |
Methods¶
split(y, X_actual=None)
¶
Generate indices to split time series data with expanding windows.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
y
|
DataFrame
|
Target time series used to generate train/test split indices.
Must have a |
required |
X_actual
|
DataFrame or None
|
Actual features. Not used for splitting but accepted for API consistency. |
None
|
Yields:
| Name | Type | Description |
|---|---|---|
train |
ndarray
|
Training set row indices for that split. |
test |
ndarray
|
Test set row indices for that split. |
Source Code¶
Show/Hide source
get_n_splits(y=None, X_actual=None)
¶
Return the number of cross-validation folds.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
y
|
DataFrame or None
|
Not used. Accepted for API consistency. |
None
|
X_actual
|
DataFrame or None
|
Not used. Accepted for API consistency. |
None
|
Returns¶
| Type | Description |
|---|---|
int
|
The number of cross-validation folds. |
Source Code¶
Show/Hide source
Tutorials¶
The following example notebooks use this component:
-
How to Tune Fourier Seasonality Terms
Data-Features
Explore how Fourier harmonic count affects seasonal fit quality, compare Fourier vs Pattern seasonality, and tune harmonics jointly with GridSearchCV.
-
How to Handle Short Series
Data-Features
Use Fourier seasonality, simple train/test splits, and panel pooling when individual series are too short for standard approaches.
-
Cross-Validation for Time Series
Evaluation-Search
Evaluate forecasters with cross_val_score, cross_validate, and cross_val_predict using temporal splitters.
-
How to Run Panel Cross-Validation
Panel-Data
Time series cross-validation on panel data with GridSearchCV, selective group observation, rewind operations, and groupwise performance comparison.