Search code examples
pythonscikit-learnregressiontransformer-model

Add a custom transformer of the predicted vector for regression in sklearn pipeline


I have constructed a sklearn pipeline with a preprocessor and a regressor for a regression problem.

Regressor = GradientBoostingRegressor()

Model = Pipeline([("preprocessor", xgb_model_preprocessor),
              ("reg", Regressor)])

The output of Model.predict(X_test) contains some negative values, but my target Y_test is a positive vector. To increase my score, I want to apply a very simple custom function that returns 0 for every negative prediction. I want to add this into my pipeline directly.

Example :

Model.predict(X_test) = [5 , 10 , 1  , -2  , 8 , -1 ]

And I want my new pipeline Model_2 so that :

Model_2.predict(X_test) = [5 , 10 , 1  , 0  , 8 , 0 ]

Can anyone help me achieving this ?

Thank you very much for your help.


Solution

  • Okay, months later I have got the answer !

    You can use sklearn.ensemble.StackingRegressor , which allows you to use the output of a given estimator as the input of antoher estimator.

    In the case of my problem, a custom estimator easy to code would work.