MNT Minor refactoring _fit_and_predict
@@ -819,6 +819,11 @@ def cross_val_predict(estimator, X, y=None, *, groups=None, cv=None,
X, y, groups = indexable(X, y, groups)
cv = check_cv(cv, y, classifier=is_classifier(estimator))
+ splits = list(cv.split(X, y, groups))
+
+ test_indices = np.concatenate([test for _, test in splits])
+ if not _check_is_permutation(test_indices, _num_samples(X)):
+ raise ValueError('cross_val_predict only works for partitions')
# If classification methods produce multiple columns of output,
# we need to manually encode classes to ensure consistent column ordering.
@@ -839,17 +844,9 @@ def cross_val_predict(estimator, X, y=None, *, groups=None, cv=None,
# independent, and that it is pickle-able.
parallel = Parallel(n_jobs=n_jobs, verbose=verbose,
pre_dispatch=pre_dispatch)
- prediction_blocks = parallel(delayed(_fit_and_predict)(
+ predictions = parallel(delayed(_fit_and_predict)(
clone(estimator), X, y, train, test, verbose, fit_params, method)
- for train, test in cv.split(X, y, groups))
-
- # Concatenate the predictions
- predictions = [pred_block_i for pred_block_i, _ in prediction_blocks]
- test_indices = np.concatenate([indices_i
- for _, indices_i in prediction_blocks])
-
- if not _check_is_permutation(test_indices, _num_samples(X)):
- raise ValueError('cross_val_predict only works for partitions')
+ for train, test in splits)
inv_test_indices = np.empty(len(test_indices), dtype=int)
inv_test_indices[test_indices] = np.arange(len(test_indices))
@@ -916,9 +913,6 @@ def _fit_and_predict(estimator, X, y, train, test, verbose, fit_params,
-------
predictions : sequence
Result of calling 'estimator.method'
-
- test : array-like
- This is the value of the test parameter
"""
# Adjust length of sample weights
fit_params = fit_params if fit_params is not None else {}
@@ -948,7 +942,7 @@ def _fit_and_predict(estimator, X, y, train, test, verbose, fit_params,
n_classes = len(set(y)) if y.ndim == 1 else y.shape[1]
predictions = _enforce_prediction_order(
estimator.classes_, predictions, n_classes, method)
- return predictions, test
+ return predictions
def _enforce_prediction_order(classes, predictions, n_classes, method):
