【问题标题】:How can i access to the best estimator params in a model that is contained in a pipeline?如何访问管道中包含的模型中的最佳估计器参数?
【发布时间】:2020-03-10 07:38:17
【问题描述】:

我有以下 sklearn 管道:

Pipeline(memory=None,
     steps=[('feature_processor', DataProcessor()),
            ('classifier', GridSearchCV(cv=15,
                                        error_score='raise-deprecating',
                                        estimator=XGBClassifier(base_score=0.5,
                                                                booster='gbtree',
                                                                colsample_bylevel=1,
                                                                colsample_bynode=1, 
                                                                colsample_bytree=1,
                                                                gamma=0, learning_rate=0.1,
                                                                max_delta_step=0,..._dispatch='2*n_jobs',
                                                                refit=True,
                                                                return_train_score='warn',
                                                                scoring='accuracy', verbose=1))
            ])

里面有一个训练好的模型,使用GridSearchCV优化了参数。带有管道的模型被保存到 pickle 中。我正在使用pickle.load() 来读回它,但现在我只是不知道如何访问GridSearchCV 找到的最佳参数。

请有人指出我正确的方向吗?

如果无法通过管道的信息访问此信息,是否还有其他方法可以做到这一点?

非常感谢您

【问题讨论】:

  • 这个答案有帮助吗?
  • 是的,是的,对不起,我忘了接受,但现在可以了

标签: python python-3.x machine-learning scikit-learn pipeline


【解决方案1】:

试试这个:

pipeline.named_steps['classifier'].best_params_

【讨论】:

    【解决方案2】:

    从您的示例中,您可以通过以下方式获得最佳模型:

    loaded_pipe = pickle.load(open("<your_pkl_file>", 'rb'))
    loaded_pipe['classifer'].best_estimator_
    

    编辑:

    仅用于最佳参数:

    loaded_pipe['classifer'].best_params_
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-09
      • 2021-07-19
      • 2019-10-30
      • 2019-12-04
      • 2018-06-24
      • 2020-09-11
      • 2017-06-13
      相关资源
      最近更新 更多