duscaes
duscaes

Reputation: 41

Use of OneClassSVM with GridSearchCV

I am trying to perform a GridSearchCV function on OneClassSVM, but I can't seem to find right scoring method for OCSVM. From what i've gathered something like OneClassSVM.score does not exists thus is doesn't have a default scoring function needed in GridSearchCV. Unfortunately no scoring methods from the documentation doesn't work either because they are dedicated to supervised ML and OCSVM is a unsupervised method.

Is there any way to perform GridSearch (or something similar to it, letting me tune the model with right parameters) on OneClassSVM??

Here is my code for GridSearchCV

nus = [0.001, 0.01, 0.1, 1]
gammas = [0.001, 0.01, 0.1, 1]
tuned_parameters = {'kernel' : ['rbf'], 'gamma' : gammas, 'nu': nus}
grid_search = GridSearchCV(svm.OneClassSVM(), tuned_parameters, 
scoring="??????????????????????", n_jobs=4)
grid_search.fit(X_train)

Yes I know .fit only takes one parameter but since it is unsupervised method i don't have any Y to put there. Thank you for the help.

Upvotes: 4

Views: 6411

Answers (1)

Agnieszka Miszkurka
Agnieszka Miszkurka

Reputation: 39

I know it's a late reply but hopefully it will be useful to somebody. To tune parameters you need to have right labels (outlier/inliner). Then when you have correct parameters you can use OneClassSVM in an unsupervised way.

So scoring function for this approach can be for example:

  • f1
  • precision
  • recall

Code for checking precision and recall scores:

scores = ['precision', 'recall']
for score in scores:
    clf = GridSearchCV(svm.OneClassSVM(), tuned_parameters, cv=10,
                           scoring='%s_macro' % score, return_train_score=True)

    clf.fit(X_train, y_train)

    resultDf = pd.DataFrame(clf.cv_results_)
    print(resultDf[["mean_test_score", "std_test_score", "params"]].sort_values(by=["mean_test_score"], ascending=False).head())

    print("Best parameters set found on development set:")
    print()
    print(clf.best_params_)

Here is the link with example usage of ElipticEnvelope (another anomaly detection algorithm) with GridSearchCV: https://sdsawtelle.github.io/blog/output/week9-anomaly-andrew-ng-machine-learning-with-python.html

Here you can find example of using precision and recall scoring with classification algorith: https://scikit-learn.org/stable/auto_examples/model_selection/plot_grid_search_digits.html

Upvotes: 3

Related Questions