Home > database >  cross_val_score returns nan when put in fit_params
cross_val_score returns nan when put in fit_params

Time:02-23

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

CodePudding user response:

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.

CodePudding user response:

svc cannot accept the X_train and Y_train for on put you should import GridSearchCV fit data and continue

  • Related