Search code examples
pythonmachine-learningscikit-learncross-validation

cross_val_score returns nan when put in fit_params


I am doing SVC for classification task with cross validation using cross_val_score in slearn, but turns out it return list of nan value when I put in parameters for fit_params but working fine if I dont put in the parameters for fit_params.

Code:

# 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])

Solution

  • fit_params is designated for fit methods (e.g., array of sample weights for training data), but you pass your parameter grid to 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 or similar tools.