Skip to content

DOC add example for implementing a custom CV splitter - #34450

Closed
moehersi wants to merge 1 commit into
scikit-learn:mainfrom
moehersi:moeahmed-github-patch-1
Closed

moehersi wants to merge 1 commit into
scikit-learn:mainfrom
moehersi:moeahmed-github-patch-1

Conversation

@moehersi

@moehersi moehersi commented Jul 8, 2026

Copy link
Copy Markdown

Adds a gallery example demonstrating how to implement a custom cross-validation splitter, as suggested in #28828.

Reference Issues/PRs

What does this implement/fix? Explain your changes.

First time contributor introduction

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

Any other comments?

Adds a gallery example demonstrating how to implement a custom cross-validation splitter, as suggested in scikit-learn#28828.
@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown

Thank you for opening your first pull request to scikit-learn! 🎉

To help get your contribution reviewed, please make sure that:

  • You have filled out the pull request template.
  • The pull request addresses an existing issue that is ready for
    contribution (e.g. not tagged as "Needs Triage", "Needs Decision", ...).
    If you are proposing a new feature, please open an issue to discuss it first.
  • There are no other open pull requests already targeting the same issue.
  • You have followed the pull request checklist.
    In particular, linting and tests should pass.

@github-actions github-actions Bot added Documentation CI:Linter failure The linter CI is failing on this PR labels Jul 8, 2026
@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown

❌ Linting issues

This PR is introducing linting issues. Here's a summary of the issues. Note that you can avoid having linting issues by enabling pre-commit hooks. Instructions to enable them can be found here.

You can see the details of the linting issues under the lint job here


ruff check

ruff detected issues. Please run ruff check --fix --output-format=full locally, fix the remaining issues, and push the changes. Here you can see the detected issues. Note that the installed ruff version is ruff=0.12.2.

Details

examples/model_selection/plot_custom_cv_splitter.py:1:1: CPY001 Missing copyright notice at top of file
Found 1 error.

Generated for commit: be5c7b0. Link to the linter CI: here

@StefanieSenger

Copy link
Copy Markdown
Member

This PR could be reviewed as part of the sprints at EuroPython/EuroScipy, for a participant interested in trying yourself at reviewing.

@StefanieSenger StefanieSenger added Sprint good first PR to review Simple atomic PR to review and removed Sprint labels Jul 18, 2026

@PranavAchar01 PranavAchar01 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for putting this together, a worked example for custom splitters is a nice addition. I ran it locally and it does work end to end. A few things I noticed:

The example emits a ConvergenceWarning. LogisticRegression with the default max_iter=100 does not converge on unscaled iris, so building the gallery prints lbfgs failed to converge after 100 iteration(s). max_iter=1000 clears it.

random_state=42 has no effect here. Per the docstring it is only used when solver is 'sag', 'saga' or 'liblinear', and the default is 'lbfgs'. In a teaching example it may suggest the fit is stochastic when it is not.

The folds end up nearly single-class. iris is ordered by target and the splitter takes contiguous blocks, so with n_folds=5 the test folds are:

fold 0: {0: 30}
fold 1: {0: 20, 1: 10}
fold 2: {1: 30}
fold 3: {1: 10, 2: 20}
fold 4: {2: 30}

Nothing breaks (mean 0.927, versus 0.973 for StratifiedKFold), but a reader copying the pattern onto their own ordered data could get a misleading estimate without realising why. A sentence noting that contiguous folds assume the data is not ordered by the target, or shuffling the indices, would make the lesson safer.

Minor: in split, the comprehension reuses the loop variable name:

for split_idx, split in enumerate(splits):
    train_idxs = np.concatenate(
        [split for idx, split in enumerate(splits) if idx != split_idx]
    )

This is correct in Python 3 since the comprehension has its own scope, but it is a double take when reading.

@ArturoAmorQ

Copy link
Copy Markdown
Member

Hi @moeahmed-github are you still working on this one? Do you mind if I take over?

@moehersi

Copy link
Copy Markdown
Author

Hi @ArturoAmorQ feel free to take over

@virchan virchan left a comment

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.

Closing in favour of #34852.

@virchan virchan added Stalled and removed good first PR to review Simple atomic PR to review labels Sep 17, 2026
@virchan virchan closed this Sep 17, 2026
@virchan virchan removed the Stalled label Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:Linter failure The linter CI is failing on this PR Documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants