【问题标题】:Passing GridSearchCV results to an Imbalanced-Learn's Pipeline object将 GridSearchCV 结果传递给 Imbalanced-Learn 的 Pipeline 对象
【发布时间】:2019-11-09 23:37:08
【问题描述】:

这里有一个有趣的问题 - 我有 GridSearchCV 结果,在从 grid_search_cv.results_ 属性中挑选后,捕获如下:

Input: pd.DataFrame(grid_clf_rf.cv_results_).iloc[4966]['params']

Output: {'rf__max_depth': 40, 'rf__max_features': 2, 'rf__n_estimators': 310}

现在,据我了解,Imbalanced Learn 包的 Pipeline 对象是 SciKit-Learn 的 Pipeline 的包装器,它应该在其 .fit() 方法中接受 **fit_params 参数,如下所示:

clf = BalancedRandomForestClassifier(random_state = random_state, 
                                 n_jobs = n_jobs)

pipeline = Pipeline([('nt', nt), ('rf', clf)])

pipeline.fit(X_train, y_train, **pd.DataFrame(grid_clf_rf.cv_results_).iloc[4966]['params'])

但是,当我执行上述表达式时,我得到以下结果:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-64-a26424dc8038> in <module>
      4 pipeline = Pipeline([('nt', nt), ('rf', clf)])
      5 
----> 6 pipeline.fit(X_train, y_train, **pd.DataFrame(grid_clf_rf.cv_results_).iloc[4966]['params'])
      7 
      8 print_scores(pipeline, X_train, y_train, X_test, y_test)

/opt/conda/lib/python3.7/site-packages/imblearn/pipeline.py in fit(self, X, y, **fit_params)
    237         Xt, yt, fit_params = self._fit(X, y, **fit_params)
    238         if self._final_estimator is not None:
--> 239             self._final_estimator.fit(Xt, yt, **fit_params)
    240         return self
    241 

TypeError: fit() got an unexpected keyword argument 'max_features'

任何想法我做错了什么?

【问题讨论】:

    标签: python pandas scikit-learn imblearn


    【解决方案1】:

    让我们假设你想出了一组超参数,如下所示

    hyper_params=  {'rf__max_depth': 40, 'rf__max_features': 2, 'rf__n_estimators': 310}
    

    正如@Parthasarathy Subburaj 所述,这些不是fit_params。我们可以使用.set_params() 选项为管道内的分类器设置这些参数

    from imblearn.ensemble import BalancedRandomForestClassifier
    from sklearn.datasets import make_classification
    from imblearn.pipeline import Pipeline
    
    X, y = make_classification(n_samples=1000, n_classes=3,
                               n_informative=4, weights=[0.2, 0.3, 0.5],
                               random_state=0)
    
    clf = BalancedRandomForestClassifier(random_state=0)
    
    pipeline = Pipeline([ ('rf', clf)])
    
    hyper_params=  {'rf__max_depth': 40, 'rf__max_features': 2, 'rf__n_estimators': 310}
    pipeline.set_params(**hyper_params)
    
    pipeline.fit(X,y)
    
    #
    Pipeline(memory=None,
             steps=[('rf',
                     BalancedRandomForestClassifier(bootstrap=True,
                                                    class_weight=None,
                                                    criterion='gini', max_depth=40,
                                                    max_features=2,
                                                    max_leaf_nodes=None,
                                                    min_impurity_decrease=0.0,
                                                    min_samples_leaf=2,
                                                    min_samples_split=2,
                                                    min_weight_fraction_leaf=0.0,
                                                    n_estimators=310, n_jobs=1,
                                                    oob_score=False, random_state=0,
                                                    replacement=False,
                                                    sampling_strategy='auto',
                                                    verbose=0, warm_start=False))],
             verbose=False)
    

    【讨论】:

      【解决方案2】:

      为什么要将包含用于构建模型的参数的数据框提供给 .fit() 方法,它只需要 X 和 y 两个参数。您需要将模型的参数传递给BalancedRandomForestClassifier 构造函数。由于您的参数名称与 BalancedRandomForestClassifier 采用的参数名称不匹配,因此您需要像这样手动输入它

      clf = BalancedRandomForestClassifier(max_depth = 40, max_features = 2, n_estimators = 310, random_state = random_state, n_jobs = n_jobs)
      

      希望这会有所帮助!

      【讨论】:

      • 感谢您的回答,但我已经意识到这种可能性。由于其他原因,我需要的是能够将 GridSearchCV 的输出加载到管道的 .fit() 方法中。
      • 在这种情况下,您可以尝试.best_params_,它是 gridsearch 对象的一个​​属性,它返回最佳参数集的字典,键是参数名称,值是参数的值本身。
      猜你喜欢
      • 1970-01-01
      • 2022-06-29
      • 2013-11-17
      • 2018-01-27
      • 2020-01-30
      • 2020-09-02
      • 1970-01-01
      • 2011-08-30
      • 1970-01-01
      相关资源
      最近更新 更多