我在一些数据集中应用了 SVM (scikit-learn),并希望找到可以为测试集提供最佳准确度的 C 和 gamma 值。
我首先将 C 固定为某个整数,然后遍历多个 gamma 值,直到获得为该 C 提供最佳测试集精度的 gamma。然后我固定了我在上述步骤中获得的 gamma 和迭代 C 的值并找到一个 C 可以给我最好的准确性等等......
但上述步骤永远无法给出产生最佳测试集准确度的 gamma 和 C 的最佳组合。
任何人都可以帮助我找到一种方法来获得这个组合(gamma,C) sckit-学习?
最佳答案
您正在寻找超参数调整。在参数调整中,我们传递一个包含分类器可能值列表的字典,然后根据您选择的方法(即 GridSearchCV、RandomSearch 等)返回最佳可能参数。您可以阅读更多相关信息 here .
例如:
#Create a dictionary of possible parameters
params_grid = {'C': [0.001, 0.01, 0.1, 1, 10, 100],
'gamma': [0.0001, 0.001, 0.01, 0.1],
'kernel':['linear','rbf'] }
#Create the GridSearchCV object
grid_clf = GridSearchCV(SVC(class_weight='balanced'), params_grid)
#Fit the data with the best possible parameters
grid_clf = clf.fit(X_train, y_train)
#Print the best estimator with it's parameters
print grid_clf.best_estimators
您可以阅读有关 GridSearchCV 的更多信息 here和 RandomizedSearchCV here .不过请注意,SVM 会占用大量 CPU 资源,因此请注意传递的参数数量。根据您的数据和您传递的参数数量,可能需要一些时间来处理。
This link还包含一个示例
关于python - 找到 C 和 gamma 的值以优化 SVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46330329/