问题描述
我正在尝试在 OneClassSVM 上执行 GridSearchCV 函数,但我似乎无法为 OCSVM 找到正确的评分方法.从我收集到的类似 OneClassSVM.score 的信息不存在,因此在 GridSearchCV 中没有需要的默认评分函数.不幸的是,文档中没有任何评分方法不起作用,因为它们专用于有监督的 ML,而 OCSVM 是一种无监督的方法.
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.
有没有办法在 OneClassSVM 上执行 GridSearch(或类似的东西,让我用正确的参数调整模型)??
Is there any way to perform GridSearch (or something similar to it, letting me tune the model with right parameters) on OneClassSVM??
这是我的 GridSearchCV 代码
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)
是的,我知道 .fit 只接受一个参数,但由于它是无监督方法,我没有任何 Y 可以放在那里.感谢您的帮助.
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.
推荐答案
我知道这是一个迟到的回复,但希望它对某人有用.要调整参数,您需要有正确的标签(离群值/内联值).然后当你有正确的参数时,你可以以无监督的方式使用 OneClassSVM.
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
- 精度
- 回忆
检查准确率和召回率的代码:
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_)
这是 ElipticEnvelope(另一种异常检测算法)与 GridSearchCV 的示例用法链接:https://sdsawtelle.github.io/blog/output/week9-anomaly-andrew-ng-machine-learning-with-python.html
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
您可以在此处找到使用分类算法进行准确率和召回率评分的示例:https://scikit-learn.org/stable/auto_examples/model_selection/plot_grid_search_digits.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
这篇关于OneClassSVM 与 GridSearchCV 的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!