Model Selection¶
sklego.model_selection.TimeGapSplit
¶
Provides train/test indices to split time series data samples.
This cross-validation object is a variation of TimeSeriesSplit with the following differences:
- The splits are made based on datetime duration, instead of number of rows.
- The user specifies the
valid_duration
and eithertrain_duration
orn_splits
. - The user can specify a
gap_duration
that is added after the training split and before the validation split.
The 3 duration parameters can be used to really replicate how the model is going to be used in production in batch learning.
Each validation fold doesn't overlap. The entire window
moves by 1 valid_duration
until there is not enough
data.
If this would lead to more splits then specified with n_splits
, the window
moves by valid_duration
times the
fraction of possible splits and requested splits:
n_possible_splits = (total_length - train_duration-gap_duration) // valid_duration
time_shift = valid_duration * n_possible_splits / n_slits
so the CV spans the whole dataset.
If train_duration
is not passed but n_splits
is, the training duration is increased to:
train_duration = total_length - (gap_duration + valid_duration * n_splits)
such that the shifting the entire window by one validation duration spans the whole training set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
date_serie
|
Series
|
Series with the date, that should have all the indices of X used in the split() method.
If the Series is not pandas-like (for example, if it's a Polars Series, which does not have
an index) then it must the same same length as the |
required |
valid_duration
|
timedelta
|
Retraining period. |
required |
train_duration
|
timedelta | None
|
Historical training data. |
None
|
gap_duration
|
timedelta
|
Forward looking window of the target. The period of the forward looking window necessary to create your target variable. This period is dropped at the end of your training folds due to lack of recent data. In production you would have not been able to create the target for that period, and you would have drop it from the training data. |
timedelta(0)
|
n_splits
|
int | None
|
Number of splits. |
None
|
window
|
Literal[rolling, expanding]
|
Type of moving window to use.
|
"rolling"
|
Notes
Native cross-dataframe support is achieved using Narwhals. Supported dataframes are:
- pandas
- Polars (eager)
- Modin
- cuDF
See Narwhals docs for an up-to-date list
(and to learn how you can add your dataframe library to it!), though note that only those
convertible to numpy
arrays will work with this class.
Source code in sklego/model_selection.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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 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 |
|
get_n_splits(X=None, y=None, groups=None)
¶
Get the number of splits
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
DataFrame
|
Dataframe with the data to split. |
None
|
y
|
array - like | None
|
Ignored, present for compatibility. |
None
|
groups
|
array - like | None
|
Ignored, present for compatibility. |
None
|
Returns:
Type | Description |
---|---|
int
|
Number of splits. |
Source code in sklego/model_selection.py
split(X, y=None, groups=None)
¶
Generate indices to split data into training and test set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
DataFrame
|
Dataframe with the data to split. |
required |
y
|
array - like | None
|
Ignored, present for compatibility. |
None
|
groups
|
array - like | None
|
Ignored, present for compatibility. |
None
|
Yields:
Type | Description |
---|---|
tuple[ndarray, ndarray]
|
Train and test indices of the same fold. |
Source code in sklego/model_selection.py
summary(X)
¶
Describe all folds.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
DataFrame
|
Dataframe with the data to split. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
Summary of all folds. |
Source code in sklego/model_selection.py
sklego.model_selection.GroupTimeSeriesSplit
¶
Bases: _BaseKFold
Sliding window time series split.
Create n_splits
folds with an as equally possible size through a smart variant of a brute force search.
Groups parameter in .split()
method should be filled with the time groups (e.g. years)
If n_splits
is 3 ("*" = train, "x" = test):
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_splits
|
int
|
Amount of (train, test) splits to generate. |
required |
Source code in sklego/model_selection.py
361 362 363 364 365 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 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 |
|
get_n_splits(X=None, y=None, groups=None)
¶
Get the amount of splits
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
array-like of shape (n_samples, n_features)
|
Ignored, present for compatibility. |
None
|
y
|
array-like of shape (n_samples,)
|
Ignored, present for compatibility. |
None
|
groups
|
array-like of shape (n_samples,)
|
Ignored, present for compatibility. |
None
|
Returns:
Type | Description |
---|---|
int
|
Amount of splits. |
Source code in sklego/model_selection.py
split(X=None, y=None, groups=None)
¶
Generate the train-test splits of all the folds
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
array-like of shape (n_samples, n_features)
|
Data to split. |
None
|
y
|
array-like of shape (n_samples,)
|
The target variable for supervised learning problems. |
None
|
groups
|
array-like of shape (n_samples,)
|
Group labels for the samples used while splitting the dataset into train/test set, |
None
|
Returns:
Type | Description |
---|---|
List[ndarray]
|
List containing train-test split indices of each fold. |
Source code in sklego/model_selection.py
summary()
¶
Describe all folds in a pd.DataFrame which displays the groups splits and extra statistics about it.
Can only be run after having applied the .split()
method to the GroupTimeSeriesSplit
instance.
Returns:
Type | Description |
---|---|
DataFrame
|
Summary of all folds. |
Source code in sklego/model_selection.py
sklego.model_selection.ClusterFoldValidation
¶
Cross validator that creates folds based on provided cluster method. This ensures that data points in the same cluster are not split across different folds.
New in version 0.8.2
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cluster_method
|
Clusterer
|
Clustering method to use for the fold validation. |
None
|
Source code in sklego/model_selection.py
split(X, y=None, groups=None)
¶
Generate indices to split data into training and test set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
array-like of shape (n_samples, n_features)
|
Array to split on. |
required |
y
|
array-like of shape (n_samples,) | None
|
Ignored, present for compatibility. |
None
|
groups
|
array-like of shape (n_samples,) | None
|
Ignored, present for compatibility. |
None
|
Yields:
Type | Description |
---|---|
tuple[ndarray, ndarray]
|
Train and test indices of the same fold. |
Source code in sklego/model_selection.py
KlusterFoldValidation
¶
Prior to version 0.8.2
, the ClusterFoldValidation
class was named KlusterFoldValidation
. The old name is deprecated and will be removed in a future releases.