Skip to content

FEA Group aware Time-based cross validation - #16236

Open
getgaurav2 wants to merge 11 commits into
scikit-learn:mainfrom
getgaurav2:issue_14257
Open

getgaurav2 wants to merge 11 commits into
scikit-learn:mainfrom
getgaurav2:issue_14257

Conversation

@getgaurav2

@getgaurav2 getgaurav2 commented Jan 26, 2020 •

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Fixes #14257. Fixes #19072.
See also #34852, which adds a documentation example of a custom splitter also named GroupTimeSeriesSplit.

What does this implement/fix? Explain your changes.

Adds GroupTimeSeriesSplit, a time-series cross-validator that never splits a group between training and test sets (for example, all rows of the same day stay together).

  • Groups must be contiguous; otherwise split raises an error.
  • TimeSeriesSplit is applied to the ordered list of groups, and the result is mapped back to sample indices.
  • Same parameters and defaults as TimeSeriesSplit (n_splits, max_train_size, test_size, gap), but they count whole groups instead of samples, as GroupShuffleSplit.test_size does. With one sample per group, the splits are identical to TimeSeriesSplit (tested).
  • Requests groups through metadata routing, like GroupKFold, so it works in nested cross-validation.
  • Trade-off: test sets contain the same number of groups, so they can contain different numbers of samples when group sizes differ. This is documented in the docstring.

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

This pull request includes code written with the assistance of AI.
The code has not yet been reviewed by a human.

Any other comments?

Still to do: the user guide section in doc/modules/cross_validation.rst and the API reference entry.
Open question from @glemaitre: an option to balance samples, rather than groups, across test sets could be a follow-up.

@jnothman

Copy link
Copy Markdown
Member

@getgaurav2, tests may be failing. Let us know when you want review.

@jnothman

Copy link
Copy Markdown
Member

Linting is failing, actually
.

@getgaurav2

Copy link
Copy Markdown
Contributor Author

@getgaurav2, tests may be failing. Let us know when you want review.

Sure @jnothman . Will do . Thank you .

@getgaurav2

getgaurav2 commented Mar 3, 2020 •

Copy link
Copy Markdown
Contributor Author

@jnothman @ogrisel Could you please opine on the approach that I have taken for this feature ?
I can work on performance metrics , documentation etc. if you are ok with the general direction .
Thanks

@getgaurav2

Copy link
Copy Markdown
Contributor Author

@jnothman @ogrisel Could you please opine on the approach that I have taken for this feature ? ...

@getgaurav2 - gentle reminder @ogrisel

Sent with GitHawk

@getgaurav2

Copy link
Copy Markdown
Contributor Author

...

@ogrisel @jnothman - can you pls review this PR ? Thank you

Sent with GitHawk

Comment thread sklearn/model_selection/_split.py Resolved
@albertvillanova

Copy link
Copy Markdown
Contributor

@getgaurav2 are you still working on this PR?

@getgaurav2

Copy link
Copy Markdown
Contributor Author

@getgaurav2 are you still working on this PR?

@albertvillanova - yes . I would like to finish this please. Have been busy at work lately . Will find some time this week to make some progress . Thanks

Sent with GitHawk

@albertvillanova

Copy link
Copy Markdown
Contributor

@getgaurav2 Great! Because I do really need this feature in the next release ;)

@PimwipaV

PimwipaV commented Sep 5, 2020

Copy link
Copy Markdown

hi @getgaurav2, I tried pushing here but I got an error remote: Permission to getgaurav2/scikit-learn.git denied to Pimpwhippa. My git remote -v are correct I think. Please could you check if you allow edits to this PR. Thank you.

@albertvillanova

Copy link
Copy Markdown
Contributor

@getgaurav2, as you have seen, it is not a good idea to rebase onto master during a Pull Request. If you would like to sync branches, it is better you make a merge:

git merge upstream master

Anyway, it does not seem necessary to do it: see GitHub message: This branch has no conflicts with the base branch.

@getgaurav2

getgaurav2 commented Sep 6, 2020 •

Copy link
Copy Markdown
Contributor Author

hi @getgaurav2, I tried pushing here but I got an error remote: Permission to getgaurav2/scikit-learn.git denied to Pimpwhippa. My git remote -v are correct I think. Please could you check if you allow edits to this PR. Thank you.

Screen Shot 2020-09-06 at 5 33 17 PM

Let's see if this helps. Please add test cases into test_split.py

@getgaurav2

Copy link
Copy Markdown
Contributor Author

@getgaurav2 Great! Because I do really need this feature in the next release ;)

@albertvillanova - would you want to check the latest code base ?
I want to talk through the best way to handle cases when the function may yield empty training splits .

Sent with GitHawk

@PimwipaV

PimwipaV commented Sep 7, 2020

Copy link
Copy Markdown

@getgaurav2 thank you. I just opened a PR.

@albertvillanova

Copy link
Copy Markdown
Contributor

Fast check: why this empty file? sklearn/model_selection/conftest.py

@albertvillanova

albertvillanova commented Sep 8, 2020 •

Copy link
Copy Markdown
Contributor

I would like to point out an issue in your implementation. I have created a new test case:

groups = np.array(['a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'd', 'd', 'd'])
gtss = GroupTimeSeriesSplit(n_splits=3)

and this is what I get:

In [49]: for train_idx, test_idx in gtss.split(groups, groups=groups):
    ...:     print(train_idx, test_idx)
    ...:     print(groups[train_idx], groups[test_idx])
    ...:     print()
    ...:
[0 1 2 3 4 5] [6 7 8 9]
['a' 'a' 'a' 'a' 'a' 'a'] ['b' 'b' 'b' 'b']

[0 1 2 3 4 5] [10 11 12 13]
['a' 'a' 'a' 'a' 'a' 'a'] ['b' 'c' 'c' 'c']

[ 0  1  2  3  4  5  6  7  8  9 10] [14 15 16 17]
['a' 'a' 'a' 'a' 'a' 'a' 'b' 'b' 'b' 'b' 'b'] ['c' 'd' 'd' 'd']

As you can see, there are gaps between the train and the test indices, i.e. they are not contiguous.

I would expect the following result:

In [49]: for train_idx, test_idx in gtss.split(groups, groups=groups):
    ...:     print(train_idx, test_idx)
    ...:     print(groups[train_idx], groups[test_idx])
    ...:     print()
    ...:
[0 1 2 3 4 5] [6 7 8 9 10]
['a' 'a' 'a' 'a' 'a' 'a'] ['b' 'b' 'b' 'b' 'b']

[0 1 2 3 4 5 6 7 8 9 10] [11 12 13 14]
['a' 'a' 'a' 'a' 'a' 'a' 'b' 'b' 'b' 'b' 'b'] ['c' 'c' 'c' 'c']

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14] [15 16 17]
['a' 'a' 'a' 'a' 'a' 'a' 'b' 'b' 'b' 'b' 'b' 'c' 'c' 'c' 'c'] ['d' 'd' 'd']

By the way, and concerning the gaps, if you look at the next v0.24 release version of TimeSeriesSplit, there are two new parameters: test_size and gap. Maybe these should be also implemented in GroupTimeSeriesSplit.

@PimwipaV

PimwipaV commented Sep 8, 2020

Copy link
Copy Markdown

'In each split, test indices must be higher than before, and thus shuffling in cross validator is inappropriate'.
You made the class description clearer already but does that mean i can still shuffle the groups, but not the time right?

@PimwipaV

PimwipaV commented Sep 8, 2020

Copy link
Copy Markdown

i mean this is allowed
groups = ['B', 'D', 'D', 'C', 'C', 'A', 'B', 'A']
and not only this
groups = ['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D']

@getgaurav2

Copy link
Copy Markdown
Contributor Author

I would like to point out an issue in your implementation. I have created a new test case:

groups = np.array(['a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'd', 'd', 'd'])
gtss = GroupTimeSeriesSplit(n_splits=3)

and this is what I get:

In [49]: for train_idx, test_idx in gtss.split(groups, groups=groups):
    ...:     print(train_idx, test_idx)
    ...:     print(groups[train_idx], groups[test_idx])
    ...:     print()
    ...:
[0 1 2 3 4 5] [6 7 8 9]
['a' 'a' 'a' 'a' 'a' 'a'] ['b' 'b' 'b' 'b']

[0 1 2 3 4 5] [10 11 12 13]
['a' 'a' 'a' 'a' 'a' 'a'] ['b' 'c' 'c' 'c']

[ 0  1  2  3  4  5  6  7  8  9 10] [14 15 16 17]
['a' 'a' 'a' 'a' 'a' 'a' 'b' 'b' 'b' 'b' 'b'] ['c' 'd' 'd' 'd']

As you can see, there are gaps between the train and the test indices, i.e. they are not contiguous.

I would expect the following result:

In [49]: for train_idx, test_idx in gtss.split(groups, groups=groups):
    ...:     print(train_idx, test_idx)
    ...:     print(groups[train_idx], groups[test_idx])
    ...:     print()
    ...:
[0 1 2 3 4 5] [6 7 8 9 10]
['a' 'a' 'a' 'a' 'a' 'a'] ['b' 'b' 'b' 'b' 'b']

[0 1 2 3 4 5 6 7 8 9 10] [11 12 13 14]
['a' 'a' 'a' 'a' 'a' 'a' 'b' 'b' 'b' 'b' 'b'] ['c' 'c' 'c' 'c']

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14] [15 16 17]
['a' 'a' 'a' 'a' 'a' 'a' 'b' 'b' 'b' 'b' 'b' 'c' 'c' 'c' 'c'] ['d' 'd' 'd']

By the way, and concerning the gaps, if you look at the next v0.24 release version of TimeSeriesSplit, there are two new parameters: test_size and gap. Maybe these should be also implemented in GroupTimeSeriesSplit.

@albertvillanova - Thanks for your review. I have incorporated your feedback in the latest Commit . Can you please check again .

@albertvillanova

Copy link
Copy Markdown
Contributor

@getgaurav2, thanks for your contribution. Your implementation now gives the expected result in the case I pointed out above.

However, when using the new parameters (gap, test_size) I think I have found some unexpected behavior (that I am commenting below), besides that they are not documented in the docstring. Moreover, I think there are some peculiarities when dealing with groups that might deserve a discussion to decide the expected behavior, the definition of the corresponding API and its implementation. For example, I am thinking of test_size, which in TimeSeriesSplit corresponds to the number of samples in the test set; however, when dealing with groups, maybe it would be more useful to be able to specify the number of groups in the test set instead.

Maybe, I would suggest, forgetting about this parameters for the moment, so that this PR can already be finished. You could eventually open a new PR to implement those parameters. What do you think?

The unexpected behavior for the parameter gap. Current behavior:

In [10]: gtss = GroupTimeSeriesSplit(n_splits=2, test_size=2, gap=1)

In [11]: for train_idx, test_idx in gtss.split(groups, groups=groups):
    ...:     print(train_idx, test_idx)
    ...:     print(groups[train_idx], groups[test_idx])
    ...:     print()
    ...:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [11, 12]
['a' 'a' 'a' 'a' 'a' 'a' 'b' 'b' 'b' 'b' 'b'] ['c' 'c']

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] [15, 16]
['a' 'a' 'a' 'a' 'a' 'a' 'b' 'b' 'b' 'b' 'b' 'c' 'c' 'c' 'c'] ['d' 'd']

As you can see, there is no gap between training and test sets.

The unexpected behavior for the parameter test_size. Current behavior:

In [8]: gtss = GroupTimeSeriesSplit(n_splits=2, test_size=5)

In [9]: for train_idx, test_idx in gtss.split(groups, groups=groups):
   ...:     print(train_idx, test_idx)
   ...:     print(groups[train_idx], groups[test_idx])
   ...:     print()
   ...:
scikit-learn\sklearn\model_selection\_split.py:2375: UserWarning: The size=4 of group=c is smaller than test_size=5.
  warnings.warn(("The size=%d of group=%s is smaller"
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [11, 12, 13, 14]
['a' 'a' 'a' 'a' 'a' 'a' 'b' 'b' 'b' 'b' 'b'] ['c' 'c' 'c' 'c']

scikit-learn\sklearn\model_selection\_split.py:2375: UserWarning: The size=3 of group=d is smaller than test_size=5.
  warnings.warn(("The size=%d of group=%s is smaller"
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] [15, 16, 17]
['a' 'a' 'a' 'a' 'a' 'a' 'b' 'b' 'b' 'b' 'b' 'c' 'c' 'c' 'c'] ['d' 'd' 'd']

In this case, I think the result should be:

[0 1 2 3 4 5] [6 7 8 9 10]
['a' 'a' 'a' 'a' 'a' 'a'] ['b' 'b' 'b' 'b' 'b']

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [11, 12, 13, 14, 15]
['a' 'a' 'a' 'a' 'a' 'a' 'b' 'b' 'b' 'b' 'b'] ['c' 'c' 'c' 'c', 'd']

@albertvillanova

Copy link
Copy Markdown
Contributor

Maybe I would also add the test case I pointed out above:

In [47]: groups = np.array(['a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'd', 'd', 'd'])
In [48]: gtss = GroupTimeSeriesSplit(n_splits=3)

In [49]: for train_idx, test_idx in gtss.split(groups, groups=groups):
    ...:     print(train_idx, test_idx)
    ...:     print(groups[train_idx], groups[test_idx])
    ...:     print()
    ...:
[0 1 2 3 4 5] [6 7 8 9 10]
['a' 'a' 'a' 'a' 'a' 'a'] ['b' 'b' 'b' 'b' 'b']

[0 1 2 3 4 5 6 7 8 9 10] [11 12 13 14]
['a' 'a' 'a' 'a' 'a' 'a' 'b' 'b' 'b' 'b' 'b'] ['c' 'c' 'c' 'c']

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14] [15 16 17]
['a' 'a' 'a' 'a' 'a' 'a' 'b' 'b' 'b' 'b' 'b' 'c' 'c' 'c' 'c'] ['d' 'd' 'd']

Comment thread sklearn/model_selection/_split.py Resolved
Comment thread sklearn/model_selection/_split.py Resolved
Comment thread sklearn/model_selection/_split.py Resolved
@getgaurav2

getgaurav2 commented Oct 5, 2020 •

Copy link
Copy Markdown
Contributor Author

@getgaurav2, thanks for your contribution. Your implementation now gives the expected result in the case I pointed out above.

However, when using the new parameters (gap, test_size) I think I have found some unexpected behavior (that I am commenting below), besides that they are not documented in the docstring. Moreover, I think there are some peculiarities when dealing with groups that might deserve a discussion to decide the expected behavior, the definition of the corresponding API and its implementation. For example, I am thinking of test_size, which in TimeSeriesSplit corresponds to the number of samples in the test set; however, when dealing with groups, maybe it would be more useful to be able to specify the number of groups in the test set instead.

Maybe, I would suggest, forgetting about this parameters for the moment, so that this PR can already be finished. You could eventually open a new PR to implement those parameters. What do you think?

The unexpected behavior for the parameter gap. Current behavior:

In [10]: gtss = GroupTimeSeriesSplit(n_splits=2, test_size=2, gap=1)

In [11]: for train_idx, test_idx in gtss.split(groups, groups=groups):
    ...:     print(train_idx, test_idx)
    ...:     print(groups[train_idx], groups[test_idx])
    ...:     print()
    ...:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [11, 12]
['a' 'a' 'a' 'a' 'a' 'a' 'b' 'b' 'b' 'b' 'b'] ['c' 'c']

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] [15, 16]
['a' 'a' 'a' 'a' 'a' 'a' 'b' 'b' 'b' 'b' 'b' 'c' 'c' 'c' 'c'] ['d' 'd']

As you can see, there is no gap between training and test sets.

The unexpected behavior for the parameter test_size. Current behavior:

In [8]: gtss = GroupTimeSeriesSplit(n_splits=2, test_size=5)

In [9]: for train_idx, test_idx in gtss.split(groups, groups=groups):
   ...:     print(train_idx, test_idx)
   ...:     print(groups[train_idx], groups[test_idx])
   ...:     print()
   ...:
scikit-learn\sklearn\model_selection\_split.py:2375: UserWarning: The size=4 of group=c is smaller than test_size=5.
  warnings.warn(("The size=%d of group=%s is smaller"
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [11, 12, 13, 14]
['a' 'a' 'a' 'a' 'a' 'a' 'b' 'b' 'b' 'b' 'b'] ['c' 'c' 'c' 'c']

scikit-learn\sklearn\model_selection\_split.py:2375: UserWarning: The size=3 of group=d is smaller than test_size=5.
  warnings.warn(("The size=%d of group=%s is smaller"
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] [15, 16, 17]
['a' 'a' 'a' 'a' 'a' 'a' 'b' 'b' 'b' 'b' 'b' 'c' 'c' 'c' 'c'] ['d' 'd' 'd']

In this case, I think the result should be:

[0 1 2 3 4 5] [6 7 8 9 10]
['a' 'a' 'a' 'a' 'a' 'a'] ['b' 'b' 'b' 'b' 'b']

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [11, 12, 13, 14, 15]
['a' 'a' 'a' 'a' 'a' 'a' 'b' 'b' 'b' 'b' 'b'] ['c' 'c' 'c' 'c', 'd']

@albertvillanova - Thanks for your feedback. Can you please review the latest commit .

  • For the test_size = 5 , the expected output that you have put is certainly more desirable . It will be interesting to determine how the 2nd split shall behave when there is conflict between gap and test_size like :
groups = np.array(['a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'd', 'd', 'd', 'd', 'd'])
gtss = GroupTimeSeriesSplit(n_splits=2, test_size=5)

> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [11, 12, 13, 14, 15]
> ['a' 'a' 'a' 'a' 'a' 'a' 'b' 'b' 'b' 'b' 'b'] ['c' 'c' 'c' 'c', 'd']
OR 
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [15, 16, 17, 18, 19]
> ['a' 'a' 'a' 'a' 'a' 'a' 'b' 'b' 'b' 'b' 'b'] ['d' 'd' 'd' 'd', 'd']
  • Would you recommend that I open another PR here or raise a new feature request to keep things separate ?

@albertvillanova

Copy link
Copy Markdown
Contributor

@getgaurav2, as you have already done an amazing amount of work here (thank you), my suggestion would be to leave for another PR the implementation of the parameters test_size and gap. That way, you would already be able to finish this PR.

Just a minor addition: could you please add GroupTimeSeriesSplit to the test test_yields_constant_splits?

And please, add a whatsnew entry to the file v0.24.rst, in the model_selection section, as a |MajorFeature|, and reference this PR and your name and link. You can take the other entries as example.

@getgaurav2

Copy link
Copy Markdown
Contributor Author

@getgaurav2, as you have already done an amazing amount of work here (thank you), my suggestion would be to leave for another PR the implementation of the parameters test_size and gap. That way, you would already be able to finish this PR.

Just a minor addition: could you please add GroupTimeSeriesSplit to the test test_yields_constant_splits?

And please, add a whatsnew entry to the file v0.24.rst, in the model_selection section, as a |MajorFeature|, and reference this PR and your name and link. You can take the other entries as example.

@albertvillanova - Thank you . Can you please check now .

@albertvillanova

albertvillanova commented Oct 7, 2020 •

Copy link
Copy Markdown
Contributor

@getgaurav2 thanks. I think your PR is ready for a review round. Could you please change its name from WIP to MRG?

@getgaurav2

getgaurav2 commented Jan 21, 2025 •

Copy link
Copy Markdown
Contributor Author

@getgaurav2 that'd be fantastic!

@adrinjalali - would you please be able to check the issue in doc build to and give some direction ....thank you !

@adrinjalali

Copy link
Copy Markdown
Member

Something has gone wrong here probably in your merge with main. You have over 12k line change, and some large changes in the rst files. I'd start with fixing those issues if I were you.

@getgaurav2

Copy link
Copy Markdown
Contributor Author

Something has gone wrong here probably in your merge with main. You have over 12k line change, and some large changes in the rst files. I'd start with fixing those issues if I were you.

@adrinjalali - sorry for the long gap . Would you pls be able to review now ?

@getgaurav2

Copy link
Copy Markdown
Contributor Author

@glemaitre this seems like a good candidate for a "time series related" "project board".

@adrinjalali - Would you pls be able to share a link if this is public ..Thank you !

train,
np.array(
[
0,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in all these cases, we don't have to write out each individual case, you can use functions such as range, etc to make these much smaller diffs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure ..fixed now.

Comment on lines +2536 to +2539
# `np.unique` will reorder the group. We need to keep the original
# ordering.
reordered_unique_groups, indices = np.unique(groups, return_index=True)
unique_groups = reordered_unique_groups[np.argsort(indices)]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we care about the order? groups doesn't have any ordered semantics.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we care about the order? groups doesn't have any ordered semantics.

This is done to preserve the sequence in which the labels of the group appear in the dataset. The split logic will cut the list in the same order as the group label appear in the dataset.

@lcrmorin

Copy link
Copy Markdown

This is something of interest for panel data. Is there something that can be done to help ? @getgaurav2 @jnothman

@getgaurav2

Copy link
Copy Markdown
Contributor Author

This is something of interest for panel data. Is there something that can be done to help ? @getgaurav2 @jnothman

I would appreciate a review and feedback on the code from the core team

@lcrmorin

Copy link
Copy Markdown

Is it me or gap counts groups, but test_size and max_train_size trim rows ? Wouldn't counting whole groups for all three make more sense ?

Also some tests seems to be missing / stale.

@getgaurav2

getgaurav2 commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Is it me or gap counts groups, but test_size and max_train_size trim rows ? Wouldn't counting whole groups for all three make more sense ?

Also some tests seems to be missing / stale.

@lcrmorin Thanks, you're right: gap counted groups while test_size and max_train_size trimmed rows, which could cut a group in half. All three now count whole groups. I also replaced the stale tests: the old max_train_size test never actually checked anything, and test_size had no test.

The trade-off is that test sets now have the same number of groups, but not the same number of samples when group sizes differ. For example, with group sizes [20, 12, 30, 2, 3, 20, 30, 2, 3, 2, 3] and n_splits=3, the test sets contain 50, 5 and 5 samples.

getgaurav2 and others added 4 commits October 11, 2026 10:15
…4257

# Conflicts:
#	sklearn/model_selection/__init__.py
test_size and max_train_size were applied to sample indices after the
split, so they could cut a group in half. Pass them to TimeSeriesSplit on
the ordered groups instead, like gap, and validate in group terms so that
error messages count groups rather than samples.

Replace the stale max_train_size test and the index-listing tests with
parametrized tests: exact splits on imbalanced groups, invariants for all
parameter combinations, equivalence with TimeSeriesSplit when each group
has one sample, and the group-worded error.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the size/L label Nov 1, 2026
@lcrmorin

lcrmorin commented Nov 2, 2026 •

Copy link
Copy Markdown

For further validation / testing:

  • Could we explicitly say that rows must already be chronological and groups follow first appearance ? A test with labels like ["z", "z", "a", "b", "b"] would protect that behavior.

  • Could we check invalid sizes (test_size <= 0, max_train_size <= 0) and negative gap, with clear errors ?

  • Should there be a test for each of train / val / test to be empty ?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GroupedTimeSeriesSplit with params - gap and test_size Feature request: Group aware Time-based cross validation