Skip to content

Commit ee29a1f

Browse files
authored
include train and test indices in iter_cv_splits output (#2012)
1 parent be82c86 commit ee29a1f

4 files changed

Lines changed: 15 additions & 0 deletions

File tree

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ New Features
1414

1515
Changes
1616
-------
17+
- The row indices of training and testing samples are now also included in the
18+
dictionaries produced by :meth:`DataOp.skb.iter_cv_splits`. :pr:`2012` by
19+
:user:`Jérôme Dockès <jeromedockes>`.
1720

1821
Bugfixes
1922
--------

skrub/_data_ops/_estimator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,8 @@ def iter_cv_splits(data_op, environment, *, keep_subsampling=False, cv=None):
842842
"test": test_env,
843843
"X_train": X_train,
844844
"X_test": X_test,
845+
"row_indices_train": train_idx,
846+
"row_indices_test": test_idx,
845847
}
846848
if y is not None:
847849
y_train, y_test = (

skrub/_data_ops/_skrub_namespace.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,6 +1939,8 @@ def iter_cv_splits(self, environment=None, *, keep_subsampling=False, cv=None):
19391939
- y_test: the value of the variable marked with ``skb.mark_as_y()`` in
19401940
the test environment, if there is one (may not be the case for
19411941
unsupervised learning).
1942+
- row_indices_train: the row indices (in X and y) of the training samples.
1943+
- row_indices_test: the row indices (in X and y) of the testing samples.
19421944
19431945
Examples
19441946
--------

skrub/_data_ops/tests/test_estimators.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,9 +606,13 @@ def test_iter_cv_splits():
606606
s = next(splits)
607607
assert list(s["X_train"]) == list(s["train"]["_skrub_X"]) == [10, 20, 30, 40]
608608
assert list(s["X_test"]) == list(s["test"]["_skrub_X"]) == [0]
609+
assert list(s["row_indices_train"]) == [1, 2, 3, 4]
610+
assert list(s["row_indices_test"]) == [0]
609611
s = next(splits)
610612
assert list(s["X_train"]) == list(s["train"]["_skrub_X"]) == [0, 20, 30, 40]
611613
assert list(s["X_test"]) == list(s["test"]["_skrub_X"]) == [10]
614+
assert list(s["row_indices_train"]) == [0, 2, 3, 4]
615+
assert list(s["row_indices_test"]) == [1]
612616

613617
X = skrub.X(np.arange(4) * 10)
614618
y = skrub.y(np.arange(4) * -10)
@@ -618,11 +622,15 @@ def test_iter_cv_splits():
618622
assert list(s["X_test"]) == list(s["test"]["_skrub_X"]) == [0]
619623
assert list(s["y_train"]) == list(s["train"]["_skrub_y"]) == [-10, -20, -30]
620624
assert list(s["y_test"]) == list(s["test"]["_skrub_y"]) == [0]
625+
assert list(s["row_indices_train"]) == [1, 2, 3]
626+
assert list(s["row_indices_test"]) == [0]
621627
s = next(splits)
622628
assert list(s["X_train"]) == list(s["train"]["_skrub_X"]) == [0, 20, 30]
623629
assert list(s["X_test"]) == list(s["test"]["_skrub_X"]) == [10]
624630
assert list(s["y_train"]) == list(s["train"]["_skrub_y"]) == [0, -20, -30]
625631
assert list(s["y_test"]) == list(s["test"]["_skrub_y"]) == [-10]
632+
assert list(s["row_indices_train"]) == [0, 2, 3]
633+
assert list(s["row_indices_test"]) == [1]
626634

627635

628636
def test_train_test_split_splitter_renaming():

0 commit comments

Comments
 (0)