【问题标题】:GridSearchCV output problems in Scikit-learnScikit-learn 中的 GridSearchCV 输出问题
【发布时间】:2020-11-22 12:52:15
【问题描述】:

我想在 sklearn 中执行超参数搜索以选择预处理步骤和模型,如下所示:

pipeline = Pipeline([("combiner", PolynomialFeatures()),
                     ("dimred", PCA()),
                     ("classifier", RandomForestClassifier())])

parameters = [{"combiner": [None]},
              {"combiner": [PolynomialFeatures()], "combiner__degree": [2], "combiner__interaction_only": [False, True]},

              {"dimred": [None]},
              {"dimred": [PCA()], "dimred__n_components": [.95, .75]},

              {"classifier": [RandomForestClassifier(n_estimators=100, class_weight="balanced")],
               "classifier__max_depth": [5, 10, None]},
              {"classifier": [KNeighborsClassifier(weights="distance")],
               "classifier__n_neighbors": [3, 7, 11]}]

CV = GridSearchCV(pipeline, parameters, cv=5, scoring="f1_weighted", refit=True, n_jobs=-1)
CV.fit(train_X, train_y)

当然,我需要具有最佳参数的最佳管道的结果。但是,当我使用 CV.best_estimator_ 请求最佳估算器时,我只得到获胜的组件,而不是超参数:

Pipeline(steps=[('combiner', None), ('dimred', PCA()),
                ('classifier', RandomForestClassifier())])

当我打印出CV.best_params_ 时,我得到的信息更短(仅包含Pipeline 的第一个元素,combiner,没有关于dimredclassifier 的任何信息):

{'combiner': None}

如何获得组件及其超参数的最佳管道组合?

【问题讨论】:

    标签: python machine-learning scikit-learn data-science hyperparameters


    【解决方案1】:

    Pipeline 对象有一个 get_params() 方法,它返回管道的参数。这也包括各个步骤的参数。根据您的示例,命令

    CV.best_estimator_.get_params()
    

    将检索最佳估算器的所有管道参数,包括您正在寻找的参数。

    【讨论】:

      【解决方案2】:

      由于您的 param_grid 是一个字典列表,因此每个这样的字典都提供一个单独的网格,并且搜索发生在这些网格的不相交并集上。因此,在您的情况下,best_estimator_best_params_ 对应于具有 combiner=None 的单点网格以及原始 pipeline 中定义的所有其他内容。 (而且搜索从未探索过combiner=None 与其他超参数。)

      【讨论】:

        猜你喜欢
        • 2017-09-23
        • 2013-10-01
        • 2018-05-14
        • 2017-03-14
        • 2018-01-27
        • 2020-07-17
        • 2014-02-21
        • 2017-12-08
        相关资源
        最近更新 更多