将 OneClassSVM 与 GridSearchCV 结合使用

2024-04-30

我正在尝试在 OneClassSVM 上执行 GridSearchCV 函数,但我似乎无法找到 OCSVM 的正确评分方法。根据我收集的信息,像 OneClassSVM.score 这样的东西不存在,因此 GridSearchCV 中没有所需的默认评分函数。不幸的是,文档中的任何评分方法都不起作用,因为它们专用于有监督的 ML,而 OCSVM 是一种无监督的方法。

有没有办法在 One Class SVM 上执行网格搜索(或类似的东西,让我用正确的参数调整模型)?

这是我的 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 可以放在那里。感谢您的帮助。


我知道这是一个迟到的回复,但希望它对某人有用。 要调整参数,您需要有正确的标签(离群值/内联值)。 然后,当您拥有正确的参数时,您可以以无监督的方式使用 OneClassSVM。

因此,这种方法的评分函数可以是:

  • f1
  • 精确
  • recall

检查准确率和召回率分数的代码:

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 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 https://scikit-learn.org/stable/auto_examples/model_selection/plot_grid_search_digits.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 OneClassSVM 与 GridSearchCV 结合使用 的相关文章

随机推荐