SlidingWindowSplitter¶
yohou.model_selection.split.SlidingWindowSplitter
¶
Bases: BaseSplitter
Sliding window time series cross-validation splitter.
Both training and test windows slide forward with fixed sizes. This is useful when recent data is more relevant than distant past (concept drift), or when simulating production scenarios with fixed-size training windows.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
n_splits
|
int
|
Number of cross-validation folds. Must be at least 2. |
3
|
train_size
|
int
|
Number of samples in each training window. When |
None
|
test_size
|
int
|
Number of samples in each test window. |
10
|
stride
|
int
|
Number of samples to move forward between splits. If None,
defaults to |
None
|
Attributes¶
| Name | Type | Description |
|---|---|---|
train_size_ |
int
|
Resolved training window size, set during |
Examples¶
>>> import polars as pl
>>> from datetime import datetime, timedelta
>>> from yohou.model_selection import SlidingWindowSplitter
>>>
>>> # Create time series
>>> time = [datetime(2020, 1, 1) + timedelta(days=i) for i in range(100)]
>>> y = pl.DataFrame({"time": time, "value": range(100)})
>>>
>>> # 5-fold sliding window with 10-day test windows
>>> splitter = SlidingWindowSplitter(n_splits=5, test_size=10)
>>> splits = list(splitter.split(y))
>>> len(splits)
5
>>>
>>> # First split: training size computed automatically
>>> train, test = splits[0]
>>> len(test)
10
>>>
>>> # All training windows have the same size
>>> all(len(tr) == len(splits[0][0]) for tr, _ in splits)
True
Notes¶
- Training and test windows have fixed sizes
- Windows slide forward by
stridesamples - Useful for concept drift scenarios
- When
train_sizeis omitted, it is computed fromn_splitsand the data length in the first call tosplit()
See Also¶
ExpandingWindowSplitter: Growing training window splitter
Source Code¶
Show/Hide source
366 367 368 369 370 371 372 373 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 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 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 | |
Methods¶
split(y, X_actual=None)
¶
Generate indices to split time series data with sliding 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. |