Skip to content

Fix validation and fold assignment in get_k_folds - #302

Merged
siriak merged 1 commit into
TheAlgorithms:masterfrom
Navanee45:fix-k-fold-validation
Nov 19, 2026
Merged

siriak merged 1 commit into
TheAlgorithms:masterfrom
Navanee45:fix-k-fold-validation

Conversation

@Navanee45

Copy link
Copy Markdown
Contributor

Fixes get_k_folds() in data_preprocessing/k_folds.r.

The function now validates its inputs and reports clear errors when y is empty or k is invalid. Fold assignments are generated once, so the list and vector return formats represent the same randomized, reproducible assignment. Fold sizes differ by at most one observation.

Tests

  • Checked that valid inputs assign every observation to exactly one fold.
  • Checked that list and vector outputs agree for the same seed.
  • Checked that invalid k values produce a clear error.

Copilot AI 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.

🟡 Changes recommended

The fold-splitting code mishandles non-finite and out-of-range k values and introduces O(n × k) grouping, causing quadratic work for leave-one-out assignments.

2 open issues
What changed in this PR

Updates get_k_folds() in the data-preprocessing utilities to validate inputs and produce consistent, balanced cross-validation assignments.

Changes:

  • Adds checks for empty y, invalid k, and invalid isList.
  • Uses one seeded random assignment for both return formats, with fold sizes differing by at most one observation.
  • Clarifies parameter descriptions and return formats in comments.
File Description
data_preprocessing/​k_folds.r Adds input validation and replaces matrix-based grouping with shared randomized fold IDs.

Review effort: Balanced

Comment thread data_preprocessing/k_folds.r Resolved
Comment thread data_preprocessing/k_folds.r Resolved

@siriak siriak 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.

Looks good, thanks!

@siriak
siriak merged commit 521ec7b into TheAlgorithms:master Nov 19, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants