【问题标题】:Sklearn and GridSearchCV - Is it expected to return optimal parameters?Sklearn 和 GridSearchCV - 是否期望返回最佳参数?
【发布时间】:2014-11-14 22:49:13
【问题描述】:

我一直致力于在 Scikit-Learn 中优化 SVR 模型,但一直无法理解如何利用 GridSearchCV。

考虑对documentation 中提供的示例代码稍作修改:

from sklearn import svm, grid_search, datasets
iris = datasets.load_iris()
parameters = {'kernel': ('linear', 'rbf'), 'C':[1.5, 10]}
svr = svm.SVC()
clf = grid_search.GridSearchCV(svr, parameters)
clf.fit(iris.data, iris.target)

clf.get_params()

由于我指定最佳 C 值的搜索仅包含 1.5 和 10,我希望模型返回使用这两个值之一。但是,当我查看输出时,情况似乎并非如此:

{'cv': None,
 'estimator': SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,
   kernel='rbf', max_iter=-1, probability=False, random_state=None,
   shrinking=True, tol=0.001, verbose=False),
 'estimator__C': 1.0,
 'estimator__cache_size': 200,
 'estimator__class_weight': None,
 'estimator__coef0': 0.0,
 'estimator__degree': 3,
 'estimator__gamma': 0.0,
 'estimator__kernel': 'rbf',
 'estimator__max_iter': -1,
 'estimator__probability': False,
 'estimator__random_state': None,
 'estimator__shrinking': True,
 'estimator__tol': 0.001,
 'estimator__verbose': False,
 'fit_params': {},
 'iid': True,
 'loss_func': None,
 'n_jobs': 1,
 'param_grid': {'C': [1.5, 10], 'kernel': ('linear', 'rbf')},
 'pre_dispatch': '2*n_jobs',
 'refit': True,
 'score_func': None,
 'scoring': None,
 'verbose': 0}

我怀疑我对 GridSearchCV 如何使用它以及我期望它返回什么有根本的误解。我曾期望它会根据我的搜索选择返回具有优化参数的分类器,但事实并非如此。

任何指导将不胜感激。

非常感谢。

【问题讨论】:

    标签: python scikit-learn


    【解决方案1】:

    你不应该在这里使用get_params。使用best_params_best_estimator_.paramsget_params 将您提供的构造函数参数返回给您。其中之一是估算器,您在其中给它一个带有默认参数的 SVC,这就是您在此处看到的。这与网格搜索中尝试的参数无关。

    如果您查看示例 (look at the bottom of the dev documentation for example),您将永远不会在 GridSearchCV 上看到 get_params - 或者实际上,我认为 ;) 它是定义 GridSearchCV 如何使用其他估算器的接口。

    【讨论】:

    • 谢谢安德烈亚斯 - 这清除了它。 'best_params' 方法返回优化的参数。再次感谢您为 sklearn 所做的所有工作 - 这是一个我一直密切关注的迷人项目。
    猜你喜欢
    • 2021-12-18
    • 2020-01-16
    • 2020-12-29
    • 1970-01-01
    • 2015-04-06
    • 2022-07-19
    • 2017-09-21
    • 2018-03-21
    • 2020-10-10
    相关资源
    最近更新 更多