Use of OneClassSVM with GridSearchCV

眉间皱痕 提交于 2019-12-10 19:49:40

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/49979171/use-of-oneclasssvm-with-gridsearchcv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!