Indeed model_selection.cross_val_score https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.cross_val_score.html使用输入模型来拟合数据,因此不必进行拟合。然而,它不适合用作输入的实际对象,而是一个copy它的,因此错误This SVC instance is not fitted yet...
当试图预测时。
查看源代码cross_validate https://github.com/scikit-learn/scikit-learn/blob/fd237278e895b42abe8d8d09105cbb82dc2cbba7/sklearn/model_selection/_validation.py#L42这被称为cross_val_score
,在评分步骤中,estimator
穿过去clone https://github.com/scikit-learn/scikit-learn/blob/fd237278e895b42abe8d8d09105cbb82dc2cbba7/sklearn/base.py#L48 first:
scores = parallel(
delayed(_fit_and_score)(
clone(estimator), X, y, scorers, train, test, verbose, None,
fit_params, return_train_score=return_train_score,
return_times=True, return_estimator=return_estimator,
error_score=error_score)
for train, test in cv.split(X, y, groups))
这会创建模型的深层副本(这就是实际输入模型未拟合的原因):
def clone(estimator, *, safe=True):
"""Constructs a new estimator with the same parameters.
Clone does a deep copy of the model in an estimator
without actually copying attached data. It yields a new estimator
with the same parameters that has not been fit on any data.
...