具有scikit-learn的多类支持向量机参数优化

时间:2016-02-02 15:45:11

标签: scikit-learn svm

我试图在scikit-learn库中使用SVC类来解决多类分类问题。我对一对一策略很感兴趣。我想 优化每对类的超参数(C和gamma)。但我不是 知道如何在scikit-learn中做到这一点。我怎样才能做到这一点? 非常感谢你。

1 个答案:

答案 0 :(得分:2)

如@ncfirth所述,您可以使用GridSearchCV根据您的训练集找到最佳参数。我在我的程序中使用了以下代码。

tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4, 1e-5, 1e-6, 1e-7, 1e-8],
                     'C': [1, 10, 100, 1000]}]

scores = ['precision', 'recall']

for score in scores:
    print("# Tuning hyper-parameters for %s" % score)
    print()

    clf = GridSearchCV(svm.SVC(C=1), tuned_parameters, cv=5,
                       scoring='%s_macro' % score)
    clf.fit(X, Y)

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

我从stackoverflow获得了上述解决方案(没有链接到它),它帮助我在我的程序中选择正确的gamma和C值。我的要求是检查' rbf'仅内核。您可以包含线性,多边形和其他内核及其参数,以检查您是否适合您的程序。