(Python - sklearn) How to pass parameters to the customize ModelTransformer class by gridsearchcv

前端 未结 1 831
长发绾君心
长发绾君心 2020-12-04 16:53

Below is my pipeline and it seems that I can\'t pass the parameters to my models by using the ModelTransformer class, which I take it from the link (http://zacstewart.com/20

相关标签:
1条回答
  • 2020-12-04 17:32

    GridSearchCV has a special naming convention for nested objects. In your case ess__rfc__n_estimators stands for ess.rfc.n_estimators, and, according to the definition of the pipeline, it points to the property n_estimators of

    ModelTransformer(RandomForestClassifier(n_jobs=-1, random_state=1,  n_estimators=100)))
    

    Obviously, ModelTransformer instances don't have such property.

    The fix is easy: in order to access underlying object of ModelTransformer one needs to use model field. So, grid parameters become

    parameters = {
      'ess__rfc__model__n_estimators': (100, 200),
    }
    

    P.S. it's not the only problem with your code. In order to use multiple jobs in GridSearchCV, you need to make all objects you're using copy-able. This is achieved by implementing methods get_params and set_params, you can borrow them from BaseEstimator mixin.

    0 讨论(0)
提交回复
热议问题