【问题标题】:Parametrize the estimator of a feature selection step in a grid search?参数化网格搜索中特征选择步骤的估计器?
【发布时间】:2021-12-31 10:02:54
【问题描述】:

如何在网格搜索中参数化特征选择步骤的估计器(此处为 n_estimatorsRandomForestClassifier)?

from sklearn.feature_selection import SelectFromModel
from sklearn.ensemble import RandomForestClassifier

sfm = SelectFromModel(RandomForestClassifier(n_estimators=100, random_state=42))

pipe = Pipeline(steps=[('preprocessor', preprocessor),
                       ('selector',  sfm),
                       ('regressor', lr)])

from sklearn.model_selection import GridSearchCV

param_grid = {
    "selector__threshold": ['0.45*median',
                            '0.5*median',
                            '0.55*median',
                            '1*median',
                            '1.25*median',
                            '1.5*median',
                            '1.75*median',
                            '2*median'],
    "regressor__penalty": ['l2'],
    "regressor__C": [0.05, 0.1, 0.25, 0.5, 0.75, 1, 1.5],   
}

search = GridSearchCV(pipe,
                      param_grid,
                      n_jobs=-1,
                      refit=True,
                      verbose=3)

search.fit(X_train, y_train)

【问题讨论】:

    标签: python scikit-learn feature-selection


    【解决方案1】:

    做到了:

    'selector__estimator__n_estimators': [10, 50, 100],
    

    【讨论】:

      猜你喜欢
      • 2015-06-26
      • 2021-05-27
      • 2023-03-09
      • 2018-08-13
      • 2016-04-07
      • 2019-05-19
      • 2015-06-07
      • 2018-11-08
      • 2021-03-19
      相关资源
      最近更新 更多