Generate cross-validated estimates for each input data point. The data is split according to the cv parameter. Each sample belongs to exactly one test set, and its prediction is computed with an estimator fitted on the corresponding training set. Passing these predictions into an e
(
estimator,
X,
y=None,
*,
groups=None,
cv=None,
n_jobs=None,
verbose=0,
params=None,
pre_dispatch="2*n_jobs",
method="predict",
)
| 1025 | prefer_skip_nested_validation=False, # estimator is not validated yet |
| 1026 | ) |
| 1027 | def cross_val_predict( |
| 1028 | estimator, |
| 1029 | X, |
| 1030 | y=None, |
| 1031 | *, |
| 1032 | groups=None, |
| 1033 | cv=None, |
| 1034 | n_jobs=None, |
| 1035 | verbose=0, |
| 1036 | params=None, |
| 1037 | pre_dispatch="2*n_jobs", |
| 1038 | method="predict", |
| 1039 | ): |
| 1040 | """Generate cross-validated estimates for each input data point. |
| 1041 | |
| 1042 | The data is split according to the cv parameter. Each sample belongs |
| 1043 | to exactly one test set, and its prediction is computed with an |
| 1044 | estimator fitted on the corresponding training set. |
| 1045 | |
| 1046 | Passing these predictions into an evaluation metric may not be a valid |
| 1047 | way to measure generalization performance. Results can differ from |
| 1048 | :func:`cross_validate` and :func:`cross_val_score` unless all tests sets |
| 1049 | have equal size and the metric decomposes over samples. |
| 1050 | |
| 1051 | Read more in the :ref:`User Guide <cross_validation>`. |
| 1052 | |
| 1053 | Parameters |
| 1054 | ---------- |
| 1055 | estimator : estimator |
| 1056 | The estimator instance to use to fit the data. It must implement a `fit` |
| 1057 | method and the method given by the `method` parameter. |
| 1058 | |
| 1059 | X : {array-like, sparse matrix} of shape (n_samples, n_features) |
| 1060 | The data to fit. Can be, for example a list, or an array at least 2d. |
| 1061 | |
| 1062 | y : {array-like, sparse matrix} of shape (n_samples,) or (n_samples, n_outputs), \ |
| 1063 | default=None |
| 1064 | The target variable to try to predict in the case of |
| 1065 | supervised learning. |
| 1066 | |
| 1067 | groups : array-like of shape (n_samples,), default=None |
| 1068 | Group labels for the samples used while splitting the dataset into |
| 1069 | train/test set. Only used in conjunction with a "Group" :term:`cv` |
| 1070 | instance (e.g., :class:`GroupKFold`). |
| 1071 | |
| 1072 | .. versionchanged:: 1.4 |
| 1073 | ``groups`` can only be passed if metadata routing is not enabled |
| 1074 | via ``sklearn.set_config(enable_metadata_routing=True)``. When routing |
| 1075 | is enabled, pass ``groups`` alongside other metadata via the ``params`` |
| 1076 | argument instead. E.g.: |
| 1077 | ``cross_val_predict(..., params={'groups': groups})``. |
| 1078 | |
| 1079 | cv : int, cross-validation generator or an iterable, default=None |
| 1080 | Determines the cross-validation splitting strategy. |
| 1081 | Possible inputs for cv are: |
| 1082 | |
| 1083 | - None, to use the default 5-fold cross validation, |
| 1084 | - int, to specify the number of folds in a `(Stratified)KFold`, |
searching dependent graphs…