I'm using GridSearchCV in combination with a pipeline that includes standardization as a first step. I've found that when predicting for a test dataset using the .predict method of GridSearchCV, the results differ from when the pipeline steps are implemented manually. I've created a simplified version of my script below, to show that the errors found differ. For the purpose of simplicity, the search space consists of only 1 value per parameter.
I'm aware that the difference here is very small. But in my original code, this difference is significantly larger. Thus I am trying to understand what causes the difference between the two methods
Initializing the data
import random
import numpy as np
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import GridSearchCV, KFold
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVR
random.seed(5)
np.random.seed(5)
x_train = np.random.rand(1000,4)
y_train = np.random.rand(1000,1)
x_test = np.random.rand(100,4)
y_test = np.random.rand(100,1)
C_space = 1
epsilon_space = 0.04
gamma_space = 0.001
Implementing a Pipeline
cv_folds = KFold(n_splits=5, shuffle=True)
steps = [
('scaler', StandardScaler()),
('svr', SVR(kernel='rbf', gamma=gamma_space, C=C_space, epsilon=epsilon_space))
]
pipe = Pipeline(steps, verbose=0)
search_space = [{
'svr__gamma':[gamma_space],
'svr__C':[C_space],
'svr__epsilon':[epsilon_space]
}]
mod = GridSearchCV(pipe, search_space,
scoring='neg_mean_absolute_error', cv=5, verbose=0, return_train_score=True, refit=True)
svr = mod.fit(x_train, y_train)
y_pred = svr.predict(x_test)
error = mean_absolute_error(y_pred, y_test)
Implementing the steps manually
scaler = StandardScaler()
x_train_scaled = scaler.fit_transform(x_train)
x_test_scaled = scaler.fit_transform(x_test)
manual_svr = SVR(kernel='rbf', gamma=gamma_space, C=C_space,
epsilon=epsilon_space).fit(x_train_scaled, y_train)
y_pred_manual = manual_svr.predict(x_test_scaled)
error_manual = mean_absolute_error(y_pred_manual, y_test)
The outcome is:
Pipeline error is: 0.23495746730222067
Manual error is: 0.23487379770774958