【问题标题】:value error happens when using GridSearchCV使用 GridSearchCV 时发生值错误
【发布时间】:2023-03-12 15:15:02
【问题描述】:

我正在使用 GridSearchCV 进行分类,我的代码是:

parameter_grid_SVM = {'dual':[True,False],
                    'loss':["squared_hinge","hinge"],
                    'penalty':["l1","l2"] 
                    }
clf = GridSearchCV(LinearSVC(),param_grid=parameter_grid_SVM,verbose=2)
clf.fit(trian_data, labels)

然后,我遇到了错误

ValueError:不支持的参数集:仅当 dual='false' 时才支持penalty='l1'。,参数:penalty='l1',loss='hinge',dual=False

稍后我将代码更改为:

clf = GridSearchCV(LinearSVC(penalty='l1',dual=False),verbose=2)

我遇到了错误

TypeError: init() 至少需要 3 个参数(给定 3 个)

我也试过了:

parameter_grid_SVM = {
                    'loss':["squared_hinge"]
                    }
clf = GridSearchCV(LinearSVC(penalty='l1',dual=False),param_grid=parameter_grid_SVM,verbose=2)
clf.fit(trian_data, labels)

但是,我仍然有错误

ValueError:不支持的参数集:仅当 dual='false' 时才支持penalty='l1'。,参数:penalty='l1',loss='squared_hinge',dual=False

有人知道我应该怎么做吗?

【问题讨论】:

  • 只是预感:尝试将固定参数添加到您用于网格搜索的字典中作为单例列表:'penalty': ['l1'], 'dual': [False]。
  • 我也试过了,但是它返回错误 ValueError: Unsupported set of arguments: penis='l1' is only supported when dual='false'., 参数:penalty='l1', loss ='squared_hinge', dual=False

标签: python classification svm


【解决方案1】:

产生此错误消息的代码是here。我看不出是什么原因导致这种情况只是偶尔发生,但仅凭 else 意味着它可能是其他东西,而不仅仅是惩罚 ='l1', dual='false' 组合。

【讨论】:

    【解决方案2】:

    我在做稀疏 SVM 时也遇到了这个问题。我在此页面SVM module explanation 中找到了一段工作演示代码。希望它可能会有所帮助。

    clf = LinearSVC(loss='l2', penalty='l1', dual=False)

    【讨论】:

      【解决方案3】:

      如果模型使用error_score 参数抛出异常,一个选项是指示GridSearchCV 手动设置分数。见my answer here

      【讨论】:

        【解决方案4】:

        有一个类似的问题,在我的例子中,它是写十二个12而不是'el two'l2

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-10-24
          • 1970-01-01
          • 1970-01-01
          • 2019-12-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多