【问题标题】:More than one estimator in GridSearchCV(sklearn)GridSearchCV(sklearn)中的多个估计器
【发布时间】:2019-01-08 18:44:48
【问题描述】:

我正在查看有关 GridSearchCV 的 sklearn 文档网页。 GridSearchCV 对象的属性之一是best_estimator_。 所以这是我的问题。如何将多个估计器传递给 GSCV 对象?

使用像这样的字典: {'SVC()':{'C':10, 'gamma':0.01}, ' DecTreeClass()':{....}}?

【问题讨论】:

    标签: python scikit-learn grid-search


    【解决方案1】:

    GridSearchCV 处理参数。它将使用param_grid 中指定的不同参数组合训练多个估计器(但同一类(SVC 或DecisionTreeClassifier 或其他分类器之一)。best_estimator_ 是在数据上表现最佳的估计器。

    所以基本上best_estimator_ 是用最佳参数初始化的同一个类对象。

    因此,在基本设置中,您不能在网格搜索中使用多个估算器。

    但作为一种解决方法,您可以在使用管道时拥有多个估算器,其中估算器是 GridSearchCV 可以设置的"parameter"

    类似这样的:

    from sklearn.pipeline import Pipeline
    from sklearn.svm import SVC
    from sklearn.tree import DecisionTreeClassifier
    from sklearn.model_selection import GridSearchCV
    from sklearn.datasets import load_iris
    iris_data = load_iris()
    X, y = iris_data.data, iris_data.target
    
    
    # Just initialize the pipeline with any estimator you like    
    pipe = Pipeline(steps=[('estimator', SVC())])
    
    # Add a dict of estimator and estimator related parameters in this list
    params_grid = [{
                    'estimator':[SVC()],
                    'estimator__C': [1, 10, 100, 1000],
                    'estimator__gamma': [0.001, 0.0001],
                    },
                    {
                    'estimator': [DecisionTreeClassifier()],
                    'estimator__max_depth': [1,2,3,4,5],
                    'estimator__max_features': [None, "auto", "sqrt", "log2"],
                    },
                   # {'estimator':[Any_other_estimator_you_want],
                   #  'estimator__valid_param_of_your_estimator':[valid_values]
    
                  ]
    
    grid = GridSearchCV(pipe, params_grid)
    

    您可以在params_grid 列表中添加任意数量的字典,但请确保每个字典都具有与'estimator' 相关的兼容参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-28
      • 2022-07-19
      • 2017-08-18
      • 2016-02-20
      • 2020-12-26
      • 2021-03-29
      • 2019-05-01
      • 2014-09-15
      相关资源
      最近更新 更多