Skip to content

Commit 4b44818

Browse files
jeromedockesrcap107
authored andcommitted
rename splitter -> split_func in DataOp.skb.train_test_split (#1630)
1 parent 895cc0f commit 4b44818

5 files changed

Lines changed: 39 additions & 13 deletions

File tree

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Changes
2828
:class:`ApplyToFrame`, or ``"no_wrap"`` for no wrapping. The old values are
2929
deprecated and will result in an error in a future release.
3030
:pr:`1628` by :user:`Jérôme Dockès <jeromedockes>`.
31+
- The parameter ``splitter`` of :meth:`DataOp.skb.train_test_split` has been
32+
renamed ``split_func``. :pr:`1630` by :user:`Jérôme Dockès <jeromedockes>`.
3133

3234

3335
Bugfixes

doc/modules/data_ops/validation/tuning_validating_data_ops.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ which we used :meth:`.skb.mark_as_X() <DataOp.skb.mark_as_X>` and
102102
:meth:`.skb.mark_as_y() <DataOp.skb.mark_as_y>`: the first few steps of the
103103
pipeline are executed until we have a value for ``X`` and for ``y``.
104104
Then, those
105-
dataframes are split using the provided splitter function (by default
105+
dataframes are split using the provided split function (by default
106106
scikit-learn's :func:`sklearn.model_selection.train_test_split`).
107107

108108
>>> split = pred.skb.train_test_split(shuffle=False)
@@ -134,5 +134,5 @@ And we can obtain predictions on the test part:
134134
>>> r2_score(test_y_true, test_pred) # doctest: +SKIP
135135
0.440999149220359
136136

137-
It is possible to define a custom splitter function to use instead of
137+
It is possible to define a custom split function to use instead of
138138
:func:`sklearn.model_selection.train_test_split`.

skrub/_data_ops/_estimator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,8 @@ def train_test_split(
644644
environment,
645645
*,
646646
keep_subsampling=False,
647-
splitter=model_selection.train_test_split,
648-
**splitter_kwargs,
647+
split_func=model_selection.train_test_split,
648+
**split_func_kwargs,
649649
):
650650
"""Split an environment into a training an testing environments.
651651
@@ -656,9 +656,9 @@ def train_test_split(
656656
environment = env_with_subsampling(data_op, environment, keep_subsampling)
657657
X, y = _compute_Xy(data_op, environment)
658658
if y is None:
659-
X_train, X_test = splitter(X, **splitter_kwargs)
659+
X_train, X_test = split_func(X, **split_func_kwargs)
660660
else:
661-
X_train, X_test, y_train, y_test = splitter(X, y, **splitter_kwargs)
661+
X_train, X_test, y_train, y_test = split_func(X, y, **split_func_kwargs)
662662
train_env = {**environment, X_NAME: X_train}
663663
test_env = {**environment, X_NAME: X_test}
664664
result = {

skrub/_data_ops/_skrub_namespace.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pickle
22
import typing
3+
import warnings
34

45
from sklearn import model_selection
56

@@ -1495,8 +1496,8 @@ def train_test_split(
14951496
environment=None,
14961497
*,
14971498
keep_subsampling=False,
1498-
splitter=model_selection.train_test_split,
1499-
**splitter_kwargs,
1499+
split_func=model_selection.train_test_split,
1500+
**split_func_kwargs,
15001501
):
15011502
"""Split an environment into a training an testing environments.
15021503
@@ -1512,12 +1513,12 @@ def train_test_split(
15121513
:meth:`DataOp.skb.subsample`), use a subsample of the data. By
15131514
default subsampling is not applied and all the data is used.
15141515
1515-
splitter : function, optional
1516+
split_func : function, optional
15161517
The function used to split X and y once they have been computed. By
15171518
default, :func:`~sklearn.model_selection.train_test_split` is used.
15181519
1519-
splitter_kwargs
1520-
Additional named arguments to pass to the splitter.
1520+
split_func_kwargs
1521+
Additional named arguments to pass to the splitting function.
15211522
15221523
Returns
15231524
-------
@@ -1563,14 +1564,24 @@ def train_test_split(
15631564
>>> accuracy_score(split["y_test"], predictions)
15641565
0.0
15651566
"""
1567+
if (splitter := split_func_kwargs.pop("splitter", None)) is not None:
1568+
warnings.warn(
1569+
(
1570+
"The `splitter` parameter of `.skb.train_test_split` has been"
1571+
" renamed `split_func`. Using it will raise an error in a future"
1572+
" release of skrub."
1573+
),
1574+
category=FutureWarning,
1575+
)
1576+
split_func = splitter
15661577
if environment is None:
15671578
environment = self.get_data()
15681579
return train_test_split(
15691580
self._data_op,
15701581
environment,
15711582
keep_subsampling=keep_subsampling,
1572-
splitter=splitter,
1573-
**splitter_kwargs,
1583+
split_func=split_func,
1584+
**split_func_kwargs,
15741585
)
15751586

15761587
def make_grid_search(self, *, fitted=False, keep_subsampling=False, **kwargs):

skrub/_data_ops/tests/test_estimators.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,19 @@ def test_train_test_split(with_y):
422422
assert e.skb.eval() == [7, 6, 5, 4, 3, 2, 1, 0]
423423

424424

425+
def test_train_test_split_splitter_renaming():
426+
# TODO remove when `splitter` is removed in 0.7.0
427+
X = skrub.X(list(range(10)))
428+
429+
def split(X, shuffle):
430+
return train_test_split(X, shuffle=shuffle)
431+
432+
with pytest.warns(FutureWarning, match="`splitter`.*has been renamed"):
433+
assert X.skb.train_test_split(splitter=split, shuffle=False)["X_train"] == list(
434+
range(7)
435+
)
436+
437+
425438
def test_iter_learners():
426439
e = skrub.choose_from([1, 2, 3], name="c").as_data_op()
427440
assert [p.describe_params() for p in e.skb.iter_learners_grid()] == [

0 commit comments

Comments
 (0)