How to normalize validation set in GridSearchCV separately from training set?

Viewed 2270

How to scale each fold separately in GridSearchCV?

While training an ML model we should normalize (scale) features regarding to the training data. And then use the fitted scaler on the test data. But if using a grid search CV (5 fold) we usually provide it the training data which is already scaled. That then gets separated into folds. But How would we separately scale each of the 4-1 folds?

scl = MinMaxScaler()
scl.fit_transform(X_train)
scl.transform(X_test)

# The training data was scaled all together and
# not train and validation separately
cv = GridSearchCV(MODEL, GRID, scoring='f1', cv=5)
cv.fit(X_train, Y_train)

Please let me know if you have a suggestion how to achieve something like this.

1 Answers

This is what Pipelines are for.

Convert your current model to Pipelined model like this:

new_model = Pipeline([('scaler', MinMaxScaler()), ('model', cur_model)])

Do not scale your training set beforehand. Every time fit is called, Pipeline will automatically fit and transform your training data, (only using training data of course) and call transform on test set using fitted MinMaxScaler.

Related