Search code examples
pythonmachine-learningscikit-learnsvmgridsearchcv

Exhaustive Grid search with OneClassSVM error


I'm trying to use OneClassSVM with GridSearchCV as follows:

param_grid={'nu':[0.0001,0.001,0.01,0.1,1],'gamma':[0.0001,0.001,0.01,0.1,1],'kernel':['rbf','poly','linear']}
svc=svm.OneClassSVM()
model=GridSearchCV(svc,param_grid)

But the command

model.fit(X_train, y_train)

Gives me the error:

TypeError: If no scoring is specified, the estimator passed should have a 'score' method. The estimator OneClassSVM(cache_size=200, coef0=0.0, degree=3, gamma='scale', kernel='rbf',
            max_iter=-1, nu=0.5, shrinking=True, tol=0.001, verbose=False) does not.

P.S. Using SVC instead of OneClassSVM works.


Solution

  • From the doc of GridSearchCV

    estimator :

    This is assumed to implement the scikit-learn estimator interface. Either estimator needs to provide a score function, or scoring must be passed.

    also
    scoring :

    Strategy to evaluate the performance of the cross-validated model on the test set.

    You can read more about it on the documentation page. In your case you may be able to use one of the scoring methods listed here Metrics and scoring

    I would start by passing 'accuracy' just to see if it fixes the issue and then take it from there

    model = GridSearchCV(svc, param_grid, scoring='accuracy')