|
19 | 19 | check_name, |
20 | 20 | deferred, |
21 | 21 | ) |
22 | | -from ._estimator import ParamSearch, SkrubLearner, cross_validate, train_test_split |
| 22 | +from ._estimator import ( |
| 23 | + ParamSearch, |
| 24 | + SkrubLearner, |
| 25 | + cross_validate, |
| 26 | + iter_cv_splits, |
| 27 | + train_test_split, |
| 28 | +) |
23 | 29 | from ._evaluation import ( |
24 | 30 | choices, |
25 | 31 | clone, |
|
33 | 39 | full_report, |
34 | 40 | ) |
35 | 41 | from ._subsampling import SubsamplePreviews, env_with_subsampling |
36 | | -from ._utils import NULL, attribute_error |
| 42 | +from ._utils import KFOLD_5, NULL, attribute_error |
37 | 43 |
|
38 | 44 |
|
39 | 45 | def _var_values_provided(data_op, environment): |
@@ -1528,9 +1534,9 @@ def train_test_split( |
1528 | 1534 |
|
1529 | 1535 | - train: a dictionary containing the training environment |
1530 | 1536 | - test: a dictionary containing the test environment |
1531 | | - - X_train: the value of the variable marked with ``skb.mark_as_x()`` in |
| 1537 | + - X_train: the value of the variable marked with ``skb.mark_as_X()`` in |
1532 | 1538 | the train environment |
1533 | | - - X_test: the value of the variable marked with ``skb.mark_as_x()`` in |
| 1539 | + - X_test: the value of the variable marked with ``skb.mark_as_X()`` in |
1534 | 1540 | the test environment |
1535 | 1541 | - y_train: the value of the variable marked with ``skb.mark_as_y()`` in |
1536 | 1542 | the train environment, if there is one (may not be the case for |
@@ -1584,6 +1590,71 @@ def train_test_split( |
1584 | 1590 | **split_func_kwargs, |
1585 | 1591 | ) |
1586 | 1592 |
|
| 1593 | + def iter_cv_splits(self, environment=None, *, keep_subsampling=False, cv=KFOLD_5): |
| 1594 | + """Yield splits of an environment into training and testing environments. |
| 1595 | +
|
| 1596 | + Parameters |
| 1597 | + ---------- |
| 1598 | + environment : dict, optional |
| 1599 | + The environment (dict mapping variable names to values) containing the |
| 1600 | + full data. If ``None`` (the default), the data is retrieved from the |
| 1601 | + DataOp. |
| 1602 | +
|
| 1603 | + keep_subsampling : bool, default=False |
| 1604 | + If True, and if subsampling has been configured (see |
| 1605 | + :meth:`DataOp.skb.subsample`), use a subsample of the data. By |
| 1606 | + default subsampling is not applied and all the data is used. |
| 1607 | +
|
| 1608 | + cv : int, cross-validation generator or iterable, default=KFold(5) |
| 1609 | + The default is 5-fold without shuffling. Can be a cross-validation |
| 1610 | + splitter, an iterable yielding pairs of (train, test) indices, or an |
| 1611 | + int to specify the number of folds for KFold splitting. |
| 1612 | +
|
| 1613 | + Yields |
| 1614 | + ------ |
| 1615 | + dict |
| 1616 | + For each split, a dict is produced, containing the following keys: |
| 1617 | +
|
| 1618 | + - train: a dictionary containing the training environment |
| 1619 | + - test: a dictionary containing the test environment |
| 1620 | + - X_train: the value of the variable marked with ``skb.mark_as_X()`` in |
| 1621 | + the train environment |
| 1622 | + - X_test: the value of the variable marked with ``skb.mark_as_X()`` in |
| 1623 | + the test environment |
| 1624 | + - y_train: the value of the variable marked with ``skb.mark_as_y()`` in |
| 1625 | + the train environment, if there is one (may not be the case for |
| 1626 | + unsupervised learning). |
| 1627 | + - y_test: the value of the variable marked with ``skb.mark_as_y()`` in |
| 1628 | + the test environment, if there is one (may not be the case for |
| 1629 | + unsupervised learning). |
| 1630 | +
|
| 1631 | + Examples |
| 1632 | + -------- |
| 1633 | + >>> import skrub |
| 1634 | + >>> from sklearn.dummy import DummyClassifier |
| 1635 | + >>> from sklearn.metrics import accuracy_score |
| 1636 | +
|
| 1637 | + >>> orders = skrub.var("orders") |
| 1638 | + >>> X = orders.skb.drop("delayed").skb.mark_as_X() |
| 1639 | + >>> y = orders["delayed"].skb.mark_as_y() |
| 1640 | + >>> delayed = X.skb.apply(skrub.TableVectorizer()).skb.apply( |
| 1641 | + ... DummyClassifier(), y=y |
| 1642 | + ... ) |
| 1643 | + >>> df = skrub.datasets.toy_orders().orders |
| 1644 | + >>> accuracies = [] |
| 1645 | + >>> for split in delayed.skb.iter_cv_splits({"orders": df}, cv=3): |
| 1646 | + ... learner = delayed.skb.make_learner().fit(split["train"]) |
| 1647 | + ... prediction = learner.predict(split["test"]) |
| 1648 | + ... accuracies.append(accuracy_score(split["y_test"], prediction)) |
| 1649 | + >>> accuracies |
| 1650 | + [1.0, 0.0, 1.0] |
| 1651 | + """ |
| 1652 | + if environment is None: |
| 1653 | + environment = self.get_data() |
| 1654 | + yield from iter_cv_splits( |
| 1655 | + self._data_op, environment, keep_subsampling=keep_subsampling, cv=cv |
| 1656 | + ) |
| 1657 | + |
1587 | 1658 | def make_grid_search(self, *, fitted=False, keep_subsampling=False, **kwargs): |
1588 | 1659 | """Find the best parameters with grid search. |
1589 | 1660 |
|
|
0 commit comments