Jos*_*Jos 2 python machine-learning svm scikit-learn hyperparameters
我在某些数据集中应用了SVM(scikit-learn),并希望找到可以为测试集提供最佳准确性的C和gamma值。
我首先将C固定为某个整数,然后遍历许多伽玛值,直到获得使该C达到最佳测试设置精度的伽玛为止。然后我修复了在上述步骤中获得的伽玛并遍历值的C并找到可以给我最好的精度的C,依此类推...
但是上述步骤永远无法给出产生最佳测试设置精度的伽玛和C的最佳组合。
谁能帮助我找到一种在sckit-learn中获得此组合(gamma,C)的方法?
您正在寻找超参数调整。在参数调整中,我们传递一个字典,其中包含用于分类器的可能值的列表,然后根据您选择的方法(即GridSearchCV,RandomSearch等),返回最佳参数。您可以在此处了解更多信息。
例如:
#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
Run Code Online (Sandbox Code Playgroud)
您可以在这里阅读更多有关GridSearchCV的信息,并在这里阅读RandomizedSearchCV的信息。请注意,SVM需要占用大量CPU资源,因此请注意传递的参数数量。根据您的数据和传递的参数数量,可能需要一些时间来处理。
此链接也包含一个示例