问题描述
我想使用 GridSearchCV .但是, LinearSVC 禁止使用某些参数组合引发异常.特别是,dual
,penalty
和loss
参数存在互斥的组合:
I want to greedily search the entire parameter space of my support vector classifier using GridSearchCV. However, some combinations of parameters are forbidden by LinearSVC and throw an exception. In particular, there are mutually exclusive combinations of the dual
, penalty
, and loss
parameters:
例如,此代码:
from sklearn import svm, datasets
from sklearn.model_selection import GridSearchCV
iris = datasets.load_iris()
parameters = {'dual':[True, False], 'penalty' : ['l1', 'l2'], \
'loss': ['hinge', 'squared_hinge']}
svc = svm.LinearSVC()
clf = GridSearchCV(svc, parameters)
clf.fit(iris.data, iris.target)
返回ValueError: Unsupported set of arguments: The combination of penalty='l2' and loss='hinge' are not supported when dual=False, Parameters: penalty='l2', loss='hinge', dual=False
我的问题是:是否可以使GridSearchCV跳过模型禁止的参数组合?如果不是,是否有一种简单的方法来构建不会违反规则的参数空间?
My question is: is it possible to make GridSearchCV skip combinations of parameters which the model forbids? If not, is there an easy way to construct a parameter space which won't violate the rules?
推荐答案
我通过将error_score=0.0
传递给GridSearchCV
解决了这个问题:
I solved this problem by passing error_score=0.0
to GridSearchCV
:
要分配给 如果估计器拟合中发生错误,则得分.如果设置为提高",则 引发错误.如果给出数值,则FitFailedWarning为 提高.此参数不会影响改装步骤,该步骤将 总是会引发错误.
Value to assign to the score if an error occurs in estimator fitting. If set to ‘raise’, the error is raised. If a numeric value is given, FitFailedWarning is raised. This parameter does not affect the refit step, which will always raise the error.
这篇关于使用GridSearchCV时跳过禁止的参数组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!