I am trying to perform grid search in Scikit-learn for a specific algorithm with different hyperparameters over multiple train datasets stored into a dedicated dictionary. First, I call the different hyperparams and the model to be used:
scoring = ['accuracy', 'balanced_accuracy', 'f1', 'precision', 'recall']
grid_search = {}
for key in X_train_d.keys():
cv = StratifiedKFold(n_splits=5, random_state=1)
model = XGBClassifier(objective="binary:logistic", random_state=42)
space = dict()
space['n_estimators']=[50] # 200
space['learning_rate']= [0.5] #0.01, 0.3, 0.5
grid_search= GridSearchCV(model, space, scoring=scoring, cv=cv, n_jobs=3, verbose=2, refit='balanced_accuracy')
Then, I create an empty dictionary that should be populated with as many GridSearchCV objects as X_train_d.keys(), via:
grid_result = {}
for key in X_train_d.keys():
grid_result[key] = grid_search.fit(X_train_d[key], Y_train_d[key])
Finally, I create as many datasets as the existing keys reporting info on scoring etc. via:
df_grid_results = {}
for key in X_train_d.keys():
df_grid_results[key]=pd.DataFrame(grid_search.cv_results_)
df_grid_results[key] = (
df_grid_results[key]
.set_index(df_grid_results[key]["params"].apply(
lambda x: "_".join(str(val) for val in x.values()))
)
.rename_axis('kernel')
)
All is working "perfectly" - in the sense that no error is shown - except that when I inspect either the different GridSearchCV objects or the df_grid_results datasets, I see that results are all identical as if the models were fit on the same dataset over and over again, while the X_train_d and Y_train_d dictionaries contain different datasets.
Of course, when I fit a model individually, like:
model1_cv = grid_search.fit(X_train_d[1], Y_train_d[1])
model2_cv = grid_search.fit(X_train_d[2], Y_train_d[2])
results differ as expected.
I feel like I am missing something really stupid and obvious here. Anybody can help? Thanks!