Transform results of estimator in a sklearn pipeline

Viewed 105

I have a sklearn pipeline that consists of a custom transformer, followed by XGBClassifier. What I would like to add as a final step in the transformer is another custom transformer that transforms the results of the XGBClassifier.

This last custom transformer will rank the predicted probabilities into ranks (5-percentiles).

Pipeline([
          ('custom_trsf1', custom_trsf1),
          ('clf', XGBCLassifier()),
          ('custom_trsf2', custom_trsf2)])

The problem is that the sklearn pipeline requires that all steps (but the last) should have a fit and transform method. Can I solve this in another way instead of extending the XGBclassifier and adding a transform method to it?

1 Answers

From seeing the source code of Pipeline implementation, the estimator used to fit the data goes on the last position of your steps, the _final_estimator property of Pipeline calls the last position of Pipeline's steps.

@property
def _final_estimator(self):
    estimator = self.steps[-1][1]
    return 'passthrough' if estimator is None else estimator

where steps might be something like

steps = [('scaler', StandardScaler(copy=True, with_mean=True, with_std=True)),
 ('svc',
  SVC(C=1.0, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
      decision_function_shape='ovr', degree=3, gamma='scale', kernel='rbf',
      max_iter=-1, probability=False, random_state=None, shrinking=True,
      tol=0.001, verbose=False))]

The _final_estimator property is just called, after fitting all the transforms one after the other, to get the estimator to be fitted to the model, see line 333 for details.

So, considering steps, I can retrieve an SVC class from it's last position

final_estimator = steps[-1][1]
final_estimator
>>> SVC(C=1.0, ..., verbose=False)

and fit it the training data

final_estimator.fit(Xt, y)

where Xt is the transformed training data (calculated before fitting the estimator) and y the training target.

Related