2
$\begingroup$

I've looked at quite a number of how to 'create a pipeline' instructions, but I have yet to see an explanation of the benefits over what I am showing below.

To keep my example code agnostic I'll use simple pseudo-code.

So what I've been doing in order to, for example, train a model is...

Create functions/methods

function get_data(parm1, parm2...)

function prepare_data(...)

function train_model(...)

function test_model(...)

Run functions/methods <- this is what I mean by 'linear sequence of code instructions' in the Title.

get_data(parm1, parm2...)
prepare_data(...)
train_model(...)
function test_model(...)
$\endgroup$

2 Answers 2

1
$\begingroup$

As far as I know, in supervised ML the main advantage of a pipeline is to make sure the exact same steps of preprocessing are applied on the training set and the test set.

Preprocessing can be complex sometimes: it potentially includes representation, normalization, removing outliers or imputing missing values, possibly calculating new features or removing some, etc. By doing this separately for the training set and the test set, there's the usual risk of code duplication: if something is modified here and not there, this can introduce inconsistencies and bugs. Additionally a model could easily go completely wrong if the test data is not represented in the same as the training data, so this would especially critical in supervised learning.

$\endgroup$
2
  • $\begingroup$ Thanks @Erwan but isn't data preprocessing usually done at the same time for train and test data. ie preprocess all the data and then split into train/test? $\endgroup$
    – ThomasAJ
    Apr 28, 2022 at 3:19
  • 2
    $\begingroup$ @ThomasAJ no, but that's a common mistake. In principle the preprocessing should always be done after the train/test split, using information from the training set only to avoid any risk of data leakage. Also the model is supposed to be applicable to any other new test data available in the future, so in theory it should be possible to re-apply the preprocessing to any new data. $\endgroup$
    – Erwan
    Apr 28, 2022 at 9:07
1
$\begingroup$

In general the preporocessing must be done after the train test split. So, always fit and transform on train data and transform on test data. In order to avoid DATA LEAKAGE we specifically use Pipelines. It is a sequentially application of a list of actions. The purpose of the pipeline is to assemble several steps that can be cross-validated together while setting different parameters.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.