cross_val_score returns nan 放入 fit_params

cross_val_score returns nan when put in fit_params

我在 slearn 中使用 cross_val_score 进行交叉验证的 SVC 分类任务,但当我为 fit_params 输入参数但工作时,结果是 return nan 值列表如果我不输入 fit_params.

的参数就好了

代码:

# define parameter
param_grid = {
    'C' : [1,5,10,20],
    'gamma' : ['auto','scale']
}

svc = SVC(kernel = "rbf")

scores = cross_val_score(svc, x_train, y_train, cv=10, fit_params = param_grid)
# scores output array([nan, nan, nan, nan, nan, nan, nan, nan, nan, nan])

scores = cross_val_score(svc, x_train, y_train, cv=10)
# scores output array([0.95833333, 0.95833333, 0.95454545, 0.93181818, 0.95454545, 0.96197719, 0.96197719, 0.94676806, 0.96197719, 0.95057034])

fit_params 指定用于 fit 方法(例如,训练数据的样本权重数组),但您将参数网格传递给 cross_val_score, which is incompatible with your data (x_train, y_train, etc.). Indeed, if you specify error_score='raise' in your cross_val_score, you will receive the corresponding error. Parameter grids should be used with GridSearchCV 或类似工具。

svc 无法接受 X_train 和 Y_train 放上 你应该导入 GridSearchCV 拟合数据并继续