how to pass fit parameters of estimator to RFECV's fit?

Viewed 272

I have a dataframe with some features and i would like to select important ones using RFECV. But, when I tried to send xgboost's fit parameters to RFECV's fit method like this:

# initialising xgboost
xgb_rfe = XGBClassifier(objective='multi:softmax', num_class=3, use_label_encoder=False,
                        random_state=100, n_estimators=10_000, n_jobs=-1)

# initialising RFECV
rfe = RFECV(estimator=xgb_rfe, min_features_to_select=2, verbose=2, n_jobs=2, cv=3, scoring=log_loss_rfe)

# fitting it
rfe.fit(X=X_train, y=y_train, estimator__early_stopping_rounds=3_000)

I get TypeError like this:

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-17-a1b4fc8b7d71> in <module>()
      1 rfe = RFECV(estimator=xgb_rfe, min_features_to_select=2, verbose=2, n_jobs=2, cv=3, scoring=log_loss_rfe)
----> 2 rfe.fit(X=X_train, y=y_train, early_stopping_rounds=3_000)

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

And passing early_stopping_rounds directly to XGBClassifier during initialization doesn't work.

How do I pass parameters of my estimator to RFECV during fit?

1 Answers

It is not possible to pass the estimator's fit parameters to rfe.fit because this method does not accept keyword arguments:

def fit(self, X, y, groups=None)

You would have to modify the source code for RFECV to make this possible.

On a side note, you can see the difference for instance with GridSearchCV which does accept keyword arguments in its fit method (GridSearchCV inherits from BaseSearchCV).

Related