【问题标题】:Can I use fixed parameter settings for one component when using GridSearchCV with pipeline?使用 GridSearchCV 和 pipeline 时,我可以为一个组件使用固定的参数设置吗?
【发布时间】:2021-09-15 19:23:54
【问题描述】:

我正在尝试先将 PCA 应用于原始数据,然后使用决策树进行分类。

对于 PCA,我只想修复 n_components,对于决策树,我正在使用 GridSearchCV 来查找最佳超参数设置。

如何确保 n_components 不会改变?我在管道中定义PCA时可以修复它,并且在GridSearchCV的param_grid中没有提及PCA的任何设置吗?

或者我应该在 GridSearchCV 的 param_grid 中修复它,例如 'PCA__n_components':[5]?

【问题讨论】:

    标签: scikit-learn pipeline gridsearchcv


    【解决方案1】:

    有两种方法可以执行此操作。

    1. 使用超参数初始化 Transformer/Estimator:

      pipe = Pipeline(
          steps=[('pca', PCA(n_components=2)),
                 ('clf', DecisionTreeClassifier())])
      
    2. 修复param_grid中的超参数:

      param_grid = {
      'pca__n_components': [2]
      }
      
      grid = GridSearchCV(pipe, param_grid)
      

    注意事项: PCA 的尺寸是微调模型的重要参数。最佳的n_components 肯定会提高模型的性能,同时降低其复杂性。

    【讨论】:

      猜你喜欢
      • 2020-07-02
      • 2023-03-29
      • 2021-06-24
      • 1970-01-01
      • 2022-01-23
      • 2018-01-27
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      相关资源
      最近更新 更多