When I train a SVC with cross validation,
y_pred = cross_val_predict(svc, X, y, cv=5, method='predict')
cross_val_predict
returns one class prediction for each element in X, so that y_pred.shape = (1000,)
when m=1000
.
This makes sense, since cv=5
and therefore the SVC was trained and validated 5 times on different parts of X. In each of the five validations, predictions were made for one fifth of the instances (m/5 = 200
). Subsequently the 5 vectors, containing 200 predictions each, were merged to y_pred
.
With all of this in mind it would be reasonable for me to calculate the overall accuracy of the SVC using y_pred
and y.
score = accuracy_score(y, y_pred)
But (!) the documentation of cross_val_predict
states:
The result of cross_val_predict may be different from those obtained using cross_val_score as the elements are grouped in different ways. The function cross_val_score takes an average over cross-validation folds, whereas cross_val_predict simply returns the labels (or probabilities) from several distinct models undistinguished. Thus, cross_val_predict is not an appropriate measure of generalisation error.
Could someone please explain in other words, why cross_val_predict
is not appropriate for measuring the generalisation error e.g. via accuracy_score(y, y_pred)
?
Edit:
I first assumed that with cv=5
in each of the 5 validations predicitons would be made for all instances of X. But this is wrong, predictions are only made for 1/5 of the instances of X per validation.