Repository navigation
FEA Group aware Time-based cross validation - #16236
getgaurav2 wants to merge 11 commits into
Conversation
|
@getgaurav2, tests may be failing. Let us know when you want review. |
|
Linting is failing, actually |
Sure @jnothman . Will do . Thank you . |
@getgaurav2 - gentle reminder @ogrisel Sent with GitHawk |
|
@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 |
|
@getgaurav2 Great! Because I do really need this feature in the next release ;) |
|
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. |
|
@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: Anyway, it does not seem necessary to do it: see GitHub message: This branch has no conflicts with the base branch. |
Let's see if this helps. Please add test cases into test_split.py |
@albertvillanova - would you want to check the latest code base ? Sent with GitHawk |
|
@getgaurav2 thank you. I just opened a PR. |
|
Fast check: why this empty file? |
|
I would like to point out an issue in your implementation. I have created a new test case: and this is what I get: 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: By the way, and concerning the gaps, if you look at the next v0.24 release version of |
|
'In each split, test indices must be higher than before, and thus shuffling in cross validator is inappropriate'. |
|
i mean this is allowed |
@albertvillanova - Thanks for your review. I have incorporated your feedback in the latest Commit . Can you please check again . |
|
@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 ( 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 As you can see, there is no gap between training and test sets. The unexpected behavior for the parameter In this case, I think the result should be: |
|
Maybe I would also add the test case I pointed out above: |
@albertvillanova - Thanks for your feedback. Can you please review the latest commit .
|
|
@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 Just a minor addition: could you please add And please, add a whatsnew entry to the file |
@albertvillanova - Thank you . Can you please check now . |
|
@getgaurav2 thanks. I think your PR is ready for a review round. Could you please change its name from WIP to MRG? |
@adrinjalali - would you please be able to check the issue in doc build to and give some direction ....thank you ! |
|
Something has gone wrong here probably in your merge with |
@adrinjalali - sorry for the long gap . Would you pls be able to review now ? |
@adrinjalali - Would you pls be able to share a link if this is public ..Thank you ! |
| train, | ||
| np.array( | ||
| [ | ||
| 0, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Sure ..fixed now.
| # `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)] |
There was a problem hiding this comment.
why do we care about the order? groups doesn't have any ordered semantics.
There was a problem hiding this comment.
why do we care about the order?
groupsdoesn'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.
|
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 |
|
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: 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 |
…4257 # Conflicts: # sklearn/model_selection/__init__.py
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
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]>
|
For further validation / testing:
|

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).splitraises an error.TimeSeriesSplitis applied to the ordered list of groups, and the result is mapped back to sample indices.TimeSeriesSplit(n_splits,max_train_size,test_size,gap), but they count whole groups instead of samples, asGroupShuffleSplit.test_sizedoes. With one sample per group, the splits are identical toTimeSeriesSplit(tested).groupsthrough metadata routing, likeGroupKFold, so it works in nested cross-validation.AI usage disclosure
I used AI assistance for:
Any other comments?
Still to do: the user guide section in
doc/modules/cross_validation.rstand the API reference entry.Open question from @glemaitre: an option to balance samples, rather than groups, across test sets could be a follow-up.